diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-03-26 23:32:12 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-03-26 23:32:12 +0200 |
| commit | 2c45b5c5193adc27d865cab27e1ac000aaae7899 (patch) | |
| tree | e1c41dbb769263a0c14fd4954d7599c08842c9da /embassy-sync/src | |
| parent | 9c7b9b7848da853028902829245887277279b53c (diff) | |
sync/pipe: update to clarify docs that it is byte-oriented.
There was some language copypasted from Channel talking about "messages"
or "values", that is not really accurate with Pipe.
Diffstat (limited to 'embassy-sync/src')
| -rw-r--r-- | embassy-sync/src/pipe.rs | 71 |
1 files changed, 43 insertions, 28 deletions
diff --git a/embassy-sync/src/pipe.rs b/embassy-sync/src/pipe.rs index 1977005fb..ee27cdec8 100644 --- a/embassy-sync/src/pipe.rs +++ b/embassy-sync/src/pipe.rs | |||
| @@ -32,16 +32,16 @@ impl<'p, M, const N: usize> Writer<'p, M, N> | |||
| 32 | where | 32 | where |
| 33 | M: RawMutex, | 33 | M: RawMutex, |
| 34 | { | 34 | { |
| 35 | /// Writes a value. | 35 | /// Write some bytes to the pipe. |
| 36 | /// | 36 | /// |
| 37 | /// See [`Pipe::write()`] | 37 | /// See [`Pipe::write()`] |
| 38 | pub fn write<'a>(&'a self, buf: &'a [u8]) -> WriteFuture<'a, M, N> { | 38 | pub fn write<'a>(&'a self, buf: &'a [u8]) -> WriteFuture<'a, M, N> { |
| 39 | self.pipe.write(buf) | 39 | self.pipe.write(buf) |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | /// Attempt to immediately write a message. | 42 | /// Attempt to immediately write some bytes to the pipe. |
| 43 | /// | 43 | /// |
| 44 | /// See [`Pipe::write()`] | 44 | /// See [`Pipe::try_write()`] |
| 45 | pub fn try_write(&self, buf: &[u8]) -> Result<usize, TryWriteError> { | 45 | pub fn try_write(&self, buf: &[u8]) -> Result<usize, TryWriteError> { |
| 46 | self.pipe.try_write(buf) | 46 | self.pipe.try_write(buf) |
| 47 | } | 47 | } |
| @@ -95,16 +95,16 @@ impl<'p, M, const N: usize> Reader<'p, M, N> | |||
| 95 | where | 95 | where |
| 96 | M: RawMutex, | 96 | M: RawMutex, |
| 97 | { | 97 | { |
| 98 | /// Reads a value. | 98 | /// Read some bytes from the pipe. |
| 99 | /// | 99 | /// |
| 100 | /// See [`Pipe::read()`] | 100 | /// See [`Pipe::read()`] |
| 101 | pub fn read<'a>(&'a self, buf: &'a mut [u8]) -> ReadFuture<'a, M, N> { | 101 | pub fn read<'a>(&'a self, buf: &'a mut [u8]) -> ReadFuture<'a, M, N> { |
| 102 | self.pipe.read(buf) | 102 | self.pipe.read(buf) |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | /// Attempt to immediately read a message. | 105 | /// Attempt to immediately read some bytes from the pipe. |
| 106 | /// | 106 | /// |
| 107 | /// See [`Pipe::read()`] | 107 | /// See [`Pipe::try_read()`] |
| 108 | pub fn try_read(&self, buf: &mut [u8]) -> Result<usize, TryReadError> { | 108 | pub fn try_read(&self, buf: &mut [u8]) -> Result<usize, TryReadError> { |
| 109 | self.pipe.try_read(buf) | 109 | self.pipe.try_read(buf) |
| 110 | } | 110 | } |
| @@ -221,12 +221,11 @@ impl<const N: usize> PipeState<N> { | |||
| 221 | } | 221 | } |
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | /// A bounded pipe for communicating between asynchronous tasks | 224 | /// A bounded byte-oriented pipe for communicating between asynchronous tasks |
| 225 | /// with backpressure. | 225 | /// with backpressure. |
| 226 | /// | 226 | /// |
| 227 | /// The pipe will buffer up to the provided number of messages. Once the | 227 | /// The pipe will buffer up to the provided number of bytes. Once the |
| 228 | /// buffer is full, attempts to `write` new messages will wait until a message is | 228 | /// buffer is full, attempts to `write` new bytes will wait until buffer space is freed up. |
| 229 | /// read from the pipe. | ||
| 230 | /// | 229 | /// |
| 231 | /// All data written will become available in the same order as it was written. | 230 | /// All data written will become available in the same order as it was written. |
| 232 | pub struct Pipe<M, const N: usize> | 231 | pub struct Pipe<M, const N: usize> |
| @@ -277,40 +276,56 @@ where | |||
| 277 | Reader { pipe: self } | 276 | Reader { pipe: self } |
| 278 | } | 277 | } |
| 279 | 278 | ||
| 280 | /// Write a value, waiting until there is capacity. | 279 | /// Write some bytes to the pipe. |
| 280 | /// | ||
| 281 | /// This method writes a nonzero amount of bytes from `buf` into the pipe, and | ||
| 282 | /// returns the amount of bytes written. | ||
| 283 | /// | ||
| 284 | /// If it is not possible to write a nonzero amount of bytes because the pipe's buffer is full, | ||
| 285 | /// this method will wait until it is. See [`try_write`](Self::try_write) for a variant that | ||
| 286 | /// returns an error instead of waiting. | ||
| 281 | /// | 287 | /// |
| 282 | /// Writeing completes when the value has been pushed to the pipe's queue. | 288 | /// It is not guaranteed that all bytes in the buffer are written, even if there's enough |
| 283 | /// This doesn't mean the value has been read yet. | 289 | /// free space in the pipe buffer for all. In other words, it is possible for `write` to return |
| 290 | /// without writing all of `buf` (returning a number less than `buf.len()`) and still leave | ||
| 291 | /// free space in the pipe buffer. You should always `write` in a loop, or use helpers like | ||
| 292 | /// `write_all` from the `embedded-io` crate. | ||
| 284 | pub fn write<'a>(&'a self, buf: &'a [u8]) -> WriteFuture<'a, M, N> { | 293 | pub fn write<'a>(&'a self, buf: &'a [u8]) -> WriteFuture<'a, M, N> { |
| 285 | WriteFuture { pipe: self, buf } | 294 | WriteFuture { pipe: self, buf } |
| 286 | } | 295 | } |
| 287 | 296 | ||
| 288 | /// Attempt to immediately write a message. | 297 | /// Attempt to immediately write some bytes to the pipe. |
| 289 | /// | 298 | /// |
| 290 | /// This method differs from [`write`](Pipe::write) by returning immediately if the pipe's | 299 | /// This method will either write a nonzero amount of bytes to the pipe immediately, |
| 291 | /// buffer is full, instead of waiting. | 300 | /// or return an error if the pipe is empty. See [`write`](Self::write) for a variant |
| 292 | /// | 301 | /// that waits instead of returning an error. |
| 293 | /// # Errors | ||
| 294 | /// | ||
| 295 | /// If the pipe capacity has been reached, i.e., the pipe has `n` | ||
| 296 | /// buffered values where `n` is the argument passed to [`Pipe`], then an | ||
| 297 | /// error is returned. | ||
| 298 | pub fn try_write(&self, buf: &[u8]) -> Result<usize, TryWriteError> { | 302 | pub fn try_write(&self, buf: &[u8]) -> Result<usize, TryWriteError> { |
| 299 | self.lock(|c| c.try_write(buf)) | 303 | self.lock(|c| c.try_write(buf)) |
| 300 | } | 304 | } |
| 301 | 305 | ||
| 302 | /// Receive the next value. | 306 | /// Read some bytes from the pipe. |
| 307 | /// | ||
| 308 | /// This method reads a nonzero amount of bytes from the pipe into `buf` and | ||
| 309 | /// returns the amount of bytes read. | ||
| 310 | /// | ||
| 311 | /// If it is not possible to read a nonzero amount of bytes because the pipe's buffer is empty, | ||
| 312 | /// this method will wait until it is. See [`try_read`](Self::try_read) for a variant that | ||
| 313 | /// returns an error instead of waiting. | ||
| 303 | /// | 314 | /// |
| 304 | /// If there are no messages in the pipe's buffer, this method will | 315 | /// It is not guaranteed that all bytes in the buffer are read, even if there's enough |
| 305 | /// wait until a message is written. | 316 | /// space in `buf` for all. In other words, it is possible for `read` to return |
| 317 | /// without filling `buf` (returning a number less than `buf.len()`) and still leave bytes | ||
| 318 | /// in the pipe buffer. You should always `read` in a loop, or use helpers like | ||
| 319 | /// `read_exact` from the `embedded-io` crate. | ||
| 306 | pub fn read<'a>(&'a self, buf: &'a mut [u8]) -> ReadFuture<'a, M, N> { | 320 | pub fn read<'a>(&'a self, buf: &'a mut [u8]) -> ReadFuture<'a, M, N> { |
| 307 | ReadFuture { pipe: self, buf } | 321 | ReadFuture { pipe: self, buf } |
| 308 | } | 322 | } |
| 309 | 323 | ||
| 310 | /// Attempt to immediately read a message. | 324 | /// Attempt to immediately read some bytes from the pipe. |
| 311 | /// | 325 | /// |
| 312 | /// This method will either read a message from the pipe immediately or return an error | 326 | /// This method will either read a nonzero amount of bytes from the pipe immediately, |
| 313 | /// if the pipe is empty. | 327 | /// or return an error if the pipe is empty. See [`read`](Self::read) for a variant |
| 328 | /// that waits instead of returning an error. | ||
| 314 | pub fn try_read(&self, buf: &mut [u8]) -> Result<usize, TryReadError> { | 329 | pub fn try_read(&self, buf: &mut [u8]) -> Result<usize, TryReadError> { |
| 315 | self.lock(|c| c.try_read(buf)) | 330 | self.lock(|c| c.try_read(buf)) |
| 316 | } | 331 | } |
