Skip to content

Commit

Permalink
0.5.2: fix zero length slices from vec
Browse files Browse the repository at this point in the history
  • Loading branch information
mohrezaei committed Oct 29, 2018
1 parent 24f7fbc commit 58739fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "thincollections"
version = "0.5.1"
version = "0.5.2"
authors = ["Mohammad Rezaei <[email protected]>"]
license = "MIT/Apache-2.0"
keywords = ["vector", "map", "set"]
Expand Down
18 changes: 9 additions & 9 deletions src/thin_v64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ impl<T> V64<T> {
unsafe {
let len = self.len() - 1;
self.set_len(len);
return ptr::read(1 as *const T);
return ptr::read(NonNull::dangling().as_ptr());
}
}
unsafe {
Expand Down Expand Up @@ -697,7 +697,7 @@ impl<T> V64<T> {
let len = self.len();
assert!(index < len);
self.set_len(len - 1);
return ptr::read(1 as *const T);
return ptr::read(NonNull::dangling().as_ptr());
}
}
let array: *mut T;
Expand Down Expand Up @@ -772,7 +772,7 @@ impl<T> V64<T> {
if mem::size_of::<T>() == 0 {
let mut count = 0;
unsafe {
let t: T = ptr::read(1 as *const T);
let t: T = ptr::read(NonNull::dangling().as_ptr());
for _i in 0..self.len() {
if f(&t) { count += 1; }
}
Expand Down Expand Up @@ -1125,7 +1125,7 @@ impl<T> V64<T> {
if len > 0 {
unsafe {
self.set_len(len - 1);
return Some(ptr::read(1 as *const T));
return Some(ptr::read(NonNull::dangling().as_ptr()));
}
}
return None;
Expand Down Expand Up @@ -1377,7 +1377,7 @@ impl<T> V64<T> {
pub fn into_boxed_slice(mut self) -> Box<[T]> {
if mem::size_of::<T>() == 0 {
unsafe {
let slice = slice::from_raw_parts_mut(1 as *mut T, self.len());
let slice = slice::from_raw_parts_mut(NonNull::dangling().as_ptr(), self.len());
let output: Box<[T]> = Box::from_raw(slice);
return output;
}
Expand Down Expand Up @@ -1915,7 +1915,7 @@ impl<T> Deref for V64<T> {

fn deref(&self) -> &[T] {
unsafe {
if mem::size_of::<T>() == 0 { return slice::from_raw_parts(1 as *const T, self.len()); }
if mem::size_of::<T>() == 0 { return slice::from_raw_parts(NonNull::dangling().as_ptr(), self.len()); }
match self.control() {
Control::Heap(ptr) => {
let len_ptr = ptr as *mut usize;
Expand All @@ -1935,7 +1935,7 @@ impl<T> Deref for V64<T> {
impl<T> DerefMut for V64<T> {
fn deref_mut(&mut self) -> &mut [T] {
unsafe {
if mem::size_of::<T>() == 0 { return slice::from_raw_parts_mut(1 as *mut T, self.len()); }
if mem::size_of::<T>() == 0 { return slice::from_raw_parts_mut(NonNull::dangling().as_ptr(), self.len()); }
match self.control() {
Control::Heap(ptr) => {
let len_ptr = ptr as *mut usize;
Expand Down Expand Up @@ -2185,7 +2185,7 @@ impl<T> Iterator for IntoIter<T> {

// Use a non-null pointer value
// (self.ptr might be null because of wrapping)
Some(ptr::read(1 as *mut T))
Some(ptr::read(NonNull::dangling().as_ptr()))
} else {
let old = self.ptr;
self.ptr = self.ptr.offset(1);
Expand Down Expand Up @@ -2224,7 +2224,7 @@ impl<T> DoubleEndedIterator for IntoIter<T> {

// Use a non-null pointer value
// (self.end might be null because of wrapping)
Some(ptr::read(1 as *mut T))
Some(ptr::read(NonNull::dangling().as_ptr()))
} else {
self.end = self.end.offset(-1);

Expand Down
22 changes: 11 additions & 11 deletions src/thin_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ impl<T> ThinVec<T> {
unsafe {
let len = self.len() - 1;
self.set_len(len);
return ptr::read(1 as *const T);
return ptr::read(NonNull::dangling().as_ptr());
}
}
unsafe {
Expand Down Expand Up @@ -723,7 +723,7 @@ impl<T> ThinVec<T> {
let len = self.len();
assert!(index < len);
self.set_len(len - 1);
return ptr::read(1 as *const T);
return ptr::read(NonNull::dangling().as_ptr());
}
}
let array: *mut T;
Expand Down Expand Up @@ -776,7 +776,7 @@ impl<T> ThinVec<T> {
if mem::size_of::<T>() == 0 {
let mut count = 0;
unsafe {
let t: T = ptr::read(1 as *const T);
let t: T = ptr::read(NonNull::dangling().as_ptr());
for _i in 0..self.len() {
if f(&t) { count += 1; }
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ impl<T> ThinVec<T> {
if len > 0 {
unsafe {
self.set_len(len - 1);
return Some(ptr::read(1 as *const T));
return Some(ptr::read(NonNull::dangling().as_ptr()));
}
}
return None;
Expand Down Expand Up @@ -1243,7 +1243,7 @@ impl<T> ThinVec<T> {
pub fn into_boxed_slice(mut self) -> Box<[T]> {
if mem::size_of::<T>() == 0 {
unsafe {
let slice = slice::from_raw_parts_mut(1 as *mut T, self.len());
let slice = slice::from_raw_parts_mut(NonNull::dangling().as_ptr(), self.len());
let output: Box<[T]> = Box::from_raw(slice);
return output;
}
Expand Down Expand Up @@ -1698,12 +1698,12 @@ impl<T> Deref for ThinVec<T> {

fn deref(&self) -> &[T] {
unsafe {
if mem::size_of::<T>() == 0 { return slice::from_raw_parts(1 as *const T, self.len()); }
if mem::size_of::<T>() == 0 { return slice::from_raw_parts(NonNull::dangling().as_ptr(), self.len()); }
let len: usize;
let arr: *mut T;
if self.u.get() == DANGLE {
len = 0;
arr = ptr::null_mut();
arr = NonNull::dangling().as_ptr();
} else {
let len_ptr = self.u.get() as *mut usize;
len = *len_ptr;
Expand All @@ -1717,12 +1717,12 @@ impl<T> Deref for ThinVec<T> {
impl<T> DerefMut for ThinVec<T> {
fn deref_mut(&mut self) -> &mut [T] {
unsafe {
if mem::size_of::<T>() == 0 { return slice::from_raw_parts_mut(1 as *mut T, self.len()); }
if mem::size_of::<T>() == 0 { return slice::from_raw_parts_mut(NonNull::dangling().as_ptr(), self.len()); }
let len: usize;
let arr: *mut T;
if self.u.get() == DANGLE {
len = 0;
arr = ptr::null_mut();
arr = NonNull::dangling().as_ptr();
} else {
let len_ptr = self.u.get() as *mut usize;
len = *len_ptr;
Expand Down Expand Up @@ -1944,7 +1944,7 @@ impl<T> Iterator for IntoIter<T> {

// Use a non-null pointer value
// (self.ptr might be null because of wrapping)
Some(ptr::read(1 as *mut T))
Some(ptr::read(NonNull::dangling().as_ptr()))
} else {
let old = self.ptr;
self.ptr = self.ptr.offset(1);
Expand Down Expand Up @@ -1983,7 +1983,7 @@ impl<T> DoubleEndedIterator for IntoIter<T> {

// Use a non-null pointer value
// (self.end might be null because of wrapping)
Some(ptr::read(1 as *mut T))
Some(ptr::read(NonNull::dangling().as_ptr()))
} else {
self.end = self.end.offset(-1);

Expand Down

0 comments on commit 58739fd

Please sign in to comment.