| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use crate::*; |
| 6 |
use ::either::Either; |
| 7 |
|
| 8 |
/// A [`Writeable`] impl that delegates to one type or another type. |
| 9 |
impl<W0, W1> Writeable for Either<W0, W1> |
| 10 |
where |
| 11 |
W0: Writeable, |
| 12 |
W1: Writeable, |
| 13 |
{ |
| 14 |
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { |
| 15 |
match self { |
| 16 |
Either::Left(w) => w.write_to(sink), |
| 17 |
Either::Right(w) => w.write_to(sink), |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result { |
| 22 |
match self { |
| 23 |
Either::Left(w) => w.write_to_parts(sink), |
| 24 |
Either::Right(w) => w.write_to_parts(sink), |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
fn writeable_length_hint(&self) -> LengthHint { |
| 29 |
match self { |
| 30 |
Either::Left(w) => w.writeable_length_hint(), |
| 31 |
Either::Right(w) => w.writeable_length_hint(), |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
fn writeable_borrow(&self) -> Option<&str> { |
| 36 |
match self { |
| 37 |
Either::Left(w) => w.writeable_borrow(), |
| 38 |
Either::Right(w) => w.writeable_borrow(), |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[cfg(feature = "alloc")] |
| 43 |
fn write_to_string(&self) -> Cow<'_, str> { |
| 44 |
match self { |
| 45 |
Either::Left(w) => w.write_to_string(), |
| 46 |
Either::Right(w) => w.write_to_string(), |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/// A [`TryWriteable`] impl that delegates to one type or another type. |
| 52 |
impl<W0, W1> TryWriteable for Either<W0, W1> |
| 53 |
where |
| 54 |
W0: TryWriteable, |
| 55 |
W1: TryWriteable, |
| 56 |
{ |
| 57 |
type Error = Either<W0::Error, W1::Error>; |
| 58 |
|
| 59 |
fn try_write_to<W: fmt::Write + ?Sized>( |
| 60 |
&self, |
| 61 |
sink: &mut W, |
| 62 |
) -> Result<Result<(), Self::Error>, fmt::Error> { |
| 63 |
match self { |
| 64 |
Either::Left(w) => w.try_write_to(sink).map(|r| r.map_err(Either::Left)), |
| 65 |
Either::Right(w) => w.try_write_to(sink).map(|r| r.map_err(Either::Right)), |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
fn try_write_to_parts<S: PartsWrite + ?Sized>( |
| 70 |
&self, |
| 71 |
sink: &mut S, |
| 72 |
) -> Result<Result<(), Self::Error>, fmt::Error> { |
| 73 |
match self { |
| 74 |
Either::Left(w) => w.try_write_to_parts(sink).map(|r| r.map_err(Either::Left)), |
| 75 |
Either::Right(w) => w.try_write_to_parts(sink).map(|r| r.map_err(Either::Right)), |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
fn writeable_length_hint(&self) -> LengthHint { |
| 80 |
match self { |
| 81 |
Either::Left(w) => w.writeable_length_hint(), |
| 82 |
Either::Right(w) => w.writeable_length_hint(), |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
#[cfg(feature = "alloc")] |
| 87 |
fn try_write_to_string(&self) -> Result<Cow<'_, str>, (Self::Error, Cow<'_, str>)> { |
| 88 |
match self { |
| 89 |
Either::Left(w) => w |
| 90 |
.try_write_to_string() |
| 91 |
.map_err(|(e, s)| (Either::Left(e), s)), |
| 92 |
Either::Right(w) => w |
| 93 |
.try_write_to_string() |
| 94 |
.map_err(|(e, s)| (Either::Right(e), s)), |
| 95 |
} |
| 96 |
} |
| 97 |
} |