Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use primitive formatting for primitive references #300

Merged
merged 5 commits into from
Dec 15, 2020

Conversation

Sh3Rm4n
Copy link
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
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
Member

japaric commented Dec 15, 2020

bors r+

@bors
Copy link
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