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),
}
//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>
where S: 'a
@ -72,6 +89,31 @@ where S: 'a
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>
where S: 'a
{
@ -198,18 +240,18 @@ where S: 'a
}
}
/// As an immutable dynamic object
pub fn as_dyn(&self) -> &(dyn AsyncWrite + 'a)
/// Consume into the inner (plain) stream
#[inline] pub fn into_inner(self) -> S
{
match self {
Self::Plain(p) => p,
Self::Encrypted(e) => e,
Self::Encrypted(e) => e.into_inner(),
_ => panic!("Poisoned")
}
}
/// As a mutable dynamic object
pub fn as_dyn_mut(&mut self) -> &mut (dyn AsyncWrite + 'a)
/// As an immutable dynamic object
pub fn as_dyn(&self) -> &(dyn AsyncWrite + 'a)
{
match self {
Self::Plain(p) => p,
@ -218,12 +260,12 @@ where S: 'a
}
}
/// Consume into the inner (plain) stream
#[inline] pub fn into_inner(self) -> S
/// As a mutable dynamic object
pub fn as_dyn_mut(&mut self) -> &mut (dyn AsyncWrite + 'a)
{
match self {
Self::Plain(p) => p,
Self::Encrypted(e) => e.into_inner(),
Self::Encrypted(e) => e,
_ => panic!("Poisoned")
}
}

Loading…
Cancel
Save