Skip to content

Use primitive formatting for primitive references#300

Merged
bors[bot] merged 5 commits intoknurling-rs:mainfrom
Sh3Rm4n:primref
Dec 15, 2020
Merged

Use primitive formatting for primitive references#300
bors[bot] merged 5 commits intoknurling-rs:mainfrom
Sh3Rm4n:primref

Conversation

@Sh3Rm4n
Copy link
Copy Markdown
Contributor

@Sh3Rm4n Sh3Rm4n commented Dec 14, 2020

Closes #163

A single recursive call in as_native_type is sufficient
to unpack the type from the reference.
Formatting functions (fmt.i32() etc.) are already taking
a reference, so this does not need to be changed.

Tests are adjusted, so that they pass.
This introduced change is not explicitly tested though.

#[derive(Format)]
struct S2<'a> {
    x: &'a i32,
    y: &'a u8,
}

now expands into

fn format(&self, f: &mut defmt::Formatter) {
    if f.needs_tag() {
        f.istr (&defmt::export::istr({ 
            #[link_section = ".defmt.{\"package\":\"use-test\",\"tag\":\"defmt_fmt\",\"data\":\"S2 {{ x: {:i32}, y: {:u8} }}\",\"disambiguator\":\"10404396863279507298\"}"]
            #[export_name = "{\"package\":\"use-test\",\"tag\":\"defmt_fmt\",\"data\":\"S2 {{ x: {:i32}, y: {:u8} }}\",\"disambiguator\":\"10404396863279507298\"}"]
            static S: u8 = 0; 
            &S as * const u8 as usize 
        })) ;
    }
    match self {
        Self { x, y } => {
            f.i32(x);
            f.u8(y);
        }
    }
}

Copy link
Copy Markdown
Member

@japaric japaric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This looks good.

This introduced change is not explicitly tested though.

(the unit test fixed in this PR is testing the derive macro change, at least it shows that the encoding is now smaller)

The CI/nightly build is broken for reasons unrelated to this this PR. We should figure out some workaround for that ...

Closes knurling-rs#163

A single recursive call in `as_native_type` is sufficient
to unpack the type from the reference.
Formatting functions (`fmt.i32()` etc.) are already taking
a reference, so this does not need to be changed.

Tests are adjusted, so that they pass.
This introduced change is not explicitly tested though.
#[warn(clippy::single_match)]
#[warn(clippy:or_fun_call)]
#[warn(clippy:useless_conversion)]
#[warn(clippy:match_like_matches_macro)]
@japaric
Copy link
Copy Markdown
Member

japaric commented Dec 15, 2020

bors r+

@bors
Copy link
Copy Markdown
Contributor

bors Bot commented Dec 15, 2020

Build succeeded:

@bors bors Bot merged commit 63966c1 into knurling-rs:main Dec 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#[derive] should use primitive formatting for &i32 and similar

2 participants