-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
275fe30
commit 8e2d29d
Showing
10 changed files
with
153 additions
and
25 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
/target | ||
**/target | ||
**/*.rs.bk | ||
Cargo.lock |
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
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
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,23 @@ | ||
[package] | ||
name = "ensure_no_std" | ||
version = "0.1.0" | ||
authors = ["sharks <sharks@sharks>"] | ||
edition = "2018" | ||
|
||
[profile.dev] | ||
panic = "abort" | ||
|
||
[profile.release] | ||
panic = "abort" | ||
lto = true | ||
|
||
[workspace] | ||
|
||
|
||
[features] | ||
default = ["alloc"] | ||
alloc = [] | ||
|
||
[dependencies] | ||
wee_alloc = "0.4" | ||
deku = { path = "../", default-features = false, features = ["alloc"] } |
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,69 @@ | ||
//! Based on https://github.com/rustwasm/wee_alloc/tree/master/example | ||
//! Run with `cargo +nightly run --release` | ||
#![no_std] | ||
#![no_main] | ||
#![feature(core_intrinsics, lang_items, alloc_error_handler)] | ||
|
||
extern crate alloc; | ||
extern crate wee_alloc; | ||
|
||
#[global_allocator] | ||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; | ||
|
||
// Need to provide a tiny `panic` implementation for `#![no_std]`. | ||
// This translates into an `unreachable` instruction that will | ||
// raise a `trap` the WebAssembly execution if we panic at runtime. | ||
#[panic_handler] | ||
#[no_mangle] | ||
pub fn panic(_info: &::core::panic::PanicInfo) -> ! { | ||
::core::intrinsics::abort(); | ||
} | ||
|
||
// Need to provide an allocation error handler which just aborts | ||
// the execution with trap. | ||
#[alloc_error_handler] | ||
#[no_mangle] | ||
pub extern "C" fn oom(_: ::core::alloc::Layout) -> ! { | ||
::core::intrinsics::abort(); | ||
} | ||
|
||
// Needed for non-wasm targets. | ||
#[lang = "eh_personality"] | ||
#[no_mangle] | ||
pub extern "C" fn eh_personality() {} | ||
|
||
use alloc::{vec, vec::Vec}; | ||
use deku::prelude::*; | ||
|
||
#[derive(Debug, PartialEq, DekuRead, DekuWrite)] | ||
struct DekuTest { | ||
#[deku(bits = "5")] | ||
field_a: u8, | ||
#[deku(bits = "3")] | ||
field_b: u8, | ||
count: u8, | ||
#[deku(len = "count")] | ||
data: Vec<u8>, | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn main() -> () { | ||
let test_data: Vec<u8> = vec![0b10101_101, 0x02, 0xBE, 0xEF]; | ||
|
||
// Test reading | ||
let (_rest, val) = DekuTest::from_bytes((&test_data, 0)).unwrap(); | ||
assert_eq!( | ||
DekuTest { | ||
field_a: 0b10101, | ||
field_b: 0b101, | ||
count: 0x02, | ||
data: vec![0xBE, 0xEF] | ||
}, | ||
val | ||
); | ||
|
||
// Test writing | ||
let val = val.to_bytes().unwrap(); | ||
assert_eq!(test_data, val); | ||
} |
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