From b8d79e5fc789dc54032c501f913a75355e6015e7 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 17 Feb 2021 09:50:10 +0100 Subject: [PATCH] add EncoderOutput::to_borrowed() may more "explicit" than using `.as_mut().into()`, but otherwise just seems like a useful addition to show the lifetime constraints on this Signed-off-by: Wolfgang Bumiller --- src/encoder/mod.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 428a5c5..92e79c2 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -221,10 +221,20 @@ pub(crate) enum EncoderOutput<'a, T> { Borrowed(&'a mut T), } +impl<'a, T> EncoderOutput<'a, T> { + #[inline] + fn to_borrowed<'s>(&'s mut self) -> EncoderOutput<'s, T> + where + 'a: 's, + { + EncoderOutput::Borrowed(self.as_mut()) + } +} + impl<'a, T> std::convert::AsMut for EncoderOutput<'a, T> { fn as_mut(&mut self) -> &mut T { match self { - EncoderOutput::Owned(ref mut o) => o, + EncoderOutput::Owned(o) => o, EncoderOutput::Borrowed(b) => b, } } @@ -273,7 +283,7 @@ impl<'a, T: SeqWrite + 'a> Drop for EncoderImpl<'a, T> { } impl<'a, T: SeqWrite + 'a> EncoderImpl<'a, T> { - pub(crate) async fn new(output: EncoderOutput<'a, T>, metadata: &Metadata) -> io::Result> { + pub async fn new(output: EncoderOutput<'a, T>, metadata: &Metadata) -> io::Result> { if !metadata.is_dir() { io_bail!("directory metadata must contain the directory mode flag"); } @@ -536,7 +546,7 @@ impl<'a, T: SeqWrite + 'a> EncoderImpl<'a, T> { Ok(EncoderImpl { // always forward as Borrowed(), to avoid stacking references on nested calls - output: self.output.as_mut().into(), + output: self.output.to_borrowed(), state: EncoderState { entry_offset, files_offset,