Skip to content

Commit

Permalink
Add no_std_lib test lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Voultapher committed Dec 18, 2023
1 parent 444b192 commit 213849f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests-extra/no_std_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "no_std_lib"
version = "0.1.0"
authors = ["Lukas Bergdoll <lukas.bergdoll@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
self_cell = { path = "../.." }
59 changes: 59 additions & 0 deletions tests-extra/no_std_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
);
}

0 comments on commit 213849f

Please sign in to comment.