rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.61k stars 12.74k forks source link

Tracking Issue for `core_io_borrowed_buf` #117693

Open jmillikin opened 1 year ago

jmillikin commented 1 year ago

Feature gate: #![feature(core_io_borrowed_buf)]

This is a tracking issue for an MVP of core::io, which contains an OS-independent subset of std::io.

Public API

The initial API of this module consists of BorrowedBuf and BorrowedCursor, which were previously only available in std.

Steps / History

Unresolved Questions

tgross35 commented 8 months ago

These types in core could use a core::fmt::Write impl, but I am not sure how this is possible without conflicting with the blanket impl for std::io::Write.

taralx commented 8 months ago

Is the intent to stabilize this at the same time as #78485 or independently of it?

kornelski commented 5 months ago

I'm surprised that this doesn't have any safe integration with Vec. I'd like to have vec.as_borrowed_buf() that can safely append to the uninitialized capacity, without having to manually perform unsafe vec.set_len correction afterwards.

keepsimple1 commented 4 months ago

Could you please add a link in the summary to the PR that originally added BorrowedBuf and BorrowedCursor ? (As both this move and the original changes are unstable, it is somewhat difficult to track back the changes).

I believe the link is: https://github.com/rust-lang/rust/pull/97015

keepsimple1 commented 3 months ago

If I understand, BorrowedBuf (like MaybeUninit) essentially is safe to write, may I suggest adding a method to the API of BorrowedBuf:

/// Copies the bytes of `buf` to self starting at `offset`.
///  Returns the number of bytes actually written.
///
/// If `offset` is equal or larger than `self.buf.len()`, returns 0.
/// If `offset` is larger than `self.init`, the region `self.buf[self.init..offset]` is zeroed. 
pub fn write(&mut self, offset: usize, buf: &[u8]) -> usize

(this method updates filled and init accordingly)

The idea is to make the API even easier to use:

What do you think?