added AsRef/Mut impls for 'dyn AsyncWrite + Unpin' when 'S' is 'Unpin' for convenience

no-dual
Avril 4 years ago
parent 589825fcaf
commit e657b72bcf
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -17,17 +17,47 @@ use std::{
bool_type!(pub Encrypted; "Is the value encrypted?");
bool_type!(pub Encryption; "What way are we en/decrypting?" => Encrypt, Decrypt);
/// A wrapper `AsyncWrite` stream that allows switching between encrypted (chacha20stream) and plain stream writing.
///
/// # Polymorphic dispatching
/// While this type implements `AsyncWrite` itself, it is recommended to use the polymorphic mutable reference returned by `as_dyn_unpin_mut()` (or `as_dyn_mut()` for non-`Unpin` values of `S`) for writing if lots of `AsyncWrite` methods will be dispatched on the instance.
/// This will prevent the need to check the discriminant of the enum each time the `DualStream`'s `AsyncWrite` methods are polled.
/// The type implements `AsRef/Mut` for streams `S` that are both `Unpin` and not `Unpin` for convenience.
#[derive(Debug)]
pub enum DualStream<S>
pub enum DualStream<S: AsyncWrite>
{
/// If there is a panic while switching modes, the stream is left in this invariant state.
///
/// Stream `S` is dropped if the instance is in this state.
Poisoned,
/// Stream `S` is being written to through a chacha20stream `AsyncSink` stream cipher.
Encrypted(AsyncSink<S>),
/// Stream `S` is being written to directly.
Plain(S),
}
// We can use dynamic dispatching to prevent the need to check the enum's discriminant each time. (In theory. It might not be that useful unless we add `Unpin` here too. TODO: Check this later.)
// We can use dynamic dispatching to prevent the need to check the enum's discriminant each time.
// We have `AsRef/Mut`s for normal and `Unpin` polymorphic `AsyncWrite`s
impl<'a, S: AsyncWrite + Unpin> AsRef<dyn AsyncWrite + Unpin + 'a> for DualStream<S>
where S: 'a
{
fn as_ref(&self) -> &(dyn AsyncWrite + Unpin + 'a)
{
self.as_dyn_unpin()
}
}
impl<'a, S: AsyncWrite + Unpin> AsMut<dyn AsyncWrite + Unpin + 'a> for DualStream<S>
where S: 'a
{
fn as_mut(&mut self) -> &mut (dyn AsyncWrite + Unpin + 'a)
{
self.as_dyn_unpin_mut()
}
}
impl<'a, S: AsyncWrite> AsRef<dyn AsyncWrite + 'a> for DualStream<S>
where S: 'a
{

Loading…
Cancel
Save