Skip to content

Commit e22012b

Browse files
committed
FIX: Use &mut properly for ContiguousMut begin_mut/end_mut
This is the only sound way to derive proper raw mut pointers from slices.
1 parent be9d4f4 commit e22012b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/container_traits.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ pub unsafe trait GetUncheckedMut : GetUnchecked {
2222
}
2323

2424
pub unsafe trait ContiguousMut : Contiguous {
25-
fn begin_mut(&self) -> *mut Self::Item {
26-
self.begin() as _
27-
}
28-
fn end_mut(&self) -> *mut Self::Item {
29-
self.end() as _
30-
}
25+
fn begin_mut(&mut self) -> *mut Self::Item;
26+
fn end_mut(&mut self) -> *mut Self::Item;
3127
fn as_mut_slice(&mut self) -> &mut [Self::Item];
3228
}
3329

@@ -52,6 +48,8 @@ unsafe impl<'a, C: ?Sized> Trustworthy for &'a mut C
5248
unsafe impl<'a, C: ?Sized> ContiguousMut for &'a mut C
5349
where C: ContiguousMut
5450
{
51+
fn begin_mut(&mut self) -> *mut Self::Item { (**self).begin_mut() }
52+
fn end_mut(&mut self) -> *mut Self::Item { (**self).end_mut() }
5553
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
5654
(**self).as_mut_slice()
5755
}
@@ -115,6 +113,14 @@ unsafe impl<T> Trustworthy for [T] {
115113
}
116114

117115
unsafe impl<T> ContiguousMut for [T] {
116+
fn begin_mut(&mut self) -> *mut Self::Item {
117+
self.as_mut_ptr()
118+
}
119+
fn end_mut(&mut self) -> *mut Self::Item {
120+
unsafe {
121+
self.begin_mut().add(self.len())
122+
}
123+
}
118124
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
119125
self
120126
}
@@ -138,7 +144,7 @@ unsafe impl<T> Contiguous for [T] {
138144
}
139145
fn end(&self) -> *const Self::Item {
140146
unsafe {
141-
self.begin().offset(self.len() as isize)
147+
self.begin().add(self.len())
142148
}
143149
}
144150
fn as_slice(&self) -> &[Self::Item] {
@@ -155,6 +161,8 @@ mod vec_impls {
155161
}
156162

157163
unsafe impl<T> ContiguousMut for Vec<T> {
164+
fn begin_mut(&mut self) -> *mut Self::Item { (**self).begin_mut() }
165+
fn end_mut(&mut self) -> *mut Self::Item { (**self).end_mut() }
158166
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
159167
self
160168
}

0 commit comments

Comments
 (0)