https://github.com/storycraft/stream-parser
Write asynchronous version of parse function, automatically supports parsing synchronously, asynchronously in both blocking, nonblocking mode
https://github.com/storycraft/stream-parser
async rust stream
Last synced: 2 months ago
JSON representation
Write asynchronous version of parse function, automatically supports parsing synchronously, asynchronously in both blocking, nonblocking mode
- Host: GitHub
- URL: https://github.com/storycraft/stream-parser
- Owner: storycraft
- Created: 2023-07-25T04:23:13.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-02T07:36:29.000Z (almost 2 years ago)
- Last Synced: 2025-02-02T18:14:03.290Z (4 months ago)
- Topics: async, rust, stream
- Language: Rust
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Stream parser
> Stop writing two version of io parserWrite asynchronous version of parse function. Use `StreamParser` to automatically supports parsing synchronously, asynchronously in both blocking, nonblocking mode. The created parser is runtime-independent and its read operation is stateful(cancel safe).
## Example
sample.txt
```
Hello world!
```main.rs
```rust ignore
#[tokio::main]
async fn main() -> Result<(), Box> {
// read constant amount of bytes
// Must be woken by underlying `AsyncRead`, otherwise it will panic
async fn read_fn(
mut reader: Pin<&mut impl AsyncRead>,
) -> io::Result<[u8; SIZE]> {
let mut arr = [0_u8; SIZE];
reader.read_exact(&mut arr[..SIZE]).await?;Ok(arr)
}let mut parser = StreamParser::new(read_fn::<5>);
let mut sync_io = std::fs::File::open("./sample.txt")?;
// read "Hello" synchronously
dbg!(&std::str::from_utf8(&parser.read(&mut sync_io)?)?);let mut async_io = tokio::fs::File::open("./sample.txt")
.await?
.compat();// read "Hello" asynchronously
dbg!(&std::str::from_utf8(
&parser.read_async(&mut async_io).await?
)?);Ok(())
}
```## License
MIT