diff options
Diffstat (limited to 'third_party/rust/futures-util/src/unfold_state.rs')
-rw-r--r-- | third_party/rust/futures-util/src/unfold_state.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/third_party/rust/futures-util/src/unfold_state.rs b/third_party/rust/futures-util/src/unfold_state.rs new file mode 100644 index 0000000000..0edc15e437 --- /dev/null +++ b/third_party/rust/futures-util/src/unfold_state.rs @@ -0,0 +1,39 @@ +use core::pin::Pin; + +use pin_project_lite::pin_project; + +pin_project! { + /// UnfoldState used for stream and sink unfolds + #[project = UnfoldStateProj] + #[project_replace = UnfoldStateProjReplace] + #[derive(Debug)] + pub(crate) enum UnfoldState<T, R> { + Value { + value: T, + }, + Future { + #[pin] + future: R, + }, + Empty, + } +} + +impl<T, R> UnfoldState<T, R> { + pub(crate) fn project_future(self: Pin<&mut Self>) -> Option<Pin<&mut R>> { + match self.project() { + UnfoldStateProj::Future { future } => Some(future), + _ => None, + } + } + + pub(crate) fn take_value(self: Pin<&mut Self>) -> Option<T> { + match &*self { + UnfoldState::Value { .. } => match self.project_replace(UnfoldState::Empty) { + UnfoldStateProjReplace::Value { value } => Some(value), + _ => unreachable!(), + }, + _ => None, + } + } +} |