Skip to content

Commit

Permalink
Fix doctests and add a bit of fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
p-avital committed May 29, 2024
1 parent 05e284a commit 19730c9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 58 deletions.
58 changes: 56 additions & 2 deletions Cargo.lock

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

70 changes: 17 additions & 53 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,50 @@ path = "src/_lib.rs"

[package]
name = "safer-ffi"
version = "0.1.8" # Keep in sync
authors = [
"Daniel Henry-Mantilla <[email protected]>",
]
version = "0.1.8" # Keep in sync
authors = ["Daniel Henry-Mantilla <[email protected]>"]
edition = "2021"

description = "Write safer FFI code in Rust without polluting it with unsafe code"
keywords = [
"ffi", "no_std", "framework", "safety", "bindings",
]
keywords = ["ffi", "no_std", "framework", "safety", "bindings"]
license = "MIT"
repository = "https://github.com/getditto/safer_ffi"

readme = "README.md"

exclude = [
"/guide",
]
exclude = ["/guide"]

[features]
default = [
"std",
]
default = ["std"]

# Document under the following features: all but for debug or experimental.
docs = [
"headers",
"default",
"nightly",
"tokio",
]
docs = ["headers", "default", "nightly", "tokio"]

nightly = []

alloc = []
std = [
"alloc",
"scopeguard/use_std",
]
std = ["alloc", "scopeguard/use_std"]

proc_macros = [] # Deprecated

async-fn = [
"safer_ffi-proc_macros/async-fn",
]
async-fn = ["safer_ffi-proc_macros/async-fn"]

debug_proc_macros = [
"safer_ffi-proc_macros/verbose-expansions",
]
debug_proc_macros = ["safer_ffi-proc_macros/verbose-expansions"]

dyn-traits = [
"safer_ffi-proc_macros/dyn-traits",
# FIXME: make this finer-grained
"std",
]

futures = [
"dep:futures",
"dyn-traits",
]
futures = ["dep:futures", "dyn-traits"]

tokio = [
"async-compat",
"dep:tokio",
"futures",
]
tokio = ["async-compat", "dep:tokio", "futures"]

headers = [
"inventory",
"safer_ffi-proc_macros/headers",
"std",
]
python-headers = [
"headers",
]
headers = ["inventory", "safer_ffi-proc_macros/headers", "std"]
python-headers = ["headers"]
# Tweak the generated `.h` ever so slightly.
c-headers-with-fn-style = [
"headers",
]
c-headers-with-fn-style = ["headers"]

# PRIVATE FEATURE / not part of crates.io package!
js = [
Expand Down Expand Up @@ -110,6 +75,7 @@ internal-tests = [
[dev-dependencies]
safer-ffi.path = "."
safer-ffi.features = ["internal-tests"]
rand = "0.8.5"

[dependencies]
async-compat.optional = true
Expand Down Expand Up @@ -139,9 +105,7 @@ scopeguard.default-features = false

tokio.optional = true
tokio.version = "1.26.0"
tokio.features = [
"rt",
]
tokio.features = ["rt"]

uninit.version = "0.5.0"
uninit.default-features = false
Expand All @@ -159,7 +123,7 @@ version = "0.0.3"

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

[workspace]
members = [
Expand Down
31 changes: 28 additions & 3 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::{
borrow::Borrow,
fmt::Debug,
hash::Hash,
ops::{Deref, Not, RangeBounds},
ops::{Deref, RangeBounds},
};

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -48,8 +48,9 @@ impl<'a> Bytes<'a> {
}
/// Slices `self` in-place:
/// ```
/// # use safer_ffi::bytes::Bytes;
/// let data = b"Hello there".as_slice();
/// let bytes = Bytes::from_static(data);
/// let mut bytes = Bytes::from_static(data);
/// bytes.subslice(3..7);
/// assert_eq!(&data[3..7], bytes.as_slice());
/// ```
Expand All @@ -74,6 +75,7 @@ impl<'a> Bytes<'a> {
}
/// Slices `self` in-place, returning it.
/// ```
/// # use safer_ffi::bytes::Bytes;
/// let data = b"Hello there".as_slice();
/// let bytes = Bytes::from_static(data);
/// assert_eq!(&data[3..7], bytes.subsliced(3..7).as_slice());
Expand All @@ -87,6 +89,7 @@ impl<'a> Bytes<'a> {
#[cfg(feature = "alloc")]
/// Splits the slice at `index`.
/// ```
/// # use safer_ffi::bytes::Bytes;
/// let data = b"Hello there".as_slice();
/// let index = 5;
/// let (l, r) = data.split_at(index);
Expand Down Expand Up @@ -283,7 +286,8 @@ impl BytesVt {
#[cfg(feature = "alloc")]
impl Clone for Bytes<'_> {
fn clone(&self) -> Self {
self.noalloc_clone().unwrap_or_else(|| Arc::<[u8]>::from(self.as_slice()).into())
self.noalloc_clone()
.unwrap_or_else(|| Arc::<[u8]>::from(self.as_slice()).into())
}
}
impl Drop for Bytes<'_> {
Expand All @@ -293,3 +297,24 @@ impl Drop for Bytes<'_> {
}
}
}

#[cfg(feature = "alloc")]
#[test]
fn fuzz() {
use rand::Rng;
let mut rng = rand::thread_rng();
for _ in 0..1000 {
let data = (0..rng.gen_range(10..100))
.map(|_| rng.gen())
.collect::<Arc<[u8]>>();
let bytes: Bytes<'_> = data.clone().into();
assert_eq!(bytes.as_slice(), &*data);
for _ in 0..100 {
let start = rng.gen_range(0..data.len());
let end = rng.gen_range(start..=data.len());
assert_eq!(bytes.clone().subsliced(start..end), &data[start..end]);
let (l, r) = bytes.clone().split_at(start).unwrap();
assert_eq!((l.as_slice(), r.as_slice()), data.split_at(start));
}
}
}

0 comments on commit 19730c9

Please sign in to comment.