-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38e7f86
commit 0d5daa1
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
self_cell = { path = "../.." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |