!homDUJhmxHaZNgyLVA:matrix.org

binrw

159 Members
Official Matrix channel for the binread & binrw Rust libraries. Bridged to Discord. https://github.com/jam1garner/binrw/10 Servers

Load older messages


SenderMessageTime
29 Jan 2024
@alavie:matrix.orgalicehey! i'm wondering if there's a convenient way to have an Option<SomeT> in my #[binrw] struct that acts like a normal enum instead of optionally writing a value. e.g. have some magic byte to indicate the presence of a value and then either the value or all zeroes04:04:45
@snover:matrix.orgsnoverI’m having a little trouble understanding the request04:09:27
@snover:matrix.orgsnover you want the write-implementation to emit zeros for None? 04:10:03
@alavie:matrix.orgalice yes! and ofc one that then also reads back to None when the binary is all zeroes 04:10:40
@alavie:matrix.orgaliceand i'm totally okay with having an extra byte to mark which variant it is, i'm not too concerned about size04:11:04
@alavie:matrix.orgalice

i tried making my own Option-esque wrapper but running into some really weird type issues... :/
code:

#[binrw]
enum RWOption<T>
where
    T: binrw::BinWrite + binrw::BinRead + Default,
    for<'a> <T as binrw::BinRead>::Args<'a>: Default,
    for<'a> <T as binrw::BinWrite>::Args<'a>: Default,
{
    #[brw(magic = 0xffu8)]
    Some(T),
    #[brw(magic = 0x00u8)]
    None,
}

(along with an impl<T> From<Option<T>> for RWOption<T> and impl<T> From<RWOption<T>> for Option<T> omitted here for brevity)

but when i try to use it the type checker isn't happy. even just doing RWOption::Some(5) gives me:

the trait bound `for<'a> <{integer} as BinRead>::Args<'a>: Default` is not satisfied
the trait `for<'a> Default` is not implemented for `<{integer} as BinRead>::Args<'a>
index.rs(35, 13): this is a known limitation of the trait solver that will be lifted in the future
04:11:07
@alavie:matrix.orgalicebasically, i want to be able to (de)serialise Option<T> to a fixed length binary04:12:40
@snover:matrix.orgsnover to derive this way you will at least need to #[br(import_raw(args: <T as BinRead>::Args<'_>>)) and similar for binwrite and forward them to T, though i am not sure if this will solve complaint from the compiler 04:17:29
@alavie:matrix.orgalicewould it be too much to ask why? i'm honestly still very hazy on the wot Args stuff works04:19:32
@alavie:matrix.orgalicehm, doesn't fix it though04:21:43
@snover:matrix.orgsnover
#[binrw]
#[br(import_raw(r: <T as BinRead>::Args<'_>))]
#[bw(import_raw(r: <T as BinWrite>::Args<'_>))]
enum RWOption<T>
where
    T: BinWrite + BinRead,
{
    #[brw(magic = 0xffu8)]
    Some(#[br(args_raw = r)] #[bw(args_raw = r)] T),
    #[brw(magic = 0x00u8)]
    None,
}
04:22:02
@snover:matrix.orgsnover the reason why is that T has arguments to parse or write T, so if you are going to encapsulate T, then you need to be able to provide the arguments to parse or write T to T 04:23:05
@alavie:matrix.orgaliceoh! that seems to work!04:24:00
@alavie:matrix.orgalice
In reply to @snover:matrix.org
the reason why is that T has arguments to parse or write T, so if you are going to encapsulate T, then you need to be able to provide the arguments to parse or write T to T
ah ofc! that makes sense ^^
04:24:13
@alavie:matrix.orgaliceahhh, this actually works! thank you so much ^^ been banging my head against this for a while04:35:18
@snover:matrix.orgsnoverof course! i’m glad you are finding binrw useful.04:36:38
@alavie:matrix.orgaliceit's been wonderful! i'm a sucker for terse code and not having duplicated models and binrw has really delivered04:37:35
6 Feb 2024
@krensito:matrix.orgckrenslehner joined the room.07:48:19
@krensito:matrix.orgckrenslehner

Hello!
I am trying to parse a struct containing a slice.
I am using the deku crate right now. However, I want to dig into binrw more and I wonder how you would convert this struct in the intended way. I searched this channel and found something about a Cursor, but still I am a bit confused.

I want to use binrw as static library within a armv7 embedded project which is setup in C and CMake. I know that there is a global allocator and everything, but I wanted to explore the idea of using binrw in this no_std context and how I could approach things.

The struct:

#[derive(Debug, PartialEq, DekuRead)]
struct Test<'a> {
    pub size: u8,
    pub id: u16,
    #[deku(count = "*size - 2")]
    pub value: &'a [u8],
}

Thank you!
Cheers, Christian

10:21:07
@krensito:matrix.orgckrenslehner *

Hello!
I am trying to parse a struct containing a slice.
I am using the deku crate right now. However, I want to dig into binrw more and I wonder how you would convert this struct in the intended way. I searched this channel and found something about a Cursor, but still I am a bit confused.

I want to use binrw as static library within a armv7 embedded project which is setup in C and CMake. I know that there is a global allocator and everything, but I wanted to explore the idea of using binrw in this no_std context and how I could approach things.

The struct:

#[derive(Debug, PartialEq, DekuRead)]
struct Test<'a> {
    pub size: u8,
    pub id: u16,
    #[deku(count = "*size - 2")]
    pub value: &'a [u8],
}

Thank you!
Cheers, Christian

10:21:26
@krensito:matrix.orgckrenslehner *

Hello!
I am trying to parse a struct containing a slice.
I am using the deku crate right now. However, I want to dig into binrw more and I wonder how you would convert this struct in the intended way. I searched this channel and found something about a Cursor, but still I am a bit confused.

I want to use binrw as static library within a armv7 embedded project which is setup in C and CMake. I know that there is a global allocator and everything, but I wanted to explore the idea of using binrw in this no_std context and how I could approach things.

The struct:

#[derive(Debug, PartialEq, DekuRead)]
struct Test<'a> {
    pub size: u8,
    pub id: u16,
    #[deku(count = "*size - 2")]
    pub value: &'a [u8],
}

Thank you!
Cheers, Christian

10:21:50
@snover:matrix.orgsnover ckrenslehner: binrw reads and writes data from streams, so you would not be storing a slice, since the expectation is that the stream is copied from rather than borrowed from, as it is not possible to borrow from e.g. a network stream, file, etc. 16:54:44
9 Feb 2024
@krensito:matrix.orgckrenslehner snover: thank you for your response and sry for the late reply. Ok I understand. The whole file is already saved within the RAM of the C code of my embedded project. So maybe I can get this running via implementing BinRead in some way. :-) 09:28:07
@krensito:matrix.orgckrenslehner * snover: thank you for your response and sry for the late reply. Ok I understand. The whole file is already saved within the RAM of the C code of my embedded project. So maybe I can get this running via implementing BinRead and some FFI in some way. :-) 09:31:09
@badrb:matrix.orgbadrb set a profile picture.09:59:29
@snover:matrix.orgsnover ckrenslehner: if you don’t plan on ever reading data from anywhere other than memory, and you don’t need anything beyond than a way to represent the memory as a struct, you may just want to use something like zerocopy instead, which will be faster 16:10:16
14 Feb 2024
@rgnb:matrix.orgrgnbHello, I am trying to read a struct of various data and a XOR-32 checksum of all the bytes prior at the end. What is the best way to implement verifying this checksum?04:51:13
@rgnb:matrix.orgrgnbhttps://docs.rs/binrw/latest/binrw/docs/attribute/index.html#verifying-a-checksum04:54:44
@rgnb:matrix.orgrgnbfound an example :) 04:54:53
17 Mar 2024
@bksalman:matrix.orgBKSalman joined the room.16:12:14

There are no newer messages yet.


Back to Room ListRoom Version: 6