From 3c32aa7dcd60efa96d1bb6b4d20bb925a432b1e8 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 25 Mar 2021 16:55:59 -0400 Subject: [PATCH] Relax Sized bound on Own, Ref, Mut ptrs --- src/ptr.rs | 62 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/src/ptr.rs b/src/ptr.rs index 412903f..27a2235 100644 --- a/src/ptr.rs +++ b/src/ptr.rs @@ -3,20 +3,32 @@ use core::marker::PhantomData; use core::ptr::NonNull; #[repr(transparent)] -pub struct Own { +pub struct Own +where + T: ?Sized, +{ pub ptr: NonNull, } -unsafe impl Send for Own {} -unsafe impl Sync for Own {} -impl Copy for Own {} -impl Clone for Own { +unsafe impl Send for Own where T: ?Sized {} + +unsafe impl Sync for Own where T: ?Sized {} + +impl Copy for Own where T: ?Sized {} + +impl Clone for Own +where + T: ?Sized, +{ fn clone(&self) -> Self { *self } } -impl Own { +impl Own +where + T: ?Sized, +{ pub fn new(ptr: Box) -> Self { Own { ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) }, @@ -49,19 +61,29 @@ impl Own { } #[repr(transparent)] -pub struct Ref<'a, T> { +pub struct Ref<'a, T> +where + T: ?Sized, +{ pub ptr: NonNull, lifetime: PhantomData<&'a T>, } -impl<'a, T> Copy for Ref<'a, T> {} -impl<'a, T> Clone for Ref<'a, T> { +impl<'a, T> Copy for Ref<'a, T> where T: ?Sized {} + +impl<'a, T> Clone for Ref<'a, T> +where + T: ?Sized, +{ fn clone(&self) -> Self { *self } } -impl<'a, T> Ref<'a, T> { +impl<'a, T> Ref<'a, T> +where + T: ?Sized, +{ pub fn new(ptr: &'a T) -> Self { Ref { ptr: NonNull::from(ptr), @@ -82,19 +104,29 @@ impl<'a, T> Ref<'a, T> { } #[repr(transparent)] -pub struct Mut<'a, T> { +pub struct Mut<'a, T> +where + T: ?Sized, +{ pub ptr: NonNull, lifetime: PhantomData<&'a mut T>, } -impl<'a, T> Copy for Mut<'a, T> {} -impl<'a, T> Clone for Mut<'a, T> { +impl<'a, T> Copy for Mut<'a, T> where T: ?Sized {} + +impl<'a, T> Clone for Mut<'a, T> +where + T: ?Sized, +{ fn clone(&self) -> Self { *self } } -impl<'a, T> Mut<'a, T> { +impl<'a, T> Mut<'a, T> +where + T: ?Sized, +{ pub fn new(ptr: &'a mut T) -> Self { Mut { ptr: NonNull::from(ptr), @@ -119,7 +151,9 @@ impl<'a, T> Mut<'a, T> { pub unsafe fn deref_mut(self) -> &'a mut T { &mut *self.ptr.as_ptr() } +} +impl<'a, T> Mut<'a, T> { pub unsafe fn read(self) -> T { self.ptr.as_ptr().read() }