Skip to content

Commit

Permalink
Improve documentation (#3)
Browse files Browse the repository at this point in the history
* README example

* Add more seriously named function, add docs on usage

* create_stream -> stream_fn

* Fix actions

* Remove cache and cargo-hack for formatter
  • Loading branch information
aumetra authored Oct 8, 2024
1 parent 40e78ff commit 9deb96d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
25 changes: 22 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ jobs:
- name: Run checks
run: cargo hack clippy --feature-powerset --no-dev-deps -- -D warnings

formatting:
name: Formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "nightly"
components: "rustfmt"
- name: Run checks
run: cargo fmt --all -- --check

tests:
name: Tests
runs-on: ubuntu-latest
Expand Down Expand Up @@ -50,8 +62,12 @@ jobs:
run: cargo hack miri test --feature-powerset

address-sanitizer:
name: Address sanitizer
name: "Sanitizer: ${{ matrix.sanitizer }}"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
sanitizer: [address, memory, thread, leak]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
Expand All @@ -60,7 +76,10 @@ jobs:
components: "rust-src"
- uses: taiki-e/install-action@cargo-hack
- uses: Swatinem/rust-cache@v2
- name: Run tests through address sanitizer
- name: "Run tests through ${{ matrix.sanitizer }} sanitizer"
env:
RUSTFLAGS: "-Zrandomize-layout -Zsanitizer=address"
ASAN_OPTIONS: "detect_stack_use_after_return=1"
RUST_BACKTRACE: "0"
RUSTDOCFLAGS: "-Zrandomize-layout -Zsanitizer=${{ matrix.sanitizer }}"
RUSTFLAGS: "-Zrandomize-layout -Zsanitizer=${{ matrix.sanitizer }}"
run: cargo hack test --feature-powerset -Zbuild-std --target x86_64-unknown-linux-gnu
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ Features:
>
> While you can't use the yielder inside the unwrapped future, stuff like `embassy` should work again.
## Example

```rust
use futures_lite::stream;
use std::pin::pin;

let stream = pin!(asynk_strim::stream_fn(|mut yielder| async move {
yielder.yield_item("hello world!").await;
yielder.yield_item("pretty neato, ain't it?").await;
}));

let mut stream = stream::block_on(stream);
assert_eq!(stream.next(), Some("hello world!"));
assert_eq!(stream.next(), Some("pretty neato, ain't it?"));
assert_eq!(stream.next(), None);
```

## Comparisons

### `async-stream`
Expand Down
39 changes: 38 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,48 @@ where
}

/// Create a new stream
///
/// # Example
///
/// Let's yield some lyrics (Song: "Verdächtig" by Systemabsturz):
///
/// ```
/// # use futures_lite::StreamExt;
/// # use std::pin::pin;
/// # futures_lite::future::block_on(async {
/// let stream = asynk_strim::stream_fn(|mut yielder| async move {
/// yielder.yield_item("Fahr den Imsi-Catcher hoch").await;
/// yielder.yield_item("Mach das Richtmikro an").await;
/// yielder.yield_item("Bring Alexa auf den Markt").await;
/// yielder.yield_item("Zapf den Netzknoten an").await;
/// yielder.yield_item("Fahr den Ü-Wagen vor").await;
/// yielder.yield_item("Kauf den Staatstrojaner ein").await;
/// yielder.yield_item("Fake die Exit-Nodes bei Tor").await;
/// yielder.yield_item("Ihr wollt doch alle sicher sein").await;
/// });
///
/// let mut stream = pin!(stream);
/// while let Some(item) = stream.next().await {
/// println!("{item}");
/// }
/// # });
#[inline]
pub fn strim_fn<F, Item, Fut>(func: F) -> impl Stream<Item = Item>
pub fn stream_fn<F, Item, Fut>(func: F) -> impl Stream<Item = Item>
where
F: FnOnce(Yielder<Item>) -> Fut,
Fut: Future<Output = ()>,
{
crate::stream::init(func)
}

/// Jokey alias for [`stream_fn`]
///
/// For more elaborate documentation, see [`stream_fn`]
#[inline]
pub fn strim_fn<F, Item, Fut>(func: F) -> impl Stream<Item = Item>
where
F: FnOnce(Yielder<Item>) -> Fut,
Fut: Future<Output = ()>,
{
stream_fn(func)
}

0 comments on commit 9deb96d

Please sign in to comment.