Skip to content

Commit 064cc4e

Browse files
Merge pull request #52 from triblespace/codex/determine-next-steps-for-project-advancement
Add winnow view Python example
2 parents bc3fae2 + efb9bb9 commit 064cc4e

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
- added example combining Python bindings with winnow parsing
5+
- added Python example demonstrating structured parsing with winnow's `view`
56
- added `ByteSource` support for `VecDeque<T>` when `zerocopy` is enabled and kept the deque as owner
67
- added `ByteSource` support for `Cow<'static, T>` where `T: AsRef<[u8]>`
78
- added `ByteArea` for staged file writes with `Section::freeze()` to return `Bytes`

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ required-features = ["pyo3"]
4545
[[example]]
4646
name = "pybytes"
4747
required-features = ["pyo3"]
48+
49+
[[example]]
50+
name = "python_winnow"
51+
required-features = ["pyo3", "winnow"]
52+
53+
[[example]]
54+
name = "python_winnow_view"
55+
required-features = ["pyo3", "winnow", "zerocopy"]

INVENTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- None at the moment.
55

66
## Desired Functionality
7-
- Python example using winnow::view to parse structured data.
7+
- Add Kani proofs for winnow view helpers.
88

99
## Discovered Issues
1010
- None at the moment.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ needs these libraries installed; otherwise disable the feature during testing.
148148
- [`examples/pybytes.rs`](examples/pybytes.rs) – demonstrates the `pyo3` feature using `PyBytes`
149149
- [`examples/from_python.rs`](examples/from_python.rs) – wrap a Python `bytes` object into `Bytes`
150150
- [`examples/python_winnow.rs`](examples/python_winnow.rs) – parse Python bytes with winnow
151+
- [`examples/python_winnow_view.rs`](examples/python_winnow_view.rs) – parse structured data from Python bytes using winnow's `view`
151152

152153
## Comparison
153154

examples/python_winnow_view.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![cfg(all(feature = "pyo3", feature = "winnow", feature = "zerocopy"))]
2+
3+
use anybytes::{winnow as ab_winnow, Bytes, View};
4+
use pyo3::{prelude::*, types::PyBytes};
5+
use winnow::{error::ContextError, stream::AsBytes, Parser};
6+
use zerocopy::{Immutable, KnownLayout, TryFromBytes};
7+
8+
#[derive(TryFromBytes, Immutable, KnownLayout)]
9+
#[repr(C)]
10+
struct Header {
11+
magic: u16,
12+
value: u16,
13+
}
14+
15+
fn main() -> PyResult<()> {
16+
Python::with_gil(|py| {
17+
let obj = PyBytes::new(py, &[0x34, 0x12, 0x78, 0x56]);
18+
let mut bytes = Bytes::from_source(obj);
19+
20+
let mut parser = ab_winnow::view::<Header, ContextError>;
21+
let header: View<Header> = parser.parse_next(&mut bytes).expect("parse header");
22+
23+
assert_eq!(header.magic, 0x1234);
24+
assert_eq!(header.value, 0x5678);
25+
assert_eq!(bytes.as_bytes(), b"".as_ref());
26+
Ok(())
27+
})
28+
}

0 commit comments

Comments
 (0)