added DualStream AsRef and AsMut impls for dynamic dispatching to AsyncWrite

TODO: Maybe add impl Borrow for the same thing
no-dual
Avril 4 years ago
parent 1eb8012240
commit 589825fcaf
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -27,7 +27,24 @@ pub enum DualStream<S>
Plain(S), Plain(S),
} }
//pub type Error = openssl::error::ErrorStack; // 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.)
impl<'a, S: AsyncWrite> AsRef<dyn AsyncWrite + 'a> for DualStream<S>
where S: 'a
{
fn as_ref(&self) -> &(dyn AsyncWrite + 'a)
{
self.as_dyn()
}
}
impl<'a, S: AsyncWrite> AsMut<dyn AsyncWrite + 'a> for DualStream<S>
where S: 'a
{
fn as_mut(&mut self) -> &mut (dyn AsyncWrite + 'a)
{
self.as_dyn_mut()
}
}
impl<'a, S: AsyncWrite + Unpin> DualStream<S> impl<'a, S: AsyncWrite + Unpin> DualStream<S>
where S: 'a where S: 'a
@ -72,6 +89,31 @@ where S: 'a
Ok(()) Ok(())
} }
} }
impl<'a, S: AsyncWrite> DualStream<S>
where S: Unpin + 'a
{
/// As an immutable dynamic object for `Unpin` streams.
pub fn as_dyn_unpin(&self) -> &(dyn AsyncWrite + Unpin + 'a)
{
match self {
Self::Plain(p) => p,
Self::Encrypted(e) => e,
_ => panic!("Poisoned")
}
}
/// As a mutable dynamic object for `Unpin` streams
pub fn as_dyn_unpin_mut(&mut self) -> &mut (dyn AsyncWrite + Unpin + 'a)
{
match self {
Self::Plain(p) => p,
Self::Encrypted(e) => e,
_ => panic!("Poisoned")
}
}
}
impl<'a, S: AsyncWrite> DualStream<S> impl<'a, S: AsyncWrite> DualStream<S>
where S: 'a where S: 'a
{ {
@ -198,18 +240,18 @@ where S: 'a
} }
} }
/// As an immutable dynamic object /// Consume into the inner (plain) stream
pub fn as_dyn(&self) -> &(dyn AsyncWrite + 'a) #[inline] pub fn into_inner(self) -> S
{ {
match self { match self {
Self::Plain(p) => p, Self::Plain(p) => p,
Self::Encrypted(e) => e, Self::Encrypted(e) => e.into_inner(),
_ => panic!("Poisoned") _ => panic!("Poisoned")
} }
} }
/// As a mutable dynamic object /// As an immutable dynamic object
pub fn as_dyn_mut(&mut self) -> &mut (dyn AsyncWrite + 'a) pub fn as_dyn(&self) -> &(dyn AsyncWrite + 'a)
{ {
match self { match self {
Self::Plain(p) => p, Self::Plain(p) => p,
@ -218,12 +260,12 @@ where S: 'a
} }
} }
/// Consume into the inner (plain) stream /// As a mutable dynamic object
#[inline] pub fn into_inner(self) -> S pub fn as_dyn_mut(&mut self) -> &mut (dyn AsyncWrite + 'a)
{ {
match self { match self {
Self::Plain(p) => p, Self::Plain(p) => p,
Self::Encrypted(e) => e.into_inner(), Self::Encrypted(e) => e,
_ => panic!("Poisoned") _ => panic!("Poisoned")
} }
} }

Loading…
Cancel
Save