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

implement Format for core::time::Duration #348

Closed
japaric opened this issue Jan 19, 2021 · 8 comments · Fixed by #391
Closed

implement Format for core::time::Duration #348

japaric opened this issue Jan 19, 2021 · 8 comments · Fixed by #391
Assignees
Labels
difficulty: easy Pretty easy to solve good first issue Good for newcomers priority: medium Medium priority for the Knurling team status: needs PR Issue just needs a Pull Request implementing the changes type: enhancement Enhancement or feature request

Comments

@japaric
Copy link
Member

japaric commented Jan 19, 2021

it would be nice if this type was supported

@japaric japaric added the type: enhancement Enhancement or feature request label Jan 19, 2021
@jonas-schievink jonas-schievink added difficulty: easy Pretty easy to solve good first issue Good for newcomers priority: medium Medium priority for the Knurling team status: needs PR Issue just needs a Pull Request implementing the changes labels Jan 19, 2021
@David-OConnor
Copy link

Same with all chrono, or rtcc types.

@jonas-schievink
Copy link
Contributor

External crates need to implement Format by themselves, like they have to do for serde traits. Implementations for libcore/liballoc types should be added here though.

@jonas-schievink
Copy link
Contributor

This would be easier to implement if we could pad a value with zeroes ({:09} syntax)

@Urhengulas Urhengulas self-assigned this Feb 7, 2021
@Urhengulas
Copy link
Member

I was just wondering if it is possible to apply own macros to foreign types, similar to how it is possible to impl own traits on foreign types?
This would be very handy to easily implement Format for the standart types.

@Urhengulas
Copy link
Member

Also it doesn't seem to be possible to implement it like following for Duration. Can someone quickly explain why not?

// src/impls.rs
impl Format for core::time::Duration {
    fn format(&self, fmt: Formatter) {
        crate::write!(
            fmt,
            "Duration {{ {=u64} {=u32} }}",
            self.as_secs(),
            self.subsec_nanos(),
        )
    }
}

// Error: failed to resolve: could not find `defmt` in `{{root}}` 

@jonas-schievink
Copy link
Contributor

failed to resolve: could not find defmt in {{root}}

This is likely because the macros expect the invoking crate to depend on defmt, and thus have that name available. You should be able to use crate as defmt; in the crate root to fix this.

@Urhengulas
Copy link
Member

@jonas-schievink I've tried that, but still doesn't work. I've also tried to replace crate::write! with defmt_macros::write!, but still didn't work.

May you have a look here? https://github.com/knurling-rs/defmt/blob/348-format-for-duration/src/impls.rs#L462-L471

@Urhengulas
Copy link
Member

Okay, I fixed it for my case, but I am unsure if this breaks other stuff. Let's see in the reviews 😺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: easy Pretty easy to solve good first issue Good for newcomers priority: medium Medium priority for the Knurling team status: needs PR Issue just needs a Pull Request implementing the changes type: enhancement Enhancement or feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants