From 5d4e59c52c2f8b57a1e7a5cec3c115bfc8bf53d5 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 17 May 2021 14:04:52 +0200 Subject: [PATCH] make the ReadAtImpl enum variants private Signed-off-by: Wolfgang Bumiller --- Cargo.toml | 1 + src/accessor/read_at.rs | 42 ++++++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f586e5d..7e73280 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [package] name = "pxar" +# API broken, next is 0.11.0 (due to `ReadAtImpl` enum variants being made private) version = "0.10.1" authors = ["Wolfgang Bumiller "] edition = "2018" diff --git a/src/accessor/read_at.rs b/src/accessor/read_at.rs index d49d080..3c82f70 100644 --- a/src/accessor/read_at.rs +++ b/src/accessor/read_at.rs @@ -47,45 +47,65 @@ pub trait ReadAtExt: ReadAt { where Self: Sized, { - ReadAtImpl::New(self, buf, offset) + ReadAtImpl::new(self, buf, offset) } } impl ReadAtExt for T {} -pub enum ReadAtImpl<'a, T: ReadAt> { +/// Future returned by [`ReadAtExt::read_at`](ReadAtExt::read_at()). +#[repr(transparent)] +pub struct ReadAtImpl<'a, T: ReadAt> { + state: ReadAtState<'a, T>, +} + +impl<'a, T: ReadAt> ReadAtImpl<'a, T> { + fn new(read: &'a T, buf: &'a mut [u8], offset: u64) -> Self { + Self { + state: ReadAtState::New(read, buf, offset), + } + } +} + +impl<'a, T: ReadAt> Future for ReadAtImpl<'a, T> { + type Output = io::Result; + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { + unsafe { self.map_unchecked_mut(|this| &mut this.state).poll(cx) } + } +} + +enum ReadAtState<'a, T: ReadAt> { Invalid, New(&'a T, &'a mut [u8], u64), Pending(&'a T, ReadAtOperation<'a>), - Ready(io::Result), } -impl ReadAtImpl<'_, T> { +impl ReadAtState<'_, T> { fn take(&mut self) -> Self { - std::mem::replace(self, ReadAtImpl::Invalid) + std::mem::replace(self, ReadAtState::Invalid) } } -impl<'a, T: ReadAt> Future for ReadAtImpl<'a, T> { +impl<'a, T: ReadAt> Future for ReadAtState<'a, T> { type Output = io::Result; fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { let this = unsafe { Pin::into_inner_unchecked(self) }; loop { match match this.take() { - ReadAtImpl::New(reader, buf, offset) => { + ReadAtState::New(reader, buf, offset) => { let pin = unsafe { Pin::new_unchecked(reader) }; (pin.start_read_at(cx, buf, offset), reader) } - ReadAtImpl::Pending(reader, op) => { + ReadAtState::Pending(reader, op) => { let pin = unsafe { Pin::new_unchecked(reader) }; (pin.poll_complete(op), reader) } - ReadAtImpl::Ready(out) => return Poll::Ready(out), - ReadAtImpl::Invalid => panic!("poll after ready"), + ReadAtState::Invalid => panic!("poll after ready"), } { (MaybeReady::Ready(out), _reader) => return Poll::Ready(out), - (MaybeReady::Pending(op), reader) => *this = ReadAtImpl::Pending(reader, op), + (MaybeReady::Pending(op), reader) => *this = ReadAtState::Pending(reader, op), } } } -- 2.39.5