Skip to content

Commit

Permalink
Cast fat raw pointers to thin raw pointers without going through refs (
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejhirsz authored Mar 14, 2020
1 parent a167927 commit 64b7812
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 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 = "beef"
version = "0.2.0"
version = "0.2.1"
authors = ["Maciej Hirsz <hello@maciej.codes>"]
edition = "2018"
description = "More compact Cow"
Expand Down
16 changes: 10 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ unsafe impl Beef for str {
}

#[inline]
unsafe fn owned_from_parts(mut ptr: NonNull<Self>, capacity: NonZeroUsize) -> String {
unsafe fn owned_from_parts(ptr: NonNull<Self>, capacity: NonZeroUsize) -> String {
let len = ptr.as_ref().len();

String::from_utf8_unchecked(Vec::from_raw_parts(
ptr.as_mut().as_mut_ptr(),
ptr.as_mut().len(),
ptr.cast().as_ptr(),
len,
capacity.get(),
))
}
Expand All @@ -106,10 +108,12 @@ unsafe impl<T: Clone> Beef for [T] {
}

#[inline]
unsafe fn owned_from_parts(mut ptr: NonNull<Self>, capacity: NonZeroUsize) -> Vec<T> {
unsafe fn owned_from_parts(ptr: NonNull<Self>, capacity: NonZeroUsize) -> Vec<T> {
let len = ptr.as_ref().len();

Vec::from_raw_parts(
ptr.as_mut().as_mut_ptr(),
ptr.as_mut().len(),
ptr.cast().as_ptr(),
len,
capacity.get(),
)
}
Expand Down

0 comments on commit 64b7812

Please sign in to comment.