Skip to content

Commit 9a65304

Browse files
committed
splittable trait
1 parent 9941a64 commit 9a65304

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

embedded-io-async/src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,57 @@ impl<T: ?Sized + Seek> Seek for &mut T {
205205
T::seek(self, pos).await
206206
}
207207
}
208+
209+
/// A trait for bidirectional communication interfaces that can be split into
210+
/// separate read and write halves.
211+
///
212+
/// This trait is useful for scenarios where you want to handle reading and
213+
/// writing operations independently, possibly in different tasks or threads.
214+
///
215+
/// # Associated Types
216+
///
217+
/// - `ReadHalf`: The type of the read half, which must implement the `Read` trait.
218+
/// - `WriteHalf`: The type of the write half, which must implement the `Write` trait.
219+
///
220+
/// # Required Methods
221+
///
222+
/// ## `split`
223+
///
224+
/// Splits the bidirectional interface into separate read and write halves.
225+
///
226+
/// ```rust
227+
/// fn split(self) -> (Self::ReadHalf, Self::WriteHalf);
228+
/// ```
229+
///
230+
/// # Examples
231+
///
232+
/// ```rust
233+
/// use embedded_io_async::Splittable;
234+
///
235+
/// async fn use_split_interface<T>(interface: T)
236+
/// where
237+
/// T: Splittable,
238+
/// {
239+
/// let (read_half, write_half) = interface.split();
240+
///
241+
/// // Use `read_half` and `write_half` independently.
242+
/// }
243+
/// ```
244+
///
245+
/// # Notes
246+
///
247+
/// - Implementors of this trait must ensure that the split operation correctly
248+
/// separates the read and write functionalities without interfering with each other.
249+
/// - The `split` method consumes the original interface, transferring ownership
250+
/// of the read and write halves to the caller.
251+
252+
pub trait Splittable: Read + Write {
253+
/// Type representing the read half
254+
type ReadHalf: Read<Error = Self::Error>;
255+
/// Type representing the write half
256+
type WriteHalf: Write<Error = Self::Error>;
257+
258+
/// Splits the bidirectional interface into separate unidirectional read and write halves.
259+
fn split(self) -> (Self::ReadHalf, Self::WriteHalf);
260+
}
261+

0 commit comments

Comments
 (0)