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

Object safe Format #861

Open
EdBuilds opened this issue Aug 18, 2024 · 1 comment
Open

Object safe Format #861

EdBuilds opened this issue Aug 18, 2024 · 1 comment
Labels
breaking change fix / feature / improvement involves a breaking change and needs to wait until next minor version type: enhancement Enhancement or feature request

Comments

@EdBuilds
Copy link

Hello,
Would it be possible to make the Format trait object safe? I would like to pass messages around by using

&dyn defmt::Format 

But the static function in the trait prevents that.
Cheers!

@Urhengulas Urhengulas added type: enhancement Enhancement or feature request breaking change fix / feature / improvement involves a breaking change and needs to wait until next minor version labels Aug 27, 2024
@jonathanpallant
Copy link
Contributor

I looked into this for you.

The serialiser needs to report what kind of thing is being serialised so the deserialiser can unpack it. This is done using an interned string:

pub fn fmt<T: Format + ?Sized>(f: &T) {
    istr(&T::_format_tag());
    f._format_data();
}

pub fn fmt_slice<T: Format>(values: &[T]) {
    usize(&values.len());
    istr(&T::_format_tag());
    for value in values {
        value._format_data();
    }
}

If _format_tag() takes &self, then you can't call T::_format_tag() when serialising a slice, and we can't guarantee that the slice has non-zero length, so we can't call values[0]._format_tag() either. So the trait has to include a static method, and that means we can't have dynamic trait objects.

So with the current design, no, it's not possible to have a &dyn defmt::Format. Can you elaborate on why you would like to make &dyn references to things that can be formatted with defmt? What would you wish to do with those reference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change fix / feature / improvement involves a breaking change and needs to wait until next minor version type: enhancement Enhancement or feature request
Projects
None yet
Development

No branches or pull requests

3 participants