Skip to content

Splittable trait #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions embedded-io-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,57 @@ impl<T: ?Sized + Seek> Seek for &mut T {
T::seek(self, pos).await
}
}

/// A trait for bidirectional communication interfaces that can be split into
/// separate read and write halves.
///
/// This trait is useful for scenarios where you want to handle reading and
/// writing operations independently, possibly in different tasks or threads.
///
/// # Associated Types
///
/// - `ReadHalf`: The type of the read half, which must implement the `Read` trait.
/// - `WriteHalf`: The type of the write half, which must implement the `Write` trait.
///
/// # Required Methods
///
/// ## `split`
///
/// Splits the bidirectional interface into separate read and write halves.
///
/// ```rust
/// fn split(self) -> (Self::ReadHalf, Self::WriteHalf);
/// ```
///
/// # Examples
///
/// ```rust
/// use embedded_io_async::Splittable;
///
/// async fn use_split_interface<T>(interface: T)
/// where
/// T: Splittable,
/// {
/// let (read_half, write_half) = interface.split();
///
/// // Use `read_half` and `write_half` independently.
/// }
/// ```
///
/// # Notes
///
/// - Implementors of this trait must ensure that the split operation correctly
/// separates the read and write functionalities without interfering with each other.
/// - The `split` method consumes the original interface, transferring ownership
/// of the read and write halves to the caller.

pub trait Splittable: Read + Write {
/// Type representing the read half
type ReadHalf: Read<Error = Self::Error>;
/// Type representing the write half
type WriteHalf: Write<Error = Self::Error>;

/// Splits the bidirectional interface into separate unidirectional read and write halves.
fn split(self) -> (Self::ReadHalf, Self::WriteHalf);
}

Loading