From 3a7bb22d0639c0a57185bdd7f63206eac14d4403 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 9 Jun 2020 21:39:05 +0100 Subject: [PATCH] why does into_boxed_slice() not work? --- src/iter.rs | 8 +++++ src/lib.rs | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/src/iter.rs b/src/iter.rs index e9202fd..b11e93d 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -54,6 +54,14 @@ impl Iterator for IntoIter } } +impl ExactSizeIterator for IntoIter +{ + fn len(&self) -> usize + { + self.sz + } +} + impl IntoIterator for HeapArray { type Item = T; diff --git a/src/lib.rs b/src/lib.rs index b760b56..5276e06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,18 +36,54 @@ mod tests { fn into_iter() { let primitive = heap![1,3,5,7,9u32]; - for x in primitive.into_iter() + let iter = primitive.into_iter(); + assert_eq!(iter.len(), 5); + for x in iter { assert_eq!(x % 2, 1); } let non = heap!["string one".to_owned(), "string two".to_owned()]; - for x in non.into_iter() + let iter = non.into_iter(); + assert_eq!(iter.len(), 2); + for x in iter { assert_eq!(&x[..6], "string"); } } + + #[test] + fn vec() + { + let heap = heap![0,1,2,3,4u8]; + let vec = vec![0,1,2,3,4u8]; + + assert_eq!(&vec[..], &heap[..]); + + let heap = Vec::from(heap); + + assert_eq!(vec,heap); + + let heap = HeapArray::from(heap); + + assert_eq!(&vec[..], &heap[..]); + } + + #[test] + fn boxed_slices() { + let array = [0,1,2,3,4]; + let vector = vec![0,1,2,3,4]; + assert_eq!(&vector[..], &array[..]); + let slice = vector.into_boxed_slice(); + assert_eq!(&slice[..], &array[..]); + let heap = HeapArray::from_boxed_slice(slice); + assert_eq!(&heap[..], &array[..]); + let slice = heap.into_boxed_slice(); + assert_eq!(&slice[..], &array[..]); + let vector = Vec::from(slice); + assert_eq!(&vector[..], &array[..]); + } } mod ptr; @@ -67,6 +103,10 @@ use std::{ self, SliceIndex, }, + marker::{ + Send, + Sync, + }, }; use crate::{ ptr::{ @@ -153,6 +193,11 @@ pub struct HeapArray { pub drop_check: bool, } +unsafe impl Sync for HeapArray +where T: Sync{} +unsafe impl Send for HeapArray +where T: Send{} + impl HeapArray { pub fn len_bytes(&self) -> usize @@ -172,6 +217,14 @@ impl HeapArray { std::mem::size_of::() == 1 } + pub unsafe fn from_raw_parts(ptr: *mut T, size: usize) -> Self + { + Self{ + ptr, + size, + drop_check: true, + } + } pub fn new(size: usize) -> Self { Self { @@ -258,6 +311,29 @@ impl HeapArray } } + pub fn into_raw(self) -> (*mut T, usize) + { + let op = (self.ptr, self.size); + std::mem::forget(self); + op + } + + pub fn from_boxed_slice(bx: Box<[T]>) -> Self + { + #[cfg(feature="assume_libc")] + unsafe { + let len = bx.len(); + Self::from_raw_parts(Box::<[T]>::into_raw(bx) as *mut T, len) + } + #[cfg(not(feature="assume_libc"))] + { //TODO: wtf? + let len = bx.len(); + let out = Self::from(Vec::from(bx)); + assert_eq!(len, out.len()); + out + } + } + #[allow(unused_mut)] pub fn into_boxed_slice(mut self) -> Box<[T]> { @@ -269,8 +345,10 @@ impl HeapArray } #[cfg(not(feature="assume_libc"))] { + let len = self.len(); let vec = Vec::from(self); - return vec.into_boxed_slice(); + assert_eq!(vec.len(), len); + vec.into_boxed_slice() } } } @@ -360,6 +438,8 @@ impl BorrowMut<[T]> for HeapArray } } +// `From`s + impl From> for Vec { fn from(ha: HeapArray) -> Self @@ -372,6 +452,18 @@ impl From> for Vec output } } +impl From> for HeapArray +{ + fn from(vec: Vec) -> Self + { + let mut output = HeapArray::new_uninit(vec.len()); + for (i,x) in (0..vec.len()).zip(vec.into_iter()) + { + output[i] = x; + } + output + } +} mod iter; pub use iter::*;