forked from thk1/send_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
feat!: sync changes from compio
#1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
258cbbf
feat!: sync changes from `compio`
Berrysoft df34e4d
docs: update README and dep info
Berrysoft 7ab8140
feat: update to edition 2024
Berrysoft df9f6e7
ci: enable for PRs
Berrysoft e3f665f
ci: use nightly rust
Berrysoft 8478aa3
ci: fix condition
Berrysoft f94c169
fix: take
Berrysoft 166126b
fix: apply suggestions from code review
Berrysoft 2c34617
fix: tests
Berrysoft c49333e
feat: change `take` back
Berrysoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /target/ | ||
| **/*.rs.bk | ||
| Cargo.lock | ||
| .vscode/ |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1 +1,19 @@ | ||
| hard_tabs = true | ||
| unstable_features = true | ||
|
|
||
| style_edition = "2024" | ||
Berrysoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| group_imports = "StdExternalCrate" | ||
| imports_granularity = "Crate" | ||
| reorder_imports = true | ||
|
|
||
| wrap_comments = true | ||
| normalize_comments = true | ||
|
|
||
| reorder_impl_items = true | ||
| condense_wildcard_suffixes = true | ||
| enum_discrim_align_threshold = 20 | ||
| use_field_init_shorthand = true | ||
|
|
||
| format_strings = true | ||
| format_code_in_doc_comments = true | ||
| format_macro_matchers = true | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,99 +1,92 @@ | ||
| //! [`Future`] and [`Stream`] support for [`SendWrapper`]. | ||
| use std::{ | ||
| future::Future, | ||
| ops::{Deref as _, DerefMut as _}, | ||
| pin::Pin, | ||
| task, | ||
| }; | ||
| use std::{future::Future, pin::Pin, task}; | ||
|
|
||
| use futures_core::Stream; | ||
|
|
||
| use crate::SendWrapper; | ||
| use crate::{SendWrapper, invalid_deref, invalid_poll}; | ||
|
|
||
| impl<F: Future> Future for SendWrapper<F> { | ||
| type Output = F::Output; | ||
| type Output = F::Output; | ||
|
|
||
| /// Polls this [`SendWrapper`] [`Future`]. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Polling panics if it is done from a different thread than the one the [`SendWrapper`] | ||
| /// instance has been created with. | ||
| #[track_caller] | ||
| fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> { | ||
| self.assert_valid_for_poll(); | ||
| // This is safe as `SendWrapper` itself points to the inner `Future`. | ||
| // So, as long as `SendWrapper` is pinned, the inner `Future` is pinned too. | ||
| unsafe { self.map_unchecked_mut(Self::deref_mut) }.poll(cx) | ||
| } | ||
| /// Polls this [`SendWrapper`] [`Future`]. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Polling panics if it is done from a different thread than the one the | ||
| /// [`SendWrapper`] instance has been created with. | ||
| #[track_caller] | ||
| fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> { | ||
| self.get_pinned_mut() | ||
| .unwrap_or_else(|| invalid_poll()) | ||
| .poll(cx) | ||
| } | ||
| } | ||
|
|
||
| impl<S: Stream> Stream for SendWrapper<S> { | ||
| type Item = S::Item; | ||
| type Item = S::Item; | ||
|
|
||
| /// Polls this [`SendWrapper`] [`Stream`]. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Polling panics if it is done from a different thread than the one the [`SendWrapper`] | ||
| /// instance has been created with. | ||
| #[track_caller] | ||
| fn poll_next( | ||
| self: Pin<&mut Self>, | ||
| cx: &mut task::Context<'_>, | ||
| ) -> task::Poll<Option<Self::Item>> { | ||
| self.assert_valid_for_poll(); | ||
| // This is safe as `SendWrapper` itself points to the inner `Stream`. | ||
| // So, as long as `SendWrapper` is pinned, the inner `Stream` is pinned too. | ||
| unsafe { self.map_unchecked_mut(Self::deref_mut) }.poll_next(cx) | ||
| } | ||
| /// Polls this [`SendWrapper`] [`Stream`]. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Polling panics if it is done from a different thread than the one the | ||
| /// [`SendWrapper`] instance has been created with. | ||
| #[track_caller] | ||
| fn poll_next( | ||
| self: Pin<&mut Self>, | ||
| cx: &mut task::Context<'_>, | ||
| ) -> task::Poll<Option<Self::Item>> { | ||
| self.get_pinned_mut() | ||
| .unwrap_or_else(|| invalid_poll()) | ||
| .poll_next(cx) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| self.deref().size_hint() | ||
| } | ||
| #[inline] | ||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| self.get().unwrap_or_else(|| invalid_deref()).size_hint() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::thread; | ||
| use std::thread; | ||
|
|
||
| use futures_executor as executor; | ||
| use futures_util::{future, stream, StreamExt}; | ||
| use futures_executor as executor; | ||
| use futures_util::{StreamExt, future, stream}; | ||
|
|
||
| use crate::SendWrapper; | ||
| use crate::SendWrapper; | ||
|
|
||
| #[test] | ||
| fn test_future() { | ||
| let w1 = SendWrapper::new(future::ready(42)); | ||
| let w2 = w1.clone(); | ||
| assert_eq!( | ||
| format!("{:?}", executor::block_on(w1)), | ||
| format!("{:?}", executor::block_on(w2)), | ||
| ); | ||
| } | ||
| #[test] | ||
| fn test_future() { | ||
| let w1 = SendWrapper::new(future::ready(42)); | ||
| let w2 = w1.clone(); | ||
| assert_eq!( | ||
| format!("{:?}", executor::block_on(w1)), | ||
| format!("{:?}", executor::block_on(w2)), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_future_panic() { | ||
| let w = SendWrapper::new(future::ready(42)); | ||
| let t = thread::spawn(move || executor::block_on(w)); | ||
| assert!(t.join().is_err()); | ||
| } | ||
| #[test] | ||
| fn test_future_panic() { | ||
| let w = SendWrapper::new(future::ready(42)); | ||
| let t = thread::spawn(move || executor::block_on(w)); | ||
| assert!(t.join().is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_stream() { | ||
| let mut w1 = SendWrapper::new(stream::once(future::ready(42))); | ||
| let mut w2 = SendWrapper::new(stream::once(future::ready(42))); | ||
| assert_eq!( | ||
| format!("{:?}", executor::block_on(w1.next())), | ||
| format!("{:?}", executor::block_on(w2.next())), | ||
| ); | ||
| } | ||
| #[test] | ||
| fn test_stream() { | ||
| let mut w1 = SendWrapper::new(stream::once(future::ready(42))); | ||
| let mut w2 = SendWrapper::new(stream::once(future::ready(42))); | ||
| assert_eq!( | ||
| format!("{:?}", executor::block_on(w1.next())), | ||
| format!("{:?}", executor::block_on(w2.next())), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_stream_panic() { | ||
| let mut w = SendWrapper::new(stream::once(future::ready(42))); | ||
| let t = thread::spawn(move || executor::block_on(w.next())); | ||
| assert!(t.join().is_err()); | ||
| } | ||
| #[test] | ||
| fn test_stream_panic() { | ||
| let mut w = SendWrapper::new(stream::once(future::ready(42))); | ||
| let t = thread::spawn(move || executor::block_on(w.next())); | ||
| assert!(t.join().is_err()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.