Skip to content
Open
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
9 changes: 9 additions & 0 deletions rust/flatbuffers/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,13 +1227,18 @@ impl<T> IndexMut<ReverseIndexRange> for [T] {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "std"))]
use core::sync::atomic::{AtomicUsize, Ordering};
#[cfg(not(feature = "std"))]
use std::alloc::{GlobalAlloc, Layout, System};

#[cfg(not(feature = "std"))]
static ALLOC_COUNT: AtomicUsize = AtomicUsize::new(0);

#[cfg(not(feature = "std"))]
struct CountingAllocator;

#[cfg(not(feature = "std"))]
unsafe impl GlobalAlloc for CountingAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOC_COUNT.fetch_add(1, Ordering::Relaxed);
Expand All @@ -1244,13 +1249,16 @@ mod tests {
}
}

#[cfg(not(feature = "std"))]
#[global_allocator]
static GLOBAL: CountingAllocator = CountingAllocator;

#[cfg(not(feature = "std"))]
fn reset_alloc_count() {
ALLOC_COUNT.store(0, Ordering::Relaxed);
}

#[cfg(not(feature = "std"))]
fn alloc_count() -> usize {
ALLOC_COUNT.load(Ordering::Relaxed)
}
Expand All @@ -1264,6 +1272,7 @@ mod tests {
assert_eq!(idx.to_forward_index(&buf), 4);
}

#[cfg(not(feature = "std"))]
#[test]
fn with_internal_capacity_preallocates_vecs() {
let mut builder = FlatBufferBuilder::with_internal_capacity(64, 8, 16, 32);
Expand Down
3 changes: 3 additions & 0 deletions rust/flatbuffers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(all(test, not(feature = "std")))]
extern crate std;

mod array;
mod builder;
mod endian_scalar;
Expand Down
9 changes: 8 additions & 1 deletion tests/RustTest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ cargo test -- --quiet || exit /b 1
cargo run --bin=flatbuffers_alloc_check || exit /b 1
cargo run --bin=flexbuffers_alloc_check || exit /b 1
cargo run --bin=monster_example || exit /b 1
cd ..

cd ..\..\rust\flatbuffers
cargo test -- --quiet || exit /b 1

cd ..\flexbuffers
cargo test -- --quiet || exit /b 1

cd ..\reflection
cargo test -- --quiet || exit /b 1
15 changes: 15 additions & 0 deletions tests/RustTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,18 @@ if [[ $RUST_NIGHTLY == 1 ]]; then
rustup +nightly component add miri
MIRIFLAGS="-Zmiri-disable-isolation" cargo +nightly miri test
fi

cd ../../rust/flatbuffers
cargo test $TARGET_FLAG -- --quiet
check_test_result "Rust flatbuffers runtime tests"

cargo test $TARGET_FLAG --no-default-features -- --quiet
check_test_result "Rust flatbuffers runtime tests (no_std)"

cd ../flexbuffers
cargo test $TARGET_FLAG -- --quiet
check_test_result "Rust flexbuffers runtime tests"

cd ../reflection
cargo test $TARGET_FLAG -- --quiet
check_test_result "Rust reflection runtime tests"