Rust pro-tip: If you read to a buffer, and read a variable number of items, don't return Ok(count)! Instead return the valid subslice instead! not: fn read(&mut buf) -> Result<usize>; instead: fn read(buf: &mut [u8]) -> Result<&mut [u8]>;
-
-
Hvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
-
-
this reminds me of my early wish/misconception for "mutable slice" to mean that the slice values (offset, length) were changeable (bounded by the parent), rather than the underlying data. Useful for a sliding window into a buffer, like this. Is there a crate for that?
-
I tend to use `split_at_mut` for stuff like this. Let me put together an example real quick
- Još 5 drugih odgovora
Novi razgovor -
-
-
it makes sense in this example, but it isn't as general i feel. what if the caller wants the *remainder* of the buffer? that would be much cumbersome this way
-
Fair! I have other places where I return the "remainder" as well, but often (in bare metal embedded), we only care about the "valid" part of the buffer, like receiving a packet from a driver. Feel free to do whatever works for you :)
Kraj razgovora
Novi razgovor -
-
Tweet je nedostupan.
-
-
Very nice tip! I've never thought about it! Thanks!
Hvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
Čini se da učitavanje traje već neko vrijeme.
Twitter je možda preopterećen ili ima kratkotrajnih poteškoća u radu. Pokušajte ponovno ili potražite dodatne informacije u odjeljku Status Twittera.
