1 use crate::park::{Park, Unpark}; 2 3 use std::fmt; 4 use std::time::Duration; 5 6 pub(crate) enum Either<A, B> { 7 A(A), 8 B(B), 9 } 10 11 impl<A, B> Park for Either<A, B> 12 where 13 A: Park, 14 B: Park, 15 { 16 type Unpark = Either<A::Unpark, B::Unpark>; 17 type Error = Either<A::Error, B::Error>; 18 unpark(&self) -> Self::Unpark19 fn unpark(&self) -> Self::Unpark { 20 match self { 21 Either::A(a) => Either::A(a.unpark()), 22 Either::B(b) => Either::B(b.unpark()), 23 } 24 } 25 park(&mut self) -> Result<(), Self::Error>26 fn park(&mut self) -> Result<(), Self::Error> { 27 match self { 28 Either::A(a) => a.park().map_err(Either::A), 29 Either::B(b) => b.park().map_err(Either::B), 30 } 31 } 32 park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error>33 fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> { 34 match self { 35 Either::A(a) => a.park_timeout(duration).map_err(Either::A), 36 Either::B(b) => b.park_timeout(duration).map_err(Either::B), 37 } 38 } 39 } 40 41 impl<A, B> Unpark for Either<A, B> 42 where 43 A: Unpark, 44 B: Unpark, 45 { unpark(&self)46 fn unpark(&self) { 47 match self { 48 Either::A(a) => a.unpark(), 49 Either::B(b) => b.unpark(), 50 } 51 } 52 } 53 54 impl<A, B> fmt::Debug for Either<A, B> 55 where 56 A: fmt::Debug, 57 B: fmt::Debug, 58 { fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result59 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 60 match self { 61 Either::A(a) => a.fmt(fmt), 62 Either::B(b) => b.fmt(fmt), 63 } 64 } 65 } 66