Skip to content
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

Fix Bytes' ReprC impl, as vtable is not necessarily aligned #235

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ path = "src/_lib.rs"

[package]
name = "safer-ffi"
version = "0.1.11" # Keep in sync
version = "0.1.12" # Keep in sync
authors = ["Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>"]
edition = "2021"
rust-version = "1.72.1"
Expand Down Expand Up @@ -137,7 +137,7 @@ version = "0.0.3"

[dependencies.safer_ffi-proc_macros]
path = "src/proc_macro"
version = "=0.1.11" # Keep in sync
version = "=0.1.12" # Keep in sync

[workspace]
members = [
Expand Down
4 changes: 2 additions & 2 deletions ffi_tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions js_tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 19 additions & 21 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,15 @@ pub struct Bytes<'a> {
len: usize,
data: *const (),
capacity: usize,
vtable: ptr::NonNull<BytesVt>,
vtable: ptr::NonNull<u8>,
marker: core::marker::PhantomData<&'a [u8]>,
}
#[cfg(not(feature = "stabby"))]
unsafe impl<'a> crate::layout::__HasNiche__ for Bytes<'a> {
fn is_niche(it: &'_ <Self as crate::ඞ::ReprC>::CLayout) -> bool {
!it.vtable.is_null()
}
}
unsafe impl<'a> crate::layout::__HasNiche__ for Bytes<'a> {}
#[cfg(feature = "stabby")]
unsafe impl<'a> crate::layout::__HasNiche__ for Bytes<'a>
where
Self: stabby::IStable<HasExactlyOneNiche = stabby::abi::B1>,
unsafe impl<'a> crate::layout::__HasNiche__ for Bytes<'a> where
Self: stabby::IStable<HasExactlyOneNiche = stabby::abi::B1>
{
fn is_niche(it: &'_ <Self as crate::ඞ::ReprC>::CLayout) -> bool {
!it.vtable.is_null()
}
}

const _: () = {
Expand Down Expand Up @@ -94,7 +86,7 @@ impl<'a> Bytes<'a> {
None
} else {
// SAFETY: If the value is not inlined, then we know the vtable to have been initialized to a valid reference.
unsafe { mem::transmute::<ptr::NonNull<BytesVt>, Option<&'a BytesVt>>(self.vtable) }
unsafe { mem::transmute::<ptr::NonNull<u8>, Option<&'a BytesVt>>(self.vtable) }
}
}

Expand All @@ -121,6 +113,7 @@ impl<'a> Bytes<'a> {
ptr::NonNull::new_unchecked(
&VT as &'static BytesVt as *const BytesVt as *mut BytesVt,
)
.cast()
},
marker: core::marker::PhantomData,
}
Expand Down Expand Up @@ -157,6 +150,7 @@ impl<'a> Bytes<'a> {
ptr::NonNull::new_unchecked(
&VT as &'static BytesVt as *const BytesVt as *mut BytesVt,
)
.cast()
},
marker: core::marker::PhantomData,
}
Expand Down Expand Up @@ -483,7 +477,7 @@ impl From<Arc<[u8]>> for Bytes<'static> {
len: data.len(),
data: Arc::into_raw(data) as *const (),
capacity,
vtable: From::<&'static _>::from(&ARC_BYTES_VT),
vtable: <ptr::NonNull<BytesVt> as From<&'static _>>::from(&ARC_BYTES_VT).cast(),
marker: core::marker::PhantomData,
}
}
Expand All @@ -503,10 +497,11 @@ impl<'a, T: Sized + AsRef<[u8]> + Send + Sync + 'a> From<Arc<T>> for Bytes<'a> {
len: data.len(),
capacity: data.len(),
data: Arc::into_raw(value) as *const (),
vtable: From::<&'static _>::from(&BytesVt {
vtable: <ptr::NonNull<BytesVt> as From<&'static _>>::from(&BytesVt {
release: Some(release::<T>),
retain: Some(retain::<T>),
}),
})
.cast(),
marker: core::marker::PhantomData,
}
}
Expand All @@ -528,10 +523,11 @@ impl From<alloc::boxed::Box<[u8]>> for Bytes<'_> {
len,
capacity: len,
data,
vtable: From::<&'static _>::from(&BytesVt {
vtable: <ptr::NonNull<BytesVt> as From<&'static _>>::from(&BytesVt {
release: Some(release_box_bytes),
retain: None,
}),
})
.cast(),
marker: core::marker::PhantomData,
}
}
Expand Down Expand Up @@ -584,7 +580,8 @@ impl From<stabby::sync::ArcSlice<u8>> for Bytes<'static> {
len,
data: mem::transmute(data.start),
capacity: mem::transmute(data.end),
vtable: From::<&'static _>::from(&STABBY_ARCSLICE_BYTESVT),
vtable: <ptr::NonNull<BytesVt> as From<&'static _>>::from(&STABBY_ARCSLICE_BYTESVT)
.cast(),
marker: core::marker::PhantomData,
}
}
Expand All @@ -610,10 +607,11 @@ impl<T: Sized + AsRef<[u8]> + Send + Sync + 'static> From<stabby::sync::Arc<T>>
len: data.len(),
capacity: data.len(),
data: unsafe { mem::transmute(stabby::sync::Arc::into_raw(value)) },
vtable: From::<&'static _>::from(&BytesVt {
vtable: <ptr::NonNull<BytesVt> as From<&'static _>>::from(&BytesVt {
release: Some(release_stabby_arc::<T>),
retain: Some(retain_stabby_arc::<T>),
}),
})
.cast(),
marker: core::marker::PhantomData,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ proc-macro = true

[package]
name = "safer_ffi-proc_macros"
version = "0.1.11" # Keep in sync
version = "0.1.12" # Keep in sync
authors = ["Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>"]
edition = "2021"

Expand Down
Loading