Giter Site home page Giter Site logo

Conditional DekuWrite about deku HOT 3 CLOSED

whati001 avatar whati001 commented on June 4, 2024
Conditional DekuWrite

from deku.

Comments (3)

sharksforarms avatar sharksforarms commented on June 4, 2024 2

@whati001 I believe by combining skip and cond you can achieve what you're looking for?

You're right there isn't something like this for 'write side only' or 'read side only'

You can interpret it as "skip field (read or write) if cond"

Here's an example.

use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct MyStruct {
    should_next: bool,

    #[deku(skip, cond = "!*should_next")]
    // on read: if cond == true, we skip and the value is set to Default::default()
    // on write: if cond == true, we skip and the writing of the value
    optional: Option<u8>,

    other_field: u8,
}

fn main() {
    env_logger::init();

    {
        let input = &[0x01, 0x02, 0x03];
        let (_rest, r) = MyStruct::from_bytes((input, 0)).unwrap();
        assert_eq!(
            r,
            MyStruct {
                should_next: true,
                optional: Some(0x02),
                other_field: 0x03,
            }
        );

        assert_eq!(vec![0x01, 0x02, 0x03], r.to_bytes().unwrap())
    }

    {
        let input = &[0x00, 0x03];
        let (_rest, r) = MyStruct::from_bytes((input, 0)).unwrap();
        assert_eq!(
            r,
            MyStruct {
                should_next: false,
                optional: None,
                other_field: 0x03,
            }
        );

        assert_eq!(vec![0x00, 0x03], r.to_bytes().unwrap())
    }
}

from deku.

wcampbell0x2a avatar wcampbell0x2a commented on June 4, 2024

Deku does not have a skip_serializing_if attribute, but could add this.

from deku.

whati001 avatar whati001 commented on June 4, 2024

Hi @sharksforarms
Thank you very much for the quick feedback. You are right, seems like I have overseen to add the skip attribute.
Using the following deku #[deku(skip, cond = '...'] works now for me.

from deku.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.