Skip to content

pack/unpack a bilge struct to/from &[u8] #33

Open
@jimy-byerley

Description

Hello, that's me again
What is the best way to safely pack/unpack a bilge struct to/from a byte slice ?

I have found the following in the readme, but not exactly what I am looking for:

#[bitsize(N)]
struct MyStruct {}

// safe way to cast from/to integers
let v = MyStruct::new(0_uN)
let n = v.value()
// unsafe way to cast from/to byte array
let v = unsafe{ std::mem::transmute<&[u8; N], &MyStruct>() }
let a = unsafe{ std::mem::transmute<&MyStruct, &[u8; N]>() }

Did I missed something for safely doing the same with byte slices or byte arrays ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions