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

Adding io::{Read, Write} traits in libcore? #68315

Closed
gendx opened this issue Jan 17, 2020 · 1 comment
Closed

Adding io::{Read, Write} traits in libcore? #68315

gendx opened this issue Jan 17, 2020 · 1 comment

Comments

@gendx
Copy link

gendx commented Jan 17, 2020

Currently, libcore contains core::fmt::Write but (as far as I understand) nothing of the std::io:: namespace.

However, core::fmt::Write is more constrained than std::io::Write, in that it only allows to write UTF-8 strings, as opposed to slices of bytes in general. It seems to me that std::io::Write as a trait doesn't require any specific OS support (contrary to File, Stdout, etc.), and as such would be useful in libcore and would integrate well there.

For an example where it would be useful in a no_std context, see tock/tock#1449. In this case, one could essentially re-write the io::Write trait, but this seems quite redundant (and wouldn't inter-operate with crates that require a genuine io::Write in their API).

In general, it seems to me that std::io::{Read, Write}, as well as a other utilities such as Cursor or BufReader would have their place in libcore, to be able to use them in no_std contexts. Or is there any technical difficultly that I missed?

@jonas-schievink
Copy link
Contributor

Yes, io::Error doesn't work without liballoc. This has been discussed in the past and while I'd really like to see this happen, a better place to discuss is this RFCs issue: rust-lang/rfcs#2262

bors bot added a commit to tock/tock that referenced this issue Jan 20, 2020
1548: Add kernel::debug::IoWrite trait and remove unsound str::from_utf8_unchecked (fix #1449) r=ppannuto a=gendx

### Pull Request Overview

This pull request adds a new `IoWrite` trait in the kernel, to write arbitrary bytes instead of UTF-8 strings (fixes #1449). Until now, the `flush` function was using `str::from_utf8_unchecked`, even though the bytes are actually not UTF-8 (even with only UTF-8 input, the underlying ring buffer may wrap-around in the middle on a UTF-8 character).

In practice, the underlying implementations work byte-by-byte to send debug output, so a more general trait to write byte slices can be plugged in.

This `IoWrite` trait is similar to `std::io::Write`, except that the latter doesn't exist in `no_std` (because `std::io::Error` isn't compatible - see rust-lang/rust#68315 and rust-lang/rfcs#2262).


### Testing Strategy

This pull request was tested by Travis.


### TODO or Help Wanted

This pull request still needs support for byte-by-byte writing on the arty-e21 board. I'm not even sure the current `debug!` macro is what we want to do in a panic handler.


### Documentation Updated

- [x] Updated the relevant files in `/docs`, or no updates are required.

### Formatting

- [x] Ran `make formatall`.

Co-authored-by: Guillaume Endignoux <[email protected]>
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

2 participants