summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2022-06-17 14:45:53 -0300
committerSoniEx2 <endermoneymod@gmail.com>2022-06-17 14:45:53 -0300
commit9cc8c6d516281cd23db770169ed764899268f12a (patch)
treee3f5d996b5f60b61f01b0b479d6d3d802f938df4
parent2dfa1fba89de316e197402c4ebb9b794e774d42c (diff)
Add some Cell impls
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs136
2 files changed, 137 insertions, 1 deletions
diff --git a/Cargo.toml b/Cargo.toml
index a689fc8..694ef85 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "selfref"
-version = "0.1.1"
+version = "0.1.2"
 authors = ["SoniEx2 <endermoneymod@gmail.com>"]
 edition = "2021"
 description = "Pain-free self-referential pinned types"
diff --git a/src/lib.rs b/src/lib.rs
index 265cd17..ef8d67d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -560,3 +560,139 @@ impl<'k, T> Holder<'k, T> where T: Opaque {
         })
     }
 }
+
+/// Allows working with holders to `UnsafeCell`.
+///
+/// # Example
+///
+/// ```rust
+/// #![feature(generic_associated_types)]
+/// #![feature(pin_macro)]
+///
+/// use core::cell::UnsafeCell;
+/// use core::pin::pin;
+/// 
+/// use selfref::Holder;
+/// use selfref::SelfRef;
+/// use selfref::new_with_closure;
+/// use selfref::opaque;
+/// use selfref::operate_in_closure;
+///
+/// #[derive(Default)]
+/// struct Foo<'a> {
+///     foo: Option<&'a UnsafeCell<Foo<'a>>>,
+/// }
+/// impl<'a> SelfRef<'a> for Foo<'a> {}
+///
+/// struct FooKey;
+/// opaque! {
+///     impl Opaque for FooKey {
+///         type Kind<'a> = UnsafeCell<Foo<'a>>;
+///     }
+/// }
+///
+/// fn main() {
+///     let holder = pin!(Holder::<'_, FooKey>::new_with(
+///         new_with_closure::<FooKey, _>(|foo| UnsafeCell::default())
+///     ));
+///     // Actually making our Foo refer to itself.
+///     holder.as_ref().operate_in(
+///         operate_in_closure::<FooKey, _, _>(|foo| {
+///             unsafe {
+///                 (*foo.get()).foo = Some(foo.get_ref());
+///             }
+///         })
+///     );
+/// }
+/// ```
+impl<'a, T: SelfRef<'a>> SelfRef<'a> for core::cell::UnsafeCell<T> {}
+
+/// Allows working with holders to `Cell`.
+///
+/// # Example
+///
+/// ```rust
+/// #![feature(generic_associated_types)]
+/// #![feature(pin_macro)]
+///
+/// use core::cell::Cell;
+/// use core::pin::pin;
+/// 
+/// use selfref::Holder;
+/// use selfref::SelfRef;
+/// use selfref::new_with_closure;
+/// use selfref::opaque;
+/// use selfref::operate_in_closure;
+///
+/// #[derive(Default)]
+/// struct Foo<'a> {
+///     foo: Option<&'a Cell<Foo<'a>>>,
+/// }
+/// impl<'a> SelfRef<'a> for Foo<'a> {}
+///
+/// struct FooKey;
+/// opaque! {
+///     impl Opaque for FooKey {
+///         type Kind<'a> = Cell<Foo<'a>>;
+///     }
+/// }
+///
+/// fn main() {
+///     let holder = pin!(Holder::<'_, FooKey>::new_with(
+///         new_with_closure::<FooKey, _>(|foo| Cell::default())
+///     ));
+///     // Actually making our Foo refer to itself.
+///     holder.as_ref().operate_in(
+///         operate_in_closure::<FooKey, _, _>(|foo| {
+///             foo.set(Foo {
+///                 foo: Some(foo.get_ref())
+///             });
+///         })
+///     );
+/// }
+/// ```
+impl<'a, T: SelfRef<'a>> SelfRef<'a> for core::cell::Cell<T> {}
+
+/// Allows working with holders to `RefCell`.
+///
+/// # Example
+///
+/// ```rust
+/// #![feature(generic_associated_types)]
+/// #![feature(pin_macro)]
+///
+/// use core::cell::RefCell;
+/// use core::pin::pin;
+/// 
+/// use selfref::Holder;
+/// use selfref::SelfRef;
+/// use selfref::new_with_closure;
+/// use selfref::opaque;
+/// use selfref::operate_in_closure;
+///
+/// #[derive(Default)]
+/// struct Foo<'a> {
+///     foo: Option<&'a RefCell<Foo<'a>>>,
+/// }
+/// impl<'a> SelfRef<'a> for Foo<'a> {}
+///
+/// struct FooKey;
+/// opaque! {
+///     impl Opaque for FooKey {
+///         type Kind<'a> = RefCell<Foo<'a>>;
+///     }
+/// }
+///
+/// fn main() {
+///     let holder = pin!(Holder::<'_, FooKey>::new_with(
+///         new_with_closure::<FooKey, _>(|foo| RefCell::default())
+///     ));
+///     // Actually making our Foo refer to itself.
+///     holder.as_ref().operate_in(
+///         operate_in_closure::<FooKey, _, _>(|foo| {
+///             foo.borrow_mut().foo = Some(foo.get_ref());
+///         })
+///     );
+/// }
+/// ```
+impl<'a, T: SelfRef<'a>> SelfRef<'a> for core::cell::RefCell<T> {}