Skip to content

Commit

Permalink
Merge pull request #218 from getditto/pa/integrate-stabby-allocs
Browse files Browse the repository at this point in the history
Add initial support for stabby
  • Loading branch information
p-avital authored Jun 24, 2024
2 parents 8b7a8e0 + 44f11bf commit 3bc3848
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 7 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- macos-latest
# - windows-latest
rust:
- 1.66.1
- 1.72.1
- stable
# - nightly
steps:
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.66.1
toolchain: 1.72.1
override: true

- name: Clone repo
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.66.1
toolchain: 1.72.1
override: true

- name: Clone repo
Expand Down Expand Up @@ -148,7 +148,7 @@ jobs:
- macos-latest
- windows-latest
rust:
- 1.66.1
- 1.72.1
- stable
# - nightly
steps:
Expand Down Expand Up @@ -221,7 +221,7 @@ jobs:
with:
profile: default
override: true
toolchain: 1.66.1
toolchain: 1.72.1

- name: Install `mdbook`
uses: peaceiris/actions-mdbook@v1
Expand Down
10 changes: 9 additions & 1 deletion 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.8" # Keep in sync
version = "0.1.9" # Keep in sync
authors = [
"Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>",
]
Expand Down Expand Up @@ -41,6 +41,7 @@ alloc = []
std = [
"alloc",
"scopeguard/use_std",
"stabby?/std",
]

proc_macros = [] # Deprecated
Expand All @@ -64,6 +65,8 @@ futures = [
"dyn-traits",
]

stabby = ["dep:stabby"]

tokio = [
"async-compat",
"dep:tokio",
Expand Down Expand Up @@ -138,6 +141,11 @@ paste.version = "1.0.12"
scopeguard.version = "1.1.0"
scopeguard.default-features = false

stabby.version = "5.1.0"
stabby.optional = true
stabby.default-features = false
stabby.features = ["libc"]

tokio.optional = true
tokio.version = "1.26.0"
tokio.features = [
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.66.1
1.72.0
5 changes: 5 additions & 0 deletions src/_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ mod ptr;
pub
mod slice;

#[cfg(feature = "stabby")]
#[path = "stabby/_mod.rs"]
pub
mod stabby;

#[path = "string/_mod.rs"]
pub
mod string;
Expand Down
29 changes: 29 additions & 0 deletions src/stabby/_mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pub use stabby::*;

mod boxed_impl;
mod sync_impl;
mod fatptr_impl {
use core::ops::Not;

use crate::{
CVoid, Tuple2,
::{CLayoutOf, ConcreteReprC, ReprC},
};
use stabby::abi::{vtable::HasDropVt, IPtrOwned};

unsafe impl<Ptr: ConcreteReprC + IPtrOwned, VTable: HasDropVt> ReprC
for stabby::abi::Dyn<'_, Ptr, VTable>
{
type CLayout = CLayoutOf<Tuple2<Ptr, *const CVoid>>;
fn is_valid(it: &'_ Self::CLayout) -> bool {
Ptr::is_valid(&it._0) && it._1.is_null().not()
}
}

unsafe impl<VTable: HasDropVt> ReprC for stabby::abi::DynRef<'_, VTable> {
type CLayout = CLayoutOf<Tuple2<*const CVoid, *const CVoid>>;
fn is_valid(it: &'_ Self::CLayout) -> bool {
it._0.is_null().not() && it._1.is_null().not()
}
}
}
61 changes: 61 additions & 0 deletions src/stabby/boxed_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::{
CVoid, Tuple2_Layout,
::{ReprC, __HasNiche__},
};
pub use stabby::boxed::{Box, BoxedSlice, BoxedStr};
use stabby::{abi::U1, alloc::IAlloc, IStable};

unsafe impl<T: IStable, Alloc: IStable + IAlloc> ReprC for Box<T, Alloc>
where
Box<T, Alloc>: IStable<Size = U1>,
{
type CLayout = *const CVoid;
fn is_valid(it: &'_ Self::CLayout) -> bool {
!Self::is_niche(it) && it.align_offset(core::mem::align_of::<T>()) == 0
}
}
unsafe impl<T: IStable, Alloc: IStable + IAlloc> __HasNiche__ for Box<T, Alloc>
where
Box<T, Alloc>: IStable<Size = U1>,
{
fn is_niche(it: &'_ <Self as ReprC>::CLayout) -> bool {
it.is_null()
}
}

unsafe impl<T: IStable, Alloc: IStable + IAlloc> ReprC for BoxedSlice<T, Alloc>
where
Box<T, Alloc>: IStable<Size = U1>,
{
type CLayout = Tuple2_Layout<*const CVoid, *const CVoid>;
fn is_valid(it: &'_ Self::CLayout) -> bool {
!Self::is_niche(it)
&& it._0.align_offset(core::mem::align_of::<T>()) == 0
&& it._1.align_offset(core::mem::align_of::<T>()) == 0
}
}
unsafe impl<T: IStable, Alloc: IStable + IAlloc> __HasNiche__ for BoxedSlice<T, Alloc>
where
Box<T, Alloc>: IStable<Size = U1>,
{
fn is_niche(it: &'_ <Self as ReprC>::CLayout) -> bool {
it._0.is_null() || it._1.is_null()
}
}
unsafe impl<Alloc: IStable + IAlloc> ReprC for BoxedStr<Alloc>
where
BoxedSlice<u8, Alloc>: ReprC,
{
type CLayout = <BoxedSlice<u8, Alloc> as ReprC>::CLayout;
fn is_valid(it: &'_ Self::CLayout) -> bool {
BoxedSlice::<u8, Alloc>::is_valid(it)
}
}
unsafe impl<Alloc: IStable + IAlloc> __HasNiche__ for BoxedStr<Alloc>
where
BoxedSlice<u8, Alloc>: __HasNiche__,
{
fn is_niche(it: &'_ Self::CLayout) -> bool {
BoxedSlice::<u8, Alloc>::is_niche(it)
}
}
116 changes: 116 additions & 0 deletions src/stabby/sync_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use crate::{
CVoid, Tuple2_Layout,
::{ReprC, __HasNiche__},
};
pub use stabby::sync::{Arc, ArcSlice, ArcStr, Weak, WeakSlice, WeakStr};
use stabby::{abi::U1, alloc::IAlloc, IStable};

unsafe impl<T: IStable, Alloc: IStable + IAlloc> ReprC for Arc<T, Alloc>
where
Arc<T, Alloc>: IStable<Size = U1>,
{
type CLayout = *const CVoid;
fn is_valid(it: &'_ Self::CLayout) -> bool {
!Self::is_niche(it) && it.align_offset(core::mem::align_of::<T>()) == 0
}
}
unsafe impl<T: IStable, Alloc: IStable + IAlloc> __HasNiche__ for Arc<T, Alloc>
where
Arc<T, Alloc>: IStable<Size = U1>,
{
fn is_niche(it: &'_ <Self as ReprC>::CLayout) -> bool {
it.is_null()
}
}

unsafe impl<T: IStable, Alloc: IStable + IAlloc> ReprC for ArcSlice<T, Alloc>
where
Arc<T, Alloc>: IStable<Size = U1>,
{
type CLayout = Tuple2_Layout<*const CVoid, *const CVoid>;
fn is_valid(it: &'_ Self::CLayout) -> bool {
!Self::is_niche(it)
&& it._0.align_offset(core::mem::align_of::<T>()) == 0
&& it._1.align_offset(core::mem::align_of::<T>()) == 0
}
}
unsafe impl<T: IStable, Alloc: IStable + IAlloc> __HasNiche__ for ArcSlice<T, Alloc>
where
Arc<T, Alloc>: IStable<Size = U1>,
{
fn is_niche(it: &'_ <Self as ReprC>::CLayout) -> bool {
it._0.is_null() || it._1.is_null()
}
}
unsafe impl<Alloc: IStable + IAlloc> ReprC for ArcStr<Alloc>
where
ArcSlice<u8, Alloc>: ReprC,
{
type CLayout = <ArcSlice<u8, Alloc> as ReprC>::CLayout;
fn is_valid(it: &'_ Self::CLayout) -> bool {
ArcSlice::<u8, Alloc>::is_valid(it)
}
}
unsafe impl<Alloc: IStable + IAlloc> __HasNiche__ for ArcStr<Alloc>
where
ArcSlice<u8, Alloc>: __HasNiche__,
{
fn is_niche(it: &'_ Self::CLayout) -> bool {
ArcSlice::<u8, Alloc>::is_niche(it)
}
}

unsafe impl<T: IStable, Alloc: IStable + IAlloc> ReprC for Weak<T, Alloc>
where
Weak<T, Alloc>: IStable<Size = U1>,
{
type CLayout = *const CVoid;
fn is_valid(it: &'_ Self::CLayout) -> bool {
!Self::is_niche(it) && it.align_offset(core::mem::align_of::<T>()) == 0
}
}
unsafe impl<T: IStable, Alloc: IStable + IAlloc> __HasNiche__ for Weak<T, Alloc>
where
Weak<T, Alloc>: IStable<Size = U1>,
{
fn is_niche(it: &'_ <Self as ReprC>::CLayout) -> bool {
it.is_null()
}
}

unsafe impl<T: IStable, Alloc: IStable + IAlloc> ReprC for WeakSlice<T, Alloc>
where
Weak<T, Alloc>: IStable<Size = U1>,
{
type CLayout = Tuple2_Layout<*const CVoid, *const CVoid>;
fn is_valid(it: &'_ Self::CLayout) -> bool {
!Self::is_niche(it)
&& it._0.align_offset(core::mem::align_of::<T>()) == 0
&& it._1.align_offset(core::mem::align_of::<T>()) == 0
}
}
unsafe impl<T: IStable, Alloc: IStable + IAlloc> __HasNiche__ for WeakSlice<T, Alloc>
where
Weak<T, Alloc>: IStable<Size = U1>,
{
fn is_niche(it: &'_ <Self as ReprC>::CLayout) -> bool {
it._0.is_null() || it._1.is_null()
}
}
unsafe impl<Alloc: IStable + IAlloc> ReprC for WeakStr<Alloc>
where
WeakSlice<u8, Alloc>: ReprC,
{
type CLayout = <WeakSlice<u8, Alloc> as ReprC>::CLayout;
fn is_valid(it: &'_ Self::CLayout) -> bool {
WeakSlice::<u8, Alloc>::is_valid(it)
}
}
unsafe impl<Alloc: IStable + IAlloc> __HasNiche__ for WeakStr<Alloc>
where
WeakSlice<u8, Alloc>: __HasNiche__,
{
fn is_niche(it: &'_ Self::CLayout) -> bool {
WeakSlice::<u8, Alloc>::is_niche(it)
}
}

0 comments on commit 3bc3848

Please sign in to comment.