« first day (2090 days earlier)      last day (1385 days later) » 

10:58 AM
Oy, Shep asked a question? I missed that.
 
11:22 AM
You're in for a dead end, it seems. The compiler's AST does not seem to be able to represent the full way literals were written in the source code.
 
12:01 PM
The AST and proc macro crates only care about semantics. Number literal formatting doesn't change the program. It's a very weak use case for quote.
 
@PeterHall I am using quote for more than just that, of course :-)
Example of what I'm generating
#[derive(Debug, Copy, Clone)]
enum Instruction {
    Nop {},
    Rjmp { k: i16 },
    Break {},
}
impl Instruction {
    fn cycles(self) -> u8 {
        match self {
            Instruction::Nop { .. } => 1u8,
            Instruction::Rjmp { .. } => 2u8,
            Instruction::Break { .. } => 1u8,
        }
    }
    fn decode(opcode: u32) -> core::option::Option<Self> {
        if (opcode & 0b_0000_0000_0000_0000_1111_1111_1111_1111_u32)
            == 0b_0000_0000_0000_0000_0000_0000_0000_0000_u32
 
Oh, I meant the other way around. The use case is too weak for it to be worth implementing as a feature of the quote crate.
 
struct AsBits<T>(T);

impl ToTokens for AsBits<u32> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        use std::str::FromStr;

        let b = format!(
            "0b_{:04b}_{:04b}_{:04b}_{:04b}_{:04b}_{:04b}_{:04b}_{:04b}_u32",
            (self.0 & 0xF000_0000) >> 28,
            (self.0 & 0x0F00_0000) >> 24,
            (self.0 & 0x00F0_0000) >> 20,
            (self.0 & 0x000F_0000) >> 16,
            (self.0 & 0x0000_F000) >> 12,
            (self.0 & 0x0000_0F00) >> 08,
            (self.0 & 0x0000_00F0) >> 04,
 
ie it's only for people that actually want to read the generated code.
But yeah I can see why you'd find that useful for debugging
 
12:27 PM
Now, I know this has been asked, but it's hard to find. I want to deserialize an input struct like {"the name": { "count": 42 }} as struct X { name: String, count: i32 }
I can write some custom deser., but I swear theres a serde way
5
Q: Is there a way to tell Serde to use a struct field as a map's key?

Tim ViséeI have a map of items that I would like to serialize to a list of structs, each having a field for the corresponding key. Imagine having a YAML file like this: name_a: some_field: 0 name_b: some_field: 0 name_c: some_field: 0 And a corresponding structure like this: struct Item {...

 
I did this the other way around just recently, and wrote a custom serialiser
custom deserialiser is more complicated though
 
in my case, the map is at the top level, so I can just do the transform outside of serde
 
12:45 PM
and you don't care about some overhead for that?
 
1:22 PM
@Shepmaster attribute and tool are generally for flat a data struct not unflat serde is not good at this
I guess "the name" can change ?
 
@PeterHall this is in a build script for a toy project, the only real overhead is the wasted BTreeMap and the iteration.
 
yeah sounds like it doesn't matter at all
 
@Stargateur yep. The name is the name of the operation
 
@Shepmaster so there is a limited number of possibility ?
why not use an enum ?
 
@Stargateur the entire purpose of this build script is to create that enum
 
1:32 PM
the input doesn't come from you I guess
 
I’m writing the yaml file using data copied from the instruction manual.
I just don’t want to perform the code translation by hand.
In the future, I could make this into a pre-generated file that was checked in. For now, I’m interesting too rapidly for that to be nice
 
@Shepmaster I think the more simple is just to deserialize as json value
And pick what you expect
 
1:55 PM
@Stargateur I don’t see how that would be simpler; care to share more?
When I get back to a computer, I can paste the code I have now.
 
well nevermind, the simpler is to use my answer code xd
 
2:25 PM
Still disagree with “simpler”. Maybe more efficient.
 

« first day (2090 days earlier)      last day (1385 days later) »