Skip to content

Commit

Permalink
Remove duplicate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Oct 9, 2024
1 parent 9233d77 commit 60410e1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ Features:
- one dependency (besides `futures-core` which I don't count since it provides the `Stream` definition)
- `no_std`-compatible, zero allocations

> [!IMPORTANT]
> This crate adds a wrapper around the wakers that contains data and pointers needed to yield items.
> Crates like [`embassy`](https://embassy.dev) use a similar approach and will therefore clash with us.
>
> If you run into this issue (which will manifest as a runtime panic), you can use the `unwrap_waker` function.
> This function will wrap a future and remove the waker wrapper.
>
> While you can't use the yielder inside the unwrapped future, stuff like `embassy` should work again.
### ⚠ Important

This crate adds a wrapper around the wakers that contains data and pointers needed to yield items.
Crates like [`embassy`](https://embassy.dev) use a similar approach and will therefore clash with us.

If you run into this issue (which will manifest as a runtime panic), you can use the `unwrap_waker` function.
This function will wrap a future and remove the waker wrapper.

While you can't use the yielder inside the unwrapped future, stuff like `embassy` should work again.

## Example

Expand Down
27 changes: 12 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,21 @@ where
///
/// # Example
///
/// Let's yield some lyrics (Song: "Verdächtig" by Systemabsturz):
/// Let's yield some lyrics (Song: "Archbombe" by Systemabsturz):
///
/// ```
/// # use futures_lite::StreamExt;
/// # use std::pin::pin;
/// # use std::convert::Infallible;
/// # futures_lite::future::block_on(async {
/// let stream = asynk_strim::try_stream_fn(|mut yielder| async move {
/// yielder.yield_ok("Fahr den Imsi-Catcher hoch").await;
/// yielder.yield_ok("Mach das Richtmikro an").await;
/// yielder.yield_ok("Bring Alexa auf den Markt").await;
/// yielder.yield_ok("Zapf den Netzknoten an").await;
/// yielder.yield_ok("Fahr den Ü-Wagen vor").await;
/// yielder.yield_ok("Kauf den Staatstrojaner ein").await;
/// yielder.yield_ok("Fake die Exit-Nodes bei Tor").await;
/// yielder.yield_ok("Ihr wollt doch alle sicher sein").await;
/// yielder.yield_ok("Meine Programme habe ich mal ausgecheckt").await;
/// yielder.yield_ok("Dass ich mit Zündern reden kann finde ich suspekt").await;
/// yielder.yield_ok("Meine Codezeilen haben anfangs Hippies geschrieben").await;
/// yielder.yield_error("Von ihrem Pazifismus ist nicht viel geblieben").await;
/// yielder.yield_ok("Ich bin echt nicht glücklich und nicht einverstanden").await;
///
/// Ok::<_, Infallible>(())
/// Err("Ich als Bombensteuerung soll auf Menschen landen")
/// });
///
/// let mut stream = pin!(stream);
Expand All @@ -124,13 +121,13 @@ where
F: FnOnce(TryYielder<Ok, Error>) -> Fut,
Fut: Future<Output = Result<(), Error>>,
{
let func = |mut yielder: TryYielder<_, _>| async move {
if let Err(err) = func(yielder.duplicate()).await {
crate::stream::init(|mut yielder: TryYielder<_, _>| async move {
// trivially copyable. bit-wise copy is fine.
#[allow(unsafe_code)]
if let Err(err) = func(unsafe { core::ptr::read(&yielder) }).await {
yielder.yield_error(err).await;
}
};

crate::stream::init(func)
})
}

/// Jokey alias for [`try_stream_fn`]
Expand Down
6 changes: 0 additions & 6 deletions src/try_yielder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ pub struct TryYielder<Ok, Error> {
}

impl<Ok, Error> TryYielder<Ok, Error> {
pub(crate) fn duplicate(&self) -> Self {
Self {
yielder: self.yielder.duplicate(),
}
}

/// Yield a success value from the stream
#[inline]
pub async fn yield_ok(&mut self, item: Ok) {
Expand Down
7 changes: 0 additions & 7 deletions src/yielder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ impl<Item> Yielder<Item> {
}
}

pub(crate) fn duplicate(&self) -> Self {
Self {
_marker: PhantomData,
stream_address: self.stream_address,
}
}

/// Yield an item from the stream
#[inline]
pub async fn yield_item(&mut self, item: Item) {
Expand Down

0 comments on commit 60410e1

Please sign in to comment.