From 9cc8c6d516281cd23db770169ed764899268f12a Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Fri, 17 Jun 2022 14:45:53 -0300 Subject: Add some Cell impls --- Cargo.toml | 2 +- src/lib.rs | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) 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 "] 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>>, +/// } +/// impl<'a> SelfRef<'a> for Foo<'a> {} +/// +/// struct FooKey; +/// opaque! { +/// impl Opaque for FooKey { +/// type Kind<'a> = UnsafeCell>; +/// } +/// } +/// +/// fn main() { +/// let holder = pin!(Holder::<'_, FooKey>::new_with( +/// new_with_closure::(|foo| UnsafeCell::default()) +/// )); +/// // Actually making our Foo refer to itself. +/// holder.as_ref().operate_in( +/// operate_in_closure::(|foo| { +/// unsafe { +/// (*foo.get()).foo = Some(foo.get_ref()); +/// } +/// }) +/// ); +/// } +/// ``` +impl<'a, T: SelfRef<'a>> SelfRef<'a> for core::cell::UnsafeCell {} + +/// 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>>, +/// } +/// impl<'a> SelfRef<'a> for Foo<'a> {} +/// +/// struct FooKey; +/// opaque! { +/// impl Opaque for FooKey { +/// type Kind<'a> = Cell>; +/// } +/// } +/// +/// fn main() { +/// let holder = pin!(Holder::<'_, FooKey>::new_with( +/// new_with_closure::(|foo| Cell::default()) +/// )); +/// // Actually making our Foo refer to itself. +/// holder.as_ref().operate_in( +/// operate_in_closure::(|foo| { +/// foo.set(Foo { +/// foo: Some(foo.get_ref()) +/// }); +/// }) +/// ); +/// } +/// ``` +impl<'a, T: SelfRef<'a>> SelfRef<'a> for core::cell::Cell {} + +/// 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>>, +/// } +/// impl<'a> SelfRef<'a> for Foo<'a> {} +/// +/// struct FooKey; +/// opaque! { +/// impl Opaque for FooKey { +/// type Kind<'a> = RefCell>; +/// } +/// } +/// +/// fn main() { +/// let holder = pin!(Holder::<'_, FooKey>::new_with( +/// new_with_closure::(|foo| RefCell::default()) +/// )); +/// // Actually making our Foo refer to itself. +/// holder.as_ref().operate_in( +/// operate_in_closure::(|foo| { +/// foo.borrow_mut().foo = Some(foo.get_ref()); +/// }) +/// ); +/// } +/// ``` +impl<'a, T: SelfRef<'a>> SelfRef<'a> for core::cell::RefCell {} -- cgit 1.4.1