Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Write without providing a buffer

Aperta
#746 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

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?

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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-embedded/embedded-hal

Tutte le issue di rust-embedded/embedded-hal

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.