Skip to content

Commit ce00418

Browse files
authored
Merge pull request #144 from dtolnay/ptr
Relax Sized bound on Own, Ref, Mut ptrs
2 parents a1baad2 + 3c32aa7 commit ce00418

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

src/ptr.rs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,32 @@ use core::marker::PhantomData;
33
use core::ptr::NonNull;
44

55
#[repr(transparent)]
6-
pub struct Own<T> {
6+
pub struct Own<T>
7+
where
8+
T: ?Sized,
9+
{
710
pub ptr: NonNull<T>,
811
}
912

10-
unsafe impl<T> Send for Own<T> {}
11-
unsafe impl<T> Sync for Own<T> {}
12-
impl<T> Copy for Own<T> {}
13-
impl<T> Clone for Own<T> {
13+
unsafe impl<T> Send for Own<T> where T: ?Sized {}
14+
15+
unsafe impl<T> Sync for Own<T> where T: ?Sized {}
16+
17+
impl<T> Copy for Own<T> where T: ?Sized {}
18+
19+
impl<T> Clone for Own<T>
20+
where
21+
T: ?Sized,
22+
{
1423
fn clone(&self) -> Self {
1524
*self
1625
}
1726
}
1827

19-
impl<T> Own<T> {
28+
impl<T> Own<T>
29+
where
30+
T: ?Sized,
31+
{
2032
pub fn new(ptr: Box<T>) -> Self {
2133
Own {
2234
ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },
@@ -49,19 +61,29 @@ impl<T> Own<T> {
4961
}
5062

5163
#[repr(transparent)]
52-
pub struct Ref<'a, T> {
64+
pub struct Ref<'a, T>
65+
where
66+
T: ?Sized,
67+
{
5368
pub ptr: NonNull<T>,
5469
lifetime: PhantomData<&'a T>,
5570
}
5671

57-
impl<'a, T> Copy for Ref<'a, T> {}
58-
impl<'a, T> Clone for Ref<'a, T> {
72+
impl<'a, T> Copy for Ref<'a, T> where T: ?Sized {}
73+
74+
impl<'a, T> Clone for Ref<'a, T>
75+
where
76+
T: ?Sized,
77+
{
5978
fn clone(&self) -> Self {
6079
*self
6180
}
6281
}
6382

64-
impl<'a, T> Ref<'a, T> {
83+
impl<'a, T> Ref<'a, T>
84+
where
85+
T: ?Sized,
86+
{
6587
pub fn new(ptr: &'a T) -> Self {
6688
Ref {
6789
ptr: NonNull::from(ptr),
@@ -82,19 +104,29 @@ impl<'a, T> Ref<'a, T> {
82104
}
83105

84106
#[repr(transparent)]
85-
pub struct Mut<'a, T> {
107+
pub struct Mut<'a, T>
108+
where
109+
T: ?Sized,
110+
{
86111
pub ptr: NonNull<T>,
87112
lifetime: PhantomData<&'a mut T>,
88113
}
89114

90-
impl<'a, T> Copy for Mut<'a, T> {}
91-
impl<'a, T> Clone for Mut<'a, T> {
115+
impl<'a, T> Copy for Mut<'a, T> where T: ?Sized {}
116+
117+
impl<'a, T> Clone for Mut<'a, T>
118+
where
119+
T: ?Sized,
120+
{
92121
fn clone(&self) -> Self {
93122
*self
94123
}
95124
}
96125

97-
impl<'a, T> Mut<'a, T> {
126+
impl<'a, T> Mut<'a, T>
127+
where
128+
T: ?Sized,
129+
{
98130
pub fn new(ptr: &'a mut T) -> Self {
99131
Mut {
100132
ptr: NonNull::from(ptr),
@@ -119,7 +151,9 @@ impl<'a, T> Mut<'a, T> {
119151
pub unsafe fn deref_mut(self) -> &'a mut T {
120152
&mut *self.ptr.as_ptr()
121153
}
154+
}
122155

156+
impl<'a, T> Mut<'a, T> {
123157
pub unsafe fn read(self) -> T {
124158
self.ptr.as_ptr().read()
125159
}

0 commit comments

Comments
 (0)