diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2022-08-18 19:50:04 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2022-08-18 19:50:04 -0300 |
commit | 9108dca91492727e8aca3483d8a19ff4c4402ee2 (patch) | |
tree | db191b06373b135d5b05b1ad490e062a752b621d /src/lib.rs | |
parent | 706020be6be529f13269a205e33e61ef3d4710a4 (diff) |
Attempt support for dyn Trait in Holder
This is a potentially-breaking change, so 0.2.0.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 171 |
1 files changed, 12 insertions, 159 deletions
diff --git a/src/lib.rs b/src/lib.rs index ac33aff..8a4b81e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,25 +70,6 @@ use core::marker::PhantomPinned; use core::pin::Pin; use core::mem; -/// A self-referential struct. -/// -/// # Example -/// -/// ```rust -/// #![feature(generic_associated_types)] -/// -/// use core::cell::Cell; -/// -/// use selfref::SelfRef; -/// -/// struct Foo<'a> { -/// foo: Cell<Option<&'a Foo<'a>>>, -/// } -/// impl<'a> SelfRef<'a> for Foo<'a> {} -/// ``` -#[deprecated(since = "0.1.3", note = "wasn't needed, can be safely removed")] -pub trait SelfRef<'a> {} - /// An opaqueified self-referential struct "key". /// /// # Safety @@ -124,7 +105,7 @@ pub trait SelfRef<'a> {} /// ``` pub unsafe trait Opaque { /// The actual self-referential struct. - type Kind<'a> where Self: 'a; + type Kind<'a>: ?Sized where Self: 'a; #[doc(hidden)] fn ub_check() { } @@ -269,8 +250,8 @@ macro_rules! opaque { /// } /// ``` pub struct Holder<'k, T> where T: Opaque + 'k { - inner: <T as Opaque>::Kind<'k>, _pinned: PhantomPinned, + inner: <T as Opaque>::Kind<'k>, } /// Helper trait for creating a [`Holder`]. @@ -280,12 +261,14 @@ pub struct Holder<'k, T> where T: Opaque + 'k { /// See [`Holder::new_with`] for examples. pub trait NewWith<'k, T: Opaque + 'k> { /// Creates a new `T::Kind` with an unbound lifetime. - fn new_with<'a>(self) -> T::Kind<'a> where 'k: 'a; + fn new_with<'a>(self) -> T::Kind<'a> where 'k: 'a, T::Kind<'a>: Sized; } impl<'k, T: Opaque + 'k, F> NewWith<'k, T> for F -where F: for<'a> FnOnce([&'a (); 0]) -> <T as Opaque>::Kind<'a> { - fn new_with<'a>(self) -> T::Kind<'a> where 'k: 'a { +where + F: for<'a> FnOnce([&'a (); 0]) -> <T as Opaque>::Kind<'a>, +{ + fn new_with<'a>(self) -> T::Kind<'a> where 'k: 'a, T::Kind<'a>: Sized { self([]) } } @@ -294,7 +277,10 @@ where F: for<'a> FnOnce([&'a (); 0]) -> <T as Opaque>::Kind<'a> { /// /// This only works if `K` is `'static`. pub fn new_with_closure<K: Opaque, F>(f: F) -> F -where F: for<'a> FnOnce([&'a (); 0]) -> <K as Opaque>::Kind<'a> { +where + F: for<'a> FnOnce([&'a (); 0]) -> <K as Opaque>::Kind<'a>, + for<'a> K::Kind<'a>: Sized, +{ f } @@ -376,7 +362,7 @@ impl<'k, T> Holder<'k, T> where T: Opaque { /// ``` pub fn new_with<F>(f: F) -> Self where - F: NewWith<'k, T> + F: NewWith<'k, T>, T::Kind<'k>: Sized { Self { // it is important that the constructor cannot observe 'k! @@ -535,136 +521,3 @@ 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::new_with_closure; -/// use selfref::opaque; -/// use selfref::operate_in_closure; -/// -/// #[derive(Default)] -/// struct Foo<'a> { -/// foo: Option<&'a UnsafeCell<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()); -/// } -/// }) -/// ); -/// } -/// ``` -#[allow(deprecated)] -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::new_with_closure; -/// use selfref::opaque; -/// use selfref::operate_in_closure; -/// -/// #[derive(Default)] -/// struct Foo<'a> { -/// foo: Option<&'a Cell<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()) -/// }); -/// }) -/// ); -/// } -/// ``` -#[allow(deprecated)] -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::new_with_closure; -/// use selfref::opaque; -/// use selfref::operate_in_closure; -/// -/// #[derive(Default)] -/// struct Foo<'a> { -/// foo: Option<&'a RefCell<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()); -/// }) -/// ); -/// } -/// ``` -#[allow(deprecated)] -impl<'a, T: SelfRef<'a>> SelfRef<'a> for core::cell::RefCell<T> {} |