### Run Deku WASM Example in Firefox Source: https://github.com/sharksforarms/deku/blob/master/ensure_wasm/README.md Opens the `index.html` file in the Firefox browser. This file typically loads the generated WebAssembly and JavaScript code to run the application in the browser environment. ```bash firefox index.html ``` -------------------------------- ### Allowing Token Streams for bytes Attribute in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Demonstrates the flexibility of the `bytes` attribute, allowing token streams for specifying the size. This example uses the value of `field_a_size` to determine the number of bytes to read for `field_a`. ```Rust # #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuTest { field_a_size: u8, #[deku(bytes = "*field_a_size as usize")] field_a: u32, } ``` -------------------------------- ### Using bit_order Attribute in Rust Struct Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Demonstrates how to apply the new `bit_order` attribute to a struct definition in Rust using the Deku library. This example sets the bit order for the entire struct to least significant bit first (`lsb`). ```Rust # #[derive(Debug, DekuRead, DekuWrite, PartialEq)] #[deku(bit_order = "lsb")] // <-- pub struct SquashfsV3 { #[deku(bits = "4")] inode_type: u8, #[deku(bits = "12")] mode: u16, uid: u8, guid: u8, mtime: u32, inode_number: u32, } let data: &[u8] = &[ // inode_type // ╭----------- // | // | mode // ╭+-------- ...and so on // || || // vv vv 0x31, 0x12, 0x04, 0x05, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, ]; ``` -------------------------------- ### Build Deku WASM with wasm-pack (Web Target) Source: https://github.com/sharksforarms/deku/blob/master/ensure_wasm/README.md Builds the Deku WASM project using `wasm-pack`, targeting the web environment. This command compiles the Rust code to WebAssembly and generates the necessary JavaScript and metadata for web usage. ```bash wasm-pack build --target web ``` -------------------------------- ### Add Deku Dependency (Standard) Source: https://github.com/sharksforarms/deku/blob/master/README.md Adds the standard Deku crate dependency to your Cargo.toml file. Requires Rust compiler version 1.81+. ```toml [dependencies] deku = "0.19" ``` -------------------------------- ### Read, Modify, and Write Binary Data with Deku Source: https://github.com/sharksforarms/deku/blob/master/README.md Demonstrates how to define a struct using Deku attributes, read binary data into the struct, modify a field, and write the struct back to binary data. Uses big-endian byte order. ```rust use deku::prelude::*; #[derive(Debug, PartialEq, DekuRead, DekuWrite)] #[deku(endian = "big")] struct DekuTest { #[deku(bits = 4)] field_a: u8, #[deku(bits = 4)] field_b: u8, field_c: u16, } fn main() { let data: Vec = vec![0b0110_1001, 0xBE, 0xEF]; let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap(); assert_eq!(DekuTest { field_a: 0b0110, field_b: 0b1001, field_c: 0xBEEF, }, val); val.field_c = 0xC0FE; let data_out = val.to_bytes().unwrap(); assert_eq!(vec![0b0110_1001, 0xC0, 0xFE], data_out); } ``` -------------------------------- ### Add Deku Dependency (no_std) Source: https://github.com/sharksforarms/deku/blob/master/README.md Adds the Deku crate dependency for no_std environments to your Cargo.toml file. Disables default features and enables the 'alloc' feature. ```toml [dependencies] deku = { version = "0.19", default-features = false, features = ["alloc"] } ``` -------------------------------- ### Implementing Custom Deku Reader (New API) in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Demonstrates the updated method for implementing custom readers using the `reader` attribute, accessing the input via `deku::reader`, and reading nested values with `from_reader_with_ctx`. ```Rust #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuTest { field_a: u8, #[deku( reader = "bit_flipper_read(*field_a, deku::reader, BitSize(8))", )] field_b: u8, } fn custom_read( field_a: u8, reader: &mut Reader, bit_size: BitSize, ) -> Result { // read field_b, calling original func let value = u8::from_reader_with_ctx(reader, bit_size)?; Ok(value) } ``` -------------------------------- ### Implementing Custom Deku Reader (Old API) in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Shows the previous method for implementing custom readers using the `reader` attribute and accessing the remaining input via `deku::rest`. ```Rust #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuTest { field_a: u8, #[deku( reader = "bit_flipper_read(*field_a, deku::rest, BitSize(8))", )] field_b: u8, } fn custom_read( field_a: u8, rest: &BitSlice, bit_size: BitSize, ) -> Result<(&BitSlice, u8), DekuError> { // read field_b, calling original func let (rest, value) = u8::read(rest, bit_size)?; Ok((rest, value)) } ``` -------------------------------- ### Using #[deku(until = ...)] Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Explains the `#[deku(until = ...)]` attribute, which allows reading a sequence (like a `Vec`) until a specified predicate expression evaluates to true. The predicate typically operates on the current element or state. ```Rust #[deku(until = "|item| *item == 0")] ``` -------------------------------- ### Implementing Custom Deku Writer (New API) - Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md This Rust code snippet illustrates the updated method for implementing a custom writer function in the Deku crate's new API. The function now accepts a generic `io::Write` implementation wrapped in `deku::writer::Writer`, enabling streaming writes. It accesses previous fields and the current field size similarly to the old API but uses the `to_writer` method for output and references the writer via `deku::writer` in the `#[deku(writer = "...")]` attribute. ```Rust fn bit_flipper_write( field_a: u8, field_b: u8, writer: &mut Writer, bit_size: BitSize, ) -> Result<(), DekuError> { // Access to previously written fields println!("field_a = 0x{:X}", field_a); // value of field_b println!("field_b = 0x{:X}", field_b); // Size of the current field println!("bit_size: {:?}", bit_size); // flip the bits on value if field_a is 0x01 let value = if field_a == 0x01 { !field_b } else { field_b }; value.to_writer(writer, bit_size) } #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuTest { field_a: u8, #[deku( writer = "bit_flipper_write(*field_a, *field_b, deku::writer, BitSize(8))" )] field_b: u8, } ``` -------------------------------- ### Implementing Custom Deku Writer (Old API) - Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md This Rust code snippet demonstrates the previous method for implementing a custom writer function used with the `#[deku(writer = "...")]` attribute in the Deku crate. The function takes a `BitVec` as output, allowing bit-level manipulation, and accesses previously written fields and the current field's size. It uses the `write` method and references the output via `deku::output` in the attribute. ```Rust fn bit_flipper_write( field_a: u8, field_b: u8, output: &mut BitVec, bit_size: BitSize, ) -> Result<(), DekuError> { // Access to previously written fields println!("field_a = 0x{:X}", field_a); // value of field_b println!("field_b = 0x{:X}", field_b); // Size of the current field println!("bit_size: {:?}", bit_size); // flip the bits on value if field_a is 0x01 let value = if field_a == 0x01 { !field_b } else { field_b }; value.write(output, bit_size) } #[derive(Debug, PartialEq, DekuRead, DekuWrite)] struct DekuTest { field_a: u8, #[deku( writer = "bit_flipper_write(*field_a, *field_b, deku::output, BitSize(8))" )] field_b: u8, } ``` -------------------------------- ### Using #[deku(ctx_default = ...)] Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Describes the `#[deku(ctx_default = ...)]` attribute, which provides a default value for a field's context if no context is explicitly provided during reading or writing. This simplifies usage for types that accept context. ```Rust #[deku(ctx_default = "MyContext::default()")] ``` -------------------------------- ### Updated Reader Usage with bit_order in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Shows the change in the internal `reader` API when using the `bit_order` feature. The `read_bytes` method now requires an explicit `Order` parameter, such as `Order::Lsb0`, reflecting the configured bit order. ```Diff - reader.read_bytes(exact.0, &mut bytes)?; + reader.read_bytes(exact.0, &mut bytes, Order::Lsb0)?; ``` -------------------------------- ### Using #[deku(magic = ...)] Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Introduces the `#[deku(magic = ...)]` attribute, used to specify a sequence of bytes that must be present at the beginning of the data being read for the decorated struct or field. Reading will fail if the magic bytes do not match. ```Rust #[deku(magic = b"\\xDE\\xAD\\xBE\\xEF")] ``` -------------------------------- ### Reading Struct from io::Read using Deku in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Illustrates the new API for reading Deku-derived structs from types implementing `std::io::Read`, such as `std::fs::File`, using the `from_reader` method. ```Rust use std::io::{Seek, SeekFrom, Read}; use std::fs::File; use deku::prelude::*; #[derive(Debug, DekuRead, DekuWrite, PartialEq, Eq, Clone, Hash)] #[deku(endian = "big")] struct EcHdr { magic: [u8; 4], version: u8, padding1: [u8; 3], } let mut file = File::options().read(true).open("file").unwrap(); let ec = EcHdr::from_reader((&mut file, 0)).unwrap(); ``` -------------------------------- ### Using Struct with Enum ID Pattern (New API) in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Illustrates the updated requirement that the type used with `id_pat` must match the `id_type` and disallows using tuples for the ID part, requiring a struct instead. ```Rust #[derive(PartialEq, Debug, DekuRead, DekuWrite)] #[deku(id_type = "u8")] enum DekuTest { #[deku(id_pat = "_")] VariantC { id: u8, other: u8, }, } ``` -------------------------------- ### Reading into Borrowed Byte Slice (Old API) in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Illustrates the previous capability of reading data directly into a borrowed byte slice (`&'a [u8]`) using the `bytes_read` attribute. ```Rust #[derive(PartialEq, Debug, DekuRead, DekuWrite)] struct TestStruct<'a> { bytes: u8, #[deku(bytes_read = "bytes")] data: &'a [u8], } ``` -------------------------------- ### Using deku:: Namespace in Attribute Expressions Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Demonstrates how to access internal variables like `deku::rest`, `deku::bit_offset`, and `deku::output` within string expressions used in Deku attributes such as `reader` and `writer`. This allows custom logic based on the current parsing/writing state. ```Rust reader = "my_reader(deku::rest, deku::bit_offset)" ``` ```Rust writer = "my_writer(deku::output)" ``` -------------------------------- ### Using bytes Attribute with CString and Field Context in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Illustrates how to use the `bytes` attribute with `CString` in a Rust struct. The size of the `CString` is dynamically determined by the value of another field (`len`) in the same struct. ```Rust #[derive(PartialEq, Debug, DekuRead, DekuWrite)] pub struct Data { len: u8, #[deku(bytes = "*len as usize")] s: CString, } ``` -------------------------------- ### Reading into Owned Byte Vector (New API) in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Demonstrates the updated requirement to read data into an owned `Vec` instead of a borrowed slice (`&'a [u8]`) when using the `bytes_read` attribute with the new `io::Read` based API. ```Rust #[derive(PartialEq, Debug, DekuRead, DekuWrite)] struct TestStruct { bytes: u8, #[deku(bytes_read = "bytes")] data: Vec, } ``` -------------------------------- ### Accessing Read Offsets via deku::bit_offset and deku::byte_offset Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Explains how to access the current bit and byte offsets during reading using the internal variables `deku::bit_offset` and `deku::byte_offset`. These variables are available within expressions evaluated by Deku attributes. ```Rust deku::bit_offset ``` ```Rust deku::byte_offset ``` -------------------------------- ### Using Tuple with Enum ID Pattern (Old API) in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Shows the previous syntax where an enum variant could use a tuple to store data when using `id_pat = "_"`, even if the `id_type` was a single value. ```Rust #[derive(PartialEq, Debug, DekuRead, DekuWrite)] #[deku(id_type = "u8")] enum DekuTest { #[deku(id_pat = "_")] VariantC((u8, u8)), } ``` -------------------------------- ### Using #[deku(bits_read = ...)] Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Details the `#[deku(bits_read = ...)]` attribute, used on container types like `Vec` to specify the exact number of bits to read for the container's content. The value can be a literal or an expression. ```Rust #[deku(bits_read = "16")] ``` -------------------------------- ### Using #[deku(endian = ...)] Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Explains the `#[deku(endian = ...)]` attribute, used to specify the endianness for reading or writing a field. It accepts the literals `"big"` or `"little"`, or an expression that evaluates to an endianness value. ```Rust #[deku(endian = "big")] ``` ```Rust #[deku(endian = "ctx.endian")] ``` -------------------------------- ### Using #[deku(bytes_read = ...)] Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Details the `#[deku(bytes_read = ...)]` attribute, used on container types like `Vec` to specify the exact number of bytes to read for the container's content. The value can be a literal or an expression. ```Rust #[deku(bytes_read = "count")] ``` -------------------------------- ### Using #[deku(id = ...)] Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Details the `#[deku(id = ...)]` attribute, primarily used on enum variants to associate them with a specific identifier value found in the input data. This allows Deku to select the correct variant based on the ID read from the stream. ```Rust #[deku(id = "1")] ``` -------------------------------- ### Updating Deku Enum Attribute in Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Demonstrates the change from using the `type` attribute to `id_type` on the `#[deku]` macro for enums, required after updating the `syn` library to 2.0. ```Rust #[derive(PartialEq, Debug, DekuRead, DekuWrite)] #[deku(id_type = "u8")] enum DekuTest { #[deku(id_pat = "_")] VariantC((u8, u8)), } ``` -------------------------------- ### Using #[deku(temp)] Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Describes the `#[deku(temp)]` attribute, which allows a field to be read and used within other attribute expressions (like `count`, `offset`, etc.) without being stored in the final struct or enum variant. Requires the `deku_derive` proc-macro attribute. ```Rust #[deku(temp)] ``` -------------------------------- ### Using Byte String Literal for Enum ID Attribute Rust Source: https://github.com/sharksforarms/deku/blob/master/CHANGELOG.md Shows how to specify an enum's identifier using a byte string literal (`b"..."`) within the `id` attribute. This is useful for matching specific byte sequences in the input data to enum variants. ```Rust id = b"0x01" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.