Skip to content
Myr

Merge pull request #144 from dtolnay/ptr

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Relax Sized bound on Own, Ref, Mut ptrs

authored by David Tolnay · committed by GitHub · parents a1baad2, 3c32aa7 · Browse files · 1 file+48 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+48 added−14 removed

  • other:+48 added−14 removed

1 file+48 added−14 removed

modified:src/ptr.rs+48 added-14 removed
1 unmodified line
2use core::marker::PhantomData;3use core::ptr::NonNull;45#[repr(transparent)]6pub struct Own<T> {6pub struct Own<T>7where8 T: ?Sized,9{10 pub ptr: NonNull<T>,11}1210unsafe impl<T> Send for Own<T> {}11unsafe impl<T> Sync for Own<T> {}12impl<T> Copy for Own<T> {}13impl<T> Clone for Own<T> {13unsafe impl<T> Send for Own<T> where T: ?Sized {}1415unsafe impl<T> Sync for Own<T> where T: ?Sized {}1617impl<T> Copy for Own<T> where T: ?Sized {}1819impl<T> Clone for Own<T>20where21 T: ?Sized,22{23 fn clone(&self) -> Self {24 *self25 }26}2719impl<T> Own<T> {28impl<T> Own<T>29where30 T: ?Sized,31{32 pub fn new(ptr: Box<T>) -> Self {33 Own {34 ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },35 }
24 unmodified lines
60 }61}6263#[repr(transparent)]52pub struct Ref<'a, T> {64pub struct Ref<'a, T>65where66 T: ?Sized,67{68 pub ptr: NonNull<T>,69 lifetime: PhantomData<&'a T>,70}7157impl<'a, T> Copy for Ref<'a, T> {}58impl<'a, T> Clone for Ref<'a, T> {72impl<'a, T> Copy for Ref<'a, T> where T: ?Sized {}7374impl<'a, T> Clone for Ref<'a, T>75where76 T: ?Sized,77{78 fn clone(&self) -> Self {79 *self80 }81}8264impl<'a, T> Ref<'a, T> {83impl<'a, T> Ref<'a, T>84where85 T: ?Sized,86{87 pub fn new(ptr: &'a T) -> Self {88 Ref {89 ptr: NonNull::from(ptr),90 lifetime: PhantomData,
12 unmodified lines
103 }104}105106#[repr(transparent)]85pub struct Mut<'a, T> {107pub struct Mut<'a, T>108where109 T: ?Sized,110{111 pub ptr: NonNull<T>,112 lifetime: PhantomData<&'a mut T>,113}11490impl<'a, T> Copy for Mut<'a, T> {}91impl<'a, T> Clone for Mut<'a, T> {115impl<'a, T> Copy for Mut<'a, T> where T: ?Sized {}116117impl<'a, T> Clone for Mut<'a, T>118where119 T: ?Sized,120{121 fn clone(&self) -> Self {122 *self123 }124}12597impl<'a, T> Mut<'a, T> {126impl<'a, T> Mut<'a, T>127where128 T: ?Sized,129{130 pub fn new(ptr: &'a mut T) -> Self {131 Mut {132 ptr: NonNull::from(ptr),133 lifetime: PhantomData,
16 unmodified lines
150151 pub unsafe fn deref_mut(self) -> &'a mut T {152 &mut *self.ptr.as_ptr()153 }154}155156impl<'a, T> Mut<'a, T> {157 pub unsafe fn read(self) -> T {158 self.ptr.as_ptr().read()159 }160}
9 unmodified lines