diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 97d809e..f85915b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -29,6 +29,10 @@ jobs: run: | cd tests-extra cargo test --features=invalid_programs --verbose + - name: Run tests-extra no_std_lib + run: | + cd tests-extra/no_std_lib + cargo test --verbose - name: Run examples run: | cd examples @@ -71,6 +75,9 @@ jobs: rustup component add miri shell: bash + - name: Run tests with nightly basic + run: | + cargo test --verbose - name: Run tests x86_64-unknown-linux-gnu run: | cargo miri test --verbose --target x86_64-unknown-linux-gnu diff --git a/tests-extra/no_std_lib/Cargo.toml b/tests-extra/no_std_lib/Cargo.toml new file mode 100644 index 0000000..d9261d2 --- /dev/null +++ b/tests-extra/no_std_lib/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "no_std_lib" +version = "0.1.0" +authors = ["Lukas Bergdoll "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +self_cell = { path = "../.." } diff --git a/tests-extra/no_std_lib/src/lib.rs b/tests-extra/no_std_lib/src/lib.rs new file mode 100644 index 0000000..b39cb19 --- /dev/null +++ b/tests-extra/no_std_lib/src/lib.rs @@ -0,0 +1,59 @@ +#![no_std] + +use self_cell::self_cell; + +// Not using alloc is on purpose, self_cell should also work in such scenarios. + +const SCRATCH_REGION: [u8; 4096] = [0u8; 4096]; + +#[derive(Eq, PartialEq)] +struct StaticString { + region: &'static [u8], +} + +const MAX_NODES: usize = 8; + +#[derive(Eq, PartialEq)] +struct Ast<'a>([Option<&'a [u8]>; MAX_NODES]); + +self_cell!( + struct AstCell { + owner: StaticString, + + #[covariant] + dependent: Ast, + } + + impl {Eq, PartialEq} +); + +fn build_ast_cell() -> AstCell { + // Yes in a static setting self_cell is not terribly useful, this could be solved differently. + // This is only a test. + let pre_processed_code = StaticString { + region: &SCRATCH_REGION[4000..4015], + }; + + AstCell::new(pre_processed_code, |code| { + let mut ast_nodes = [None; MAX_NODES]; + ast_nodes[0] = Some(&code.region[3..7]); + ast_nodes[1] = Some(&code.region[10..12]); + + Ast(ast_nodes) + }) +} + +#[test] +fn self_cell_works_in_no_std_env() { + let ast_cell = build_ast_cell(); + assert_eq!(ast_cell.borrow_owner().region.len(), 15); + assert_eq!( + ast_cell + .borrow_dependent() + .0 + .iter() + .filter(|val| val.is_some()) + .count(), + 2 + ); +}