-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Pack the state and future of unfolds in the same memory (#2283)
* Pack the state and future of unfolds in the same memory * Use the same type for both sink and stream unfolds
- Loading branch information
Showing
4 changed files
with
123 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use core::pin::Pin; | ||
|
||
/// UnfoldState used for stream and sink unfolds | ||
#[derive(Debug)] | ||
pub(crate) enum UnfoldState<T, R> { | ||
Value(T), | ||
Future(/* #[pin] */ R), | ||
Empty, | ||
} | ||
|
||
impl<T, R> UnfoldState<T, R> { | ||
pub(crate) fn project_future(self: Pin<&mut Self>) -> Option<Pin<&mut R>> { | ||
// SAFETY Normal pin projection on the `Future` variant | ||
unsafe { | ||
match self.get_unchecked_mut() { | ||
Self::Future(f) => Some(Pin::new_unchecked(f)), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn take_value(self: Pin<&mut Self>) -> Option<T> { | ||
// SAFETY We only move out of the `Value` variant which is not pinned | ||
match *self { | ||
Self::Value(_) => unsafe { | ||
match core::mem::replace(self.get_unchecked_mut(), UnfoldState::Empty) { | ||
UnfoldState::Value(v) => Some(v), | ||
_ => core::hint::unreachable_unchecked(), | ||
} | ||
}, | ||
_ => None, | ||
} | ||
} | ||
} |