{"id":18823601,"url":"https://github.com/yvt/cryo","last_synced_at":"2025-08-01T23:09:04.219Z","repository":{"id":57613514,"uuid":"157319413","full_name":"yvt/cryo","owner":"yvt","description":"\u0026'a T → impl Deref + 'static","archived":false,"fork":false,"pushed_at":"2021-10-26T04:25:04.000Z","size":101,"stargazers_count":8,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-28T19:48:18.731Z","etag":null,"topics":["lifetime","rust"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/cryo","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yvt.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-13T04:17:35.000Z","updated_at":"2021-10-26T04:25:07.000Z","dependencies_parsed_at":"2022-08-30T02:02:57.330Z","dependency_job_id":null,"html_url":"https://github.com/yvt/cryo","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/yvt/cryo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yvt%2Fcryo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yvt%2Fcryo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yvt%2Fcryo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yvt%2Fcryo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yvt","download_url":"https://codeload.github.com/yvt/cryo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yvt%2Fcryo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268310796,"owners_count":24230185,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lifetime","rust"],"created_at":"2024-11-08T00:54:08.724Z","updated_at":"2025-08-01T23:09:04.195Z","avatar_url":"https://github.com/yvt.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n\u003cimg src=\"doc/banner.svg\" alt=\"Cryo — Extend the lifetime of a reference. Safely.\"\u003e\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://docs.rs/cryo/\"\u003e\u003cimg src=\"https://docs.rs/cryo/badge.svg\" alt=\"docs.rs\"\u003e\u003c/a\u003e \u003ca href=\"https://crates.io/crates/cryo\"\u003e\u003cimg src=\"https://img.shields.io/crates/v/cryo\"\u003e\u003c/a\u003e \u003cimg src=\"https://img.shields.io/badge/license-MIT%2FApache--2.0-blue\"\u003e\n\u003c/p\u003e\n\nRequires Rust 1.34.0 or later.\n\nThis crate provides a cell-like type `Cryo` that is similar to `RefCell`\nexcept that it constrains the lifetime of its borrowed value\nthrough a runtime check mechanism, erasing the compile-time lifetime\ninformation. The lock guard `CryoRef` created from `Cryo` is\n`'static` and therefore can be used in various situations that require\n`'static` types, including:\n\n - Storing `CryoRef` temporarily in a `std::any::Any`-compatible container.\n - Capturing a reference to create a [Objective-C block](https://crates.io/crates/block).\n\nThis works by, when a `Cryo` is dropped, not letting the current thread's\nexecution move forward (at least¹) until all references to the expiring\n`Cryo` are dropped so that none of them can outlive the `Cryo`.\nThis is implemented by [readers-writer locks] under the hood.\n\n[readers-writer locks]: https://en.wikipedia.org/wiki/Readers–writer_lock\n\n\u003csub\u003e¹ `SyncLock` blocks the current thread's execution on lock failure.\n`LocalLock`, on the other hand, panics because it's designed for\nsingle-thread use cases and would deadlock otherwise.\u003c/sub\u003e\n\n## Examples\n\n`with_cryo`, `Cryo`, and `LocalLock` (single-thread lock\nimplementation, used by default):\n\n```rust\nuse std::{thread::spawn, pin::Pin};\n\nlet cell: usize = 42;\n\n// `with_cryo` uses `LocalLock` by default\nwith_cryo(\u0026cell, |cryo: Pin\u003c\u0026Cryo\u003c'_, usize, _\u003e\u003e| {\n    // Borrow `cryo` and move it into a `'static` closure.\n    let borrow: CryoRef\u003cusize, _\u003e = cryo.borrow();\n    let closure: Box\u003cdyn Fn()\u003e =\n        Box::new(move || { assert_eq!(*borrow, 42); });\n    closure();\n    drop(closure);\n\n    // Compile-time lifetime works as well.\n    assert_eq!(*cryo.get(), 42);\n\n    // When `cryo` is dropped, it will block until there are no other\n    // references to `cryo`. In this case, the program will leave\n    // this block immediately because `CryoRef` has already been dropped.\n});\n```\n\n`with_cryo`, `Cryo`, and `SyncLock` (thread-safe lock implementation):\n\n```rust\nuse std::{thread::spawn, pin::Pin};\n\nlet cell: usize = 42;\n\n// This time we are specifying the lock implementation\nwith_cryo((\u0026cell, lock_ty::\u003cSyncLock\u003e()), |cryo| {\n    // Borrow `cryo` and move it into a `'static` closure.\n    // `CryoRef` can be sent to another thread because\n    // `SyncLock` is thread-safe.\n    let borrow: CryoRef\u003cusize, _\u003e = cryo.borrow();\n    spawn(move || { assert_eq!(*borrow, 42); });\n\n    // Compile-time lifetime works as well.\n    assert_eq!(*cryo.get(), 42);\n\n    // When `cryo` is dropped, it will block until there are no other\n    // references to `cryo`. In this case, the program will not leave\n    // this block until the thread we just spawned completes execution.\n});\n```\n\n`with_cryo`, `CryoMut`, and `SyncLock`:\n\n```rust\nwith_cryo((\u0026mut cell, lock_ty::\u003cSyncLock\u003e()), |cryo_mut| {\n    // Borrow `cryo_mut` and move it into a `'static` closure.\n    let mut borrow: CryoMutWriteGuard\u003cusize, _\u003e = cryo_mut.write();\n    spawn(move || { *borrow = 1; });\n\n    // When `cryo_mut` is dropped, it will block until there are no other\n    // references to `cryo_mut`. In this case, the program will not leave\n    // this block until the thread we just spawned completes execution\n});\nassert_eq!(cell, 1);\n```\n\n**Don't** do these:\n\n```rust\n// The following statement will DEADLOCK because it attempts to drop\n// `Cryo` while a `CryoRef` is still referencing it, and `Cryo`'s\n// destructor will wait for the `CryoRef` to be dropped first (which\n// will never happen)\nlet borrow = with_cryo((\u0026cell, lock_ty::\u003cSyncLock\u003e()), |cryo| cryo.borrow());\n```\n\n```rust\n// The following statement will ABORT because it attempts to drop\n// `Cryo` while a `CryoRef` is still referencing it, and `Cryo`'s\n// destructor will panic, knowing no amount of waiting would cause\n// the `CryoRef` to be dropped\nlet borrow = with_cryo(\u0026cell, |cryo| cryo.borrow());\n```\n\n## Caveats\n\n- While it's capable of extending the effective lifetime of a reference,\n  it does not apply to nested references. For example, when\n  `\u0026'a NonStaticType\u003c'b\u003e` is supplied to `Cryo`'s constructor, the\n  borrowed type is `CryoRef\u003cNonStaticType\u003c'b\u003e\u003e`, which is still partially\n  bound to the original lifetime.\n\n## Details\n\n### Feature flags\n\n - `std` (enabled by default) enables `SyncLock`.\n\n - `lock_api` enables the blanket implementation of `Lock` on\n   all types implementing `lock_api::RawRwLock`, such as\n   [`spin::RawRwLock`][] and [`parking_lot::RawRwLock`][].\n\n - `atomic` (enabled by default) enables features that require full atomics,\n   which is not supported by some targets (detecting such targets is still\n   unstable ([#32976][])). This feature will be deprecated after the\n   stabilization of #32976.\n\n[`spin::RawRwLock`]: https://docs.rs/spin/0.9.0/spin/type.RwLock.html\n[`parking_lot::RawRwLock`]: https://docs.rs/parking_lot/0.11.1/parking_lot/struct.RawRwLock.html\n[#32976]: https://github.com/rust-lang/rust/issues/32976\n\n### Overhead\n\n`Cryo\u003cT, SyncLock\u003e`'s creation, destruction, borrowing, and unborrowing\neach take one or two atomic operations in the best cases.\n\nNeither of `SyncLock` and `LocalLock` require dynamic memory allocation.\n\n### Nomenclature\n\nFrom [cryopreservation](https://en.wikipedia.org/wiki/Cryopreservation).\n\n\nLicense: MIT/Apache-2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyvt%2Fcryo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyvt%2Fcryo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyvt%2Fcryo/lists"}