Coder Social home page Coder Social logo

Set default per type about educe HOT 5 CLOSED

jayvdb avatar jayvdb commented on July 20, 2024
Set default per type

from educe.

Comments (5)

jayvdb avatar jayvdb commented on July 20, 2024

@magiclen, could I get your thoughts on this? I'm not great at macros, but I could take a crack at this if it is likely to get merged if I get it working.

from educe.

magiclen avatar magiclen commented on July 20, 2024

I'm not sure what you mean but for now I think you can just use

#[derive(Clone, Debug, Educe)]
#[educe(Default)]
pub struct Ipv4Range {
    #[educe(Default(expression = "[0, 0, 0, 0].into()"))]
    pub first: std::net::Ipv4Addr,
    #[educe(Default(expression = "[255, 255, 255, 255].into()"))]
    pub last: std::net::Ipv4Addr,
}

from educe.

jayvdb avatar jayvdb commented on July 20, 2024

I am aware I can do that, but I was hoping to avoid having to modify the code generation of the members to add this to every member. It is easier if I can tell educe at the struct level macro invocation that "when it sees any "Ipv4Addr" member, here is the value it should use as the default".

from educe.

magiclen avatar magiclen commented on July 20, 2024

Okay, I see.

I have a question. How can you tell Ipv4Addr are the same struct or not? If the code is like this,

mod a {
    pub struct Ipv4Addr(u32);
}

struct Ipv4Addr;

#[derive(Clone, Debug)]
#[educe(Default(literals = (Ipv4Addr = [0,0,0,0], Ipv6Addr = [0,0,0,0,...])))]
pub struct Ipv4Range {
    #[educe(Default)]
    pub first: std::net::Ipv4Addr,
    pub last: Ipv4Addr,
}

from educe.

jayvdb avatar jayvdb commented on July 20, 2024

I am doing codegen of the structs, so I know what types the members can be, and cant be. c.f. oxidecomputer/typify#184

from educe.

Related Issues (17)

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.