diff --git a/newsfragments/3802.added.md b/newsfragments/3802.added.md new file mode 100644 index 00000000000..86b98e9df97 --- /dev/null +++ b/newsfragments/3802.added.md @@ -0,0 +1 @@ +Add `PyBackedStr` and `PyBackedBytes`, as alternatives to `&str` and `&bytes` where a Python object owns the data. diff --git a/src/pybacked.rs b/src/pybacked.rs index d1e60db6149..b88781f35e6 100644 --- a/src/pybacked.rs +++ b/src/pybacked.rs @@ -1,3 +1,5 @@ +//! Contains types for working with Python objects that own the underlying data. + use std::{ops::Deref, ptr::NonNull}; use crate::{ @@ -39,7 +41,7 @@ impl TryFrom> for PyBackedStr { } #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] { - let bytes = string.encode_utf8()?; + let bytes = py_string.encode_utf8()?; let b = bytes.as_bytes(); let data = NonNull::from(b); let length = b.len(); @@ -67,6 +69,7 @@ pub struct PyBackedBytes { data: NonNull<[u8]>, } +#[allow(dead_code)] enum PyBackedBytesStorage { Python(Py), Rust(Box<[u8]>),