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

uwrite with alloc::string::String? no_std target #31

Open
dragonnn opened this issue Mar 9, 2021 · 5 comments
Open

uwrite with alloc::string::String? no_std target #31

dragonnn opened this issue Mar 9, 2021 · 5 comments

Comments

@dragonnn
Copy link

dragonnn commented Mar 9, 2021

Hi!
I am trying to craft a environment for my embedded board.
Trying to use ufmt uwrite! with alloc::string::String but getting such errors:

error[E0599]: the method `do_as_formatter` exists for struct `String`, but its trait bounds were not satisfied
   --> src/main.rs:24:9
    |
24  |         ufmt::uwrite!(s, "hello from rust: {}!\n", i);
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `String` due to unsatisfied trait bounds
    | 
   ::: /home/mateusz/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:278:1
    |
278 | pub struct String {
    | -----------------
    | |
    | doesn't satisfy `String: UnstableDoAsFormatter`
    | doesn't satisfy `String: uWrite`
    |
    = note: the following trait bounds were not satisfied:
            `String: uWrite`
            which is required by `String: UnstableDoAsFormatter`
            `str: uWrite`
            which is required by `str: UnstableDoAsFormatter`
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

I already have a working allocator since the underlying system is based on nuttx and I just use libc_alloc with uses malloc and other options from directly nuttx libc.
I would be really grateful for any help, I am stuck with that currently

@benma
Copy link

benma commented Sep 9, 2021

I have the exact same question / problem.

@bjorn3
Copy link

bjorn3 commented Sep 9, 2021

ufmt/write/src/lib.rs

Lines 40 to 48 in 4cbb956

#[cfg(feature = "std")]
impl uWrite for String {
type Error = Infallible;
fn write_str(&mut self, s: &str) -> Result<(), Infallible> {
self.push_str(s);
Ok(())
}
}

looks like uWrite is only implemented for String when using std.

@benma
Copy link

benma commented Sep 9, 2021

It would be good to add a alloc feature and use alloc::string::String instead, making this work better in no_std environments.

@benma
Copy link

benma commented Sep 9, 2021

As a workaround one can make a newtype struct UString(String) and implement uWrite on it, something along these lines:

    pub struct UString(pub String);
    impl ufmt::uWrite for UString {
        type Error = core::convert::Infallible;

        fn write_str(&mut self, s: &str) -> Result<(), core::convert::Infallible> {
            self.0.push_str(s);
            Ok(())
        }
    }
    impl ufmt::uDisplay for UString {
        fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
        where
            W: ufmt::uWrite + ?Sized,
        {
            <str as ufmt::uDisplay>::fmt(&self.0, f)
        }
    }

@dragonnn
Copy link
Author

dragonnn commented Sep 9, 2021

It would be good to add a alloc feature and use alloc::string::String instead, making this work better in no_std environments.

Yup! That would be perfect

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

No branches or pull requests

3 participants