Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- added example combining Python bindings with winnow parsing
- added `ByteSource` support for `VecDeque<T>` when `zerocopy` is enabled and kept the deque as owner
- added `ByteSource` support for `Cow<'static, T>` where `T: AsRef<[u8]>`
- added `ByteArea` for staged file writes with `Section::freeze()` to return `Bytes`
Expand Down
4 changes: 2 additions & 2 deletions INVENTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- None at the moment.

## Desired Functionality
- Example demonstrating Python + winnow parsing.
- Python example using winnow::view to parse structured data.

## Discovered Issues
- None at the moment.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ needs these libraries installed; otherwise disable the feature during testing.
- [`examples/try_unwrap_owner.rs`](examples/try_unwrap_owner.rs) – reclaim the owner when uniquely referenced
- [`examples/pybytes.rs`](examples/pybytes.rs) – demonstrates the `pyo3` feature using `PyBytes`
- [`examples/from_python.rs`](examples/from_python.rs) – wrap a Python `bytes` object into `Bytes`
- [`examples/python_winnow.rs`](examples/python_winnow.rs) – parse Python bytes with winnow

## Comparison

Expand Down
19 changes: 19 additions & 0 deletions examples/python_winnow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![cfg(all(feature = "pyo3", feature = "winnow"))]

use anybytes::Bytes;
use pyo3::{prelude::*, types::PyBytes};
use winnow::{error::ContextError, stream::AsBytes, token::take, Parser};

fn main() -> PyResult<()> {
Python::with_gil(|py| {
let obj = PyBytes::new(py, b"abcde");
let mut bytes = Bytes::from_source(obj);

// Take the first three bytes using a winnow parser
let mut parser = take::<_, _, ContextError>(3usize);
let prefix: Bytes = parser.parse_next(&mut bytes).expect("take");
assert_eq!(prefix.as_bytes(), b"abc".as_ref());
assert_eq!(bytes.as_bytes(), b"de".as_ref());
Ok(())
})
}