Giter Site home page Giter Site logo

Comments (4)

wcampbell0x2a avatar wcampbell0x2a commented on May 25, 2024

You can use if let for this: https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html. Unless I read your problem incorrectly.

if let DekuTest::Var4 { field_a, field_b } = &deku_test {
    println!("{field_a:?} {field_b:?}");
}

from deku.

sempervictus avatar sempervictus commented on May 25, 2024

I see, sorry was thinking about this the wrong way - have multiple arms there so trying to write this as a matcher, but this seems to do the trick - thank you!

from deku.

sempervictus avatar sempervictus commented on May 25, 2024

@wcampbell0x2a: accessing for reads does work this way, but changes to mutable elements do not.

Even manually assigning the vectors resulting from a changed element in nested access does not alter the contents of packet in this code or even selected - the s.val i'm setting to 0 here is still 1 when spat out from the debug_println! macro:

let mut data_pkt = Data::try_from(&packet.data[..]).unwrap();
match AdditionalOptions::try_from(&data_pkt.data[..]) {
    Ok(ao) => {
        for service in &ao.svcs {
            if let SvcBody::ENCRYPT { version: _, algos: _, selected } = service.svc {
                match selected {
                    Some(mut s) => {
                        s.val = 0;
                    }
                    None => {}
                };
                debug_println!("{:?}", &selected);
            }
        };
        debug_println!("{:?}", &ao);
        data_pkt.data = ao.try_into()?;
        packet.data = data_pkt.try_into()?;
    },
    Err(_) => {}
}

from deku.

sempervictus avatar sempervictus commented on May 25, 2024

This probably warrants docs all around, but getting changes to persist is far from straightforward. The mutable iterator requirement is normal Rust borrow checker stuff, but persisting the value of selected requires a mutable ref...

let mut ao = AdditionalOptions::try_from(c.data.as_ref())?;
for service in &mut ao.svcs {
    if let SvcBody::ENCRYPT { version: _, algos: _, ref mut selected } = service.svc {
        match *selected {
            Some(_s) => {
                *selected = None;
                service.hdr.sub_packets = service.hdr.sub_packets - 1;
            }
            None => {}
        };
    }
};

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.