-
Notifications
You must be signed in to change notification settings - Fork 13
slice-dst: Add example for SliceWithHeader #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MattesWhite
wants to merge
3
commits into
CAD97:master
Choose a base branch
from
MattesWhite:slice-dst_add-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
Cargo.lock |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use std::{ | ||
fmt::Display, | ||
mem::{self, MaybeUninit}, | ||
ops::Deref, | ||
}; | ||
|
||
use slice_dst::SliceWithHeader; | ||
|
||
/// Default capacity of [`MyVec`]. | ||
const MY_VEC_DEFAULT_CAPACITY: usize = 4; | ||
|
||
/// On the heap we will store the number of used elements in the slice (length) | ||
/// and a slice of (maybe uninitialized) values. | ||
/// | ||
/// For the sake of simplicity of this example the metadata is just a [`usize`]. | ||
/// However, in real use cases the metadata might be more complex than a | ||
/// [`Copy`] type. | ||
type HeapData<T> = SliceWithHeader<usize, MaybeUninit<T>>; | ||
|
||
/// Our [`Vec`] implementation. | ||
/// | ||
/// _Note:_ In contrast to [`std::vec::Vec`] this stores its length on the heap. | ||
struct MyVec<T>(Box<HeapData<T>>); | ||
|
||
impl<T> MyVec<T> { | ||
/// Empty [`MyVec`] with [default capacity](MY_VEC_DEFAULT_CAPACITY). | ||
fn new() -> Self { | ||
let inner = SliceWithHeader::new( | ||
0, | ||
(0..MY_VEC_DEFAULT_CAPACITY).map(|_| MaybeUninit::uninit()), | ||
); | ||
Self(inner) | ||
} | ||
/// Double the capacity of [`MyVec`]. | ||
/// | ||
/// Initialized elements are copied to the new allocated slice. | ||
fn grow(&mut self) { | ||
// Create an `ExactSizeIterator` double the size as the previous capacity. | ||
let iter = (0..2 * self.capacity()).map(|_| MaybeUninit::uninit()); | ||
// Allocate a new DST. | ||
let new = Self(SliceWithHeader::new(self.0.header, iter)); | ||
let mut old = mem::replace(self, new); | ||
for idx in 0..old.0.header { | ||
// Swap old, initialized values with new, uninitialized ones. | ||
mem::swap(&mut self.0.slice[idx], &mut old.0.slice[idx]) | ||
} | ||
// Reset length to prevent drop of uninitialized values. | ||
old.0.header = 0; | ||
} | ||
fn push(&mut self, element: T) { | ||
if self.len() == self.capacity() { | ||
self.grow(); | ||
} | ||
let len = &mut self.0.header; | ||
self.0.slice[*len] = MaybeUninit::new(element); | ||
*len += 1; | ||
} | ||
} | ||
|
||
impl<T> Drop for MyVec<T> { | ||
fn drop(&mut self) { | ||
let len = self.len(); | ||
self.0.slice.iter_mut().take(len).for_each(|t| { | ||
unsafe { | ||
// SAFETY: `take(len)` ensures that only initialized elements are dropped. | ||
std::ptr::drop_in_place(mem::transmute::<_, *mut T>(t)); | ||
}; | ||
}) | ||
} | ||
} | ||
|
||
impl<T> Deref for MyVec<T> { | ||
type Target = MySlice<T>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl<T> AsRef<MySlice<T>> for MyVec<T> { | ||
fn as_ref(&self) -> &MySlice<T> { | ||
// SAFETY: This is only safe because `MySlice` is a 'new-type' struct | ||
// that wraps the inner data of `MyVec`. Furthermore, `MySlice` has | ||
// `#[repr(transparent)]` which ensures that layout and alignment are | ||
// the same as `HeapData` directly. | ||
// | ||
// A more sophisticated and safe way to perform such tasks is to use | ||
// the derive macro from `ref-cast` crate. However, as it implements a | ||
// trait this would users enable to always cast `&MySlice` to | ||
// `&HeapData` which in turn would allow to modify the length, breaking | ||
// the contract that ensures the safety of `MyVec`. | ||
unsafe { mem::transmute(self.0.as_ref()) } | ||
} | ||
} | ||
|
||
/// The slice we get from a [`MyVec`]. | ||
/// | ||
/// We use the `ref-cast` crate to wrap the [`HeapData`] in our new-type | ||
/// which allows us to implement our own functions. | ||
#[repr(transparent)] | ||
struct MySlice<T>(HeapData<T>); | ||
|
||
impl<T> MySlice<T> { | ||
fn len(&self) -> usize { | ||
self.0.header | ||
} | ||
fn capacity(&self) -> usize { | ||
self.0.slice.len() | ||
} | ||
fn iter(&self) -> impl Iterator<Item = &T> { | ||
self.0.slice.iter().take(self.len()).map(|t| unsafe { | ||
// SAFETY: `take(len)` ensures that only initialized elements are iterated. | ||
mem::transmute(t) | ||
}) | ||
} | ||
} | ||
|
||
/// As [`MyVec`] implements [`Deref`] we can pass in a `&MyVec`. | ||
fn print_my_vec<T: Display>(slice: &MySlice<T>) { | ||
for (idx, t) in slice.iter().enumerate() { | ||
println!("{}. element: {}", idx, t); | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut my_vec = MyVec::new(); | ||
assert_eq!(MY_VEC_DEFAULT_CAPACITY, my_vec.capacity()); | ||
assert_eq!(0, my_vec.len()); | ||
|
||
my_vec.push("one"); | ||
my_vec.push("two"); | ||
my_vec.push("three"); | ||
my_vec.push("four"); | ||
my_vec.push("five"); | ||
assert_eq!(2 * MY_VEC_DEFAULT_CAPACITY, my_vec.capacity()); | ||
assert_eq!(5, my_vec.len()); | ||
print_my_vec(&my_vec); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: use
erasable::Thin
and storeThin<Box<HeapData<T>>>
, so the pointer is thin (single-usize
-big) and length/capacity are both exclusively on the heap.SliceWithHeader
stores its (slice) length inline to support erasing. (The eventual custom derive will omit this.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little bit reluctant to introduce
erasable::Thin
here. It addresses another issue and is from another crate. I would prefer to stick to an honorable mention here and put another example in theerasable
crate.