From 20e7d8d0ebdd684a086b6d4623210c0e36a13603 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 24 Jun 2024 15:22:40 +0200 Subject: [PATCH 1/3] Add initial support for stabby --- Cargo.toml | 10 +++- rust-toolchain | 2 +- src/_lib.rs | 5 ++ src/stabby/_mod.rs | 29 ++++++++++ src/stabby/boxed_impl.rs | 61 ++++++++++++++++++++ src/stabby/sync_impl.rs | 116 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 src/stabby/_mod.rs create mode 100644 src/stabby/boxed_impl.rs create mode 100644 src/stabby/sync_impl.rs diff --git a/Cargo.toml b/Cargo.toml index 935b4e8797..99f7b622d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 ", ] @@ -41,6 +41,7 @@ alloc = [] std = [ "alloc", "scopeguard/use_std", + "stabby/std", ] proc_macros = [] # Deprecated @@ -64,6 +65,8 @@ futures = [ "dyn-traits", ] +stabby = ["dep:stabby"] + tokio = [ "async-compat", "dep:tokio", @@ -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 = [ diff --git a/rust-toolchain b/rust-toolchain index 0403bed10c..0834888f55 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -1.66.1 +1.72.0 diff --git a/src/_lib.rs b/src/_lib.rs index d5457c3d41..d3ac62aad5 100644 --- a/src/_lib.rs +++ b/src/_lib.rs @@ -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; diff --git a/src/stabby/_mod.rs b/src/stabby/_mod.rs new file mode 100644 index 0000000000..8dad2f422d --- /dev/null +++ b/src/stabby/_mod.rs @@ -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 ReprC + for stabby::abi::Dyn<'_, Ptr, VTable> + { + type CLayout = CLayoutOf>; + fn is_valid(it: &'_ Self::CLayout) -> bool { + Ptr::is_valid(&it._0) && it._1.is_null().not() + } + } + + unsafe impl ReprC for stabby::abi::DynRef<'_, VTable> { + type CLayout = CLayoutOf>; + fn is_valid(it: &'_ Self::CLayout) -> bool { + it._0.is_null().not() && it._1.is_null().not() + } + } +} diff --git a/src/stabby/boxed_impl.rs b/src/stabby/boxed_impl.rs new file mode 100644 index 0000000000..69dc505afc --- /dev/null +++ b/src/stabby/boxed_impl.rs @@ -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 ReprC for Box +where + Box: IStable, +{ + type CLayout = *const CVoid; + fn is_valid(it: &'_ Self::CLayout) -> bool { + !Self::is_niche(it) && it.align_offset(core::mem::align_of::()) == 0 + } +} +unsafe impl __HasNiche__ for Box +where + Box: IStable, +{ + fn is_niche(it: &'_ ::CLayout) -> bool { + it.is_null() + } +} + +unsafe impl ReprC for BoxedSlice +where + Box: IStable, +{ + 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::()) == 0 + && it._1.align_offset(core::mem::align_of::()) == 0 + } +} +unsafe impl __HasNiche__ for BoxedSlice +where + Box: IStable, +{ + fn is_niche(it: &'_ ::CLayout) -> bool { + it._0.is_null() || it._1.is_null() + } +} +unsafe impl ReprC for BoxedStr +where + BoxedSlice: ReprC, +{ + type CLayout = as ReprC>::CLayout; + fn is_valid(it: &'_ Self::CLayout) -> bool { + BoxedSlice::::is_valid(it) + } +} +unsafe impl __HasNiche__ for BoxedStr +where + BoxedSlice: __HasNiche__, +{ + fn is_niche(it: &'_ Self::CLayout) -> bool { + BoxedSlice::::is_niche(it) + } +} diff --git a/src/stabby/sync_impl.rs b/src/stabby/sync_impl.rs new file mode 100644 index 0000000000..5a27e2fc3f --- /dev/null +++ b/src/stabby/sync_impl.rs @@ -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 ReprC for Arc +where + Arc: IStable, +{ + type CLayout = *const CVoid; + fn is_valid(it: &'_ Self::CLayout) -> bool { + !Self::is_niche(it) && it.align_offset(core::mem::align_of::()) == 0 + } +} +unsafe impl __HasNiche__ for Arc +where + Arc: IStable, +{ + fn is_niche(it: &'_ ::CLayout) -> bool { + it.is_null() + } +} + +unsafe impl ReprC for ArcSlice +where + Arc: IStable, +{ + 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::()) == 0 + && it._1.align_offset(core::mem::align_of::()) == 0 + } +} +unsafe impl __HasNiche__ for ArcSlice +where + Arc: IStable, +{ + fn is_niche(it: &'_ ::CLayout) -> bool { + it._0.is_null() || it._1.is_null() + } +} +unsafe impl ReprC for ArcStr +where + ArcSlice: ReprC, +{ + type CLayout = as ReprC>::CLayout; + fn is_valid(it: &'_ Self::CLayout) -> bool { + ArcSlice::::is_valid(it) + } +} +unsafe impl __HasNiche__ for ArcStr +where + ArcSlice: __HasNiche__, +{ + fn is_niche(it: &'_ Self::CLayout) -> bool { + ArcSlice::::is_niche(it) + } +} + +unsafe impl ReprC for Weak +where + Weak: IStable, +{ + type CLayout = *const CVoid; + fn is_valid(it: &'_ Self::CLayout) -> bool { + !Self::is_niche(it) && it.align_offset(core::mem::align_of::()) == 0 + } +} +unsafe impl __HasNiche__ for Weak +where + Weak: IStable, +{ + fn is_niche(it: &'_ ::CLayout) -> bool { + it.is_null() + } +} + +unsafe impl ReprC for WeakSlice +where + Weak: IStable, +{ + 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::()) == 0 + && it._1.align_offset(core::mem::align_of::()) == 0 + } +} +unsafe impl __HasNiche__ for WeakSlice +where + Weak: IStable, +{ + fn is_niche(it: &'_ ::CLayout) -> bool { + it._0.is_null() || it._1.is_null() + } +} +unsafe impl ReprC for WeakStr +where + WeakSlice: ReprC, +{ + type CLayout = as ReprC>::CLayout; + fn is_valid(it: &'_ Self::CLayout) -> bool { + WeakSlice::::is_valid(it) + } +} +unsafe impl __HasNiche__ for WeakStr +where + WeakSlice: __HasNiche__, +{ + fn is_niche(it: &'_ Self::CLayout) -> bool { + WeakSlice::::is_niche(it) + } +} From 61fd53f2452524d3fffa2854934836f605803c3f Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 24 Jun 2024 15:48:42 +0200 Subject: [PATCH 2/3] Update Cargo.toml Co-authored-by: Daniel Henry-Mantilla --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 99f7b622d9..bfedf309d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ alloc = [] std = [ "alloc", "scopeguard/use_std", - "stabby/std", + "stabby?/std", ] proc_macros = [] # Deprecated From 44f11bf747441f53e80fc79981f6c75f5ad4053f Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 24 Jun 2024 15:54:05 +0200 Subject: [PATCH 3/3] Update CI MSRV --- .github/workflows/gh-pages.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index be57730bb4..188b789fc8 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -57,7 +57,7 @@ jobs: - macos-latest # - windows-latest rust: - - 1.66.1 + - 1.72.1 - stable # - nightly steps: @@ -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 @@ -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 @@ -148,7 +148,7 @@ jobs: - macos-latest - windows-latest rust: - - 1.66.1 + - 1.72.1 - stable # - nightly steps: @@ -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