Write without providing a buffer
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Da chiarire
- Stato di attività
- Tranquilla
- Stack tecnologico
- rust
- Ambito
- api, embedded-iot
Direzione di ricerca
Start by reading the embedded_io_async Write trait and compare the referenced picoserve, embassy_net::tcp::TcpSocket::write_with, and smoltcp Device designs. Check the BorrowedCursor and BorrowedBuf stabilization discussions linked in the issue. Done means an agreed opt-in API shape, including cancellation and discarded-data semantics, with corresponding trait tests.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Hello, currently Write in embedded_io_async forces the user of the API to provide a &[u8] which is processed by the trait implementer. But often you want it the other way around: the implementer has a buffer lying around, and the user would like a buffer to write into.
I have already met with cases where crates started creating subtraits, or just diverging from embedded_io_async, to get this. For example, picoserve replaced Write with their own trait, which allowed to shrink endpoint sizes (no temporary buffers, which can also get duplicated due to bad async fn codegen). The migration was easy for us: our tcp sockets are working on top of a packet pool, so instead of getting a slice and copying into the packet, we just give the mutable slice to picoserve. embassy_net::tcp::TcpSocket::write_with is basically this too. Also, smoltcp has a similar design where it is up to Device implementer to provide the buffer.
BorrowedCursor and similar are probably getting stabilized soon (FCP is now), maybe we can think together of an additional opt-in api which can use them?
- https://github.com/rust-lang/rust/issues/154046#issuecomment-5173622285
- https://github.com/rust-lang/rust/issues/117693#issuecomment-5106792214
For std is it may be okay to pass &[u8] everywhere because there is plenty of memory, but on no_std there might not even be an allocator, in order to prevent fragmentation and OOMs.
I suggest adding this to the trait, but I don't know what the best api shape is.
trait Write {
async fn write(&mut self, data: &[u8]) -> Result<usize, Self::Error> {
self.write_with_cursor(|mut cursor| {
let amt = cmp::min(cursor.capacity(), data.len());
cursor.append(&data[..amt]);
Ok(amt)
})
.await
}
async fn write_with_cursor<R>(
&mut self,
writer: impl FnOnce(BorrowedCursor<'_, u8>) -> Result<R, Self::Error>,
) -> Result<R, Self::Error> {
let mut data = [0u8; 256];
let mut buf = BorrowedBuf::from(data.as_mut());
let res = writer(buf.unfilled())?;
self.write_all(buf.filled()).await?;
Ok(res)
}
}
The closure returns Result in case user wants to cancel the write in the middle. Whether should it discard the data the user has already written or no I don't know, for the blank impl I assumed they're discarded. In case user won't ever return Err, monomorphisation will optimize the branch away.
Also, doesn't it look kind of cool? 🤭
- Lingua principale
- Rust
- Stelle
- 2.7k
- Fork
- 283
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di rust-embedded/embedded-hal
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
rust-embedded/embedded-hal#742 · 1 commento ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 30/100
rust-embedded/embedded-hal#747 · 5 commenti · 1 reazione ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
rust-embedded/embedded-hal#745 · 1 reazione ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 55/100
rust-embedded/embedded-hal#744 · 1 commento ·
-
CAN: returning `Result` instead of `Option`Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
rust-embedded/embedded-hal#741 · 2 reazioni ·
Tutte le issue di rust-embedded/embedded-hal
Issue simili
-
RUSTSEC-2026-0348: Brotli certificate decompression can allocate up to 1 GiB before authenticationAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 60/100
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 62/100
-
core: Amount::from_str panics on values above 21M BTC instead of returning an errorForse già presa @bhuvan-somisetty l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
crate: spate-s3
Difficoltà 2/5 Mezza giornata Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
write_iceberg ignores the table's write.parquet.compression-codec propertyForse già presa @YuangGao l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
Eventual-Inc/Daft#7606 ·
I maintainer di solito rispondono entro 1 giorno