Skip to content

Commit c63086b

Browse files
Add 'py lifetime to PyUnicodeWriter
1 parent 3daf2d2 commit c63086b

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/fmt.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,24 @@ macro_rules! py_format {
4040

4141
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
4242
/// The `PyUnicodeWriter` is a utility for efficiently constructing Python strings
43-
pub struct PyUnicodeWriter {
43+
pub struct PyUnicodeWriter<'py> {
44+
python: Python<'py>,
4445
writer: NonNull<ffi::PyUnicodeWriter>,
4546
last_error: Option<PyErr>,
4647
}
4748

4849
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
49-
impl PyUnicodeWriter {
50+
impl<'py> PyUnicodeWriter<'py> {
5051
/// Creates a new `PyUnicodeWriter`.
51-
pub fn new(py: Python<'_>) -> PyResult<Self> {
52+
pub fn new(py: Python<'py>) -> PyResult<Self> {
5253
Self::with_capacity(py, 0)
5354
}
5455

5556
/// Creates a new `PyUnicodeWriter` with the specified initial capacity.
56-
pub fn with_capacity(py: Python<'_>, capacity: usize) -> PyResult<Self> {
57+
pub fn with_capacity(py: Python<'py>, capacity: usize) -> PyResult<Self> {
5758
match NonNull::new(unsafe { PyUnicodeWriter_Create(capacity.wrapping_cast()) }) {
5859
Some(ptr) => Ok(PyUnicodeWriter {
60+
python: py,
5961
writer: ptr,
6062
last_error: None,
6163
}),
@@ -64,7 +66,8 @@ impl PyUnicodeWriter {
6466
}
6567

6668
/// Consumes the `PyUnicodeWriter` and returns a `Bound<PyString>` containing the constructed string.
67-
pub fn into_py_string(mut self, py: Python<'_>) -> PyResult<Bound<'_, PyString>> {
69+
pub fn into_py_string(mut self) -> PyResult<Bound<'py, PyString>> {
70+
let py = self.python;
6871
if let Some(error) = self.take_error() {
6972
Err(error)
7073
} else {
@@ -86,14 +89,12 @@ impl PyUnicodeWriter {
8689
}
8790

8891
fn set_error(&mut self) {
89-
Python::attach(|py| {
90-
self.last_error = Some(PyErr::fetch(py));
91-
})
92+
self.last_error = Some(PyErr::fetch(self.python));
9293
}
9394
}
9495

9596
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
96-
impl fmt::Write for PyUnicodeWriter {
97+
impl fmt::Write for PyUnicodeWriter<'_> {
9798
fn write_str(&mut self, s: &str) -> fmt::Result {
9899
let result = unsafe {
99100
PyUnicodeWriter_WriteUTF8(self.as_ptr(), s.as_ptr().cast(), s.len() as isize)
@@ -118,7 +119,7 @@ impl fmt::Write for PyUnicodeWriter {
118119
}
119120

120121
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
121-
impl Drop for PyUnicodeWriter {
122+
impl Drop for PyUnicodeWriter<'_> {
122123
fn drop(&mut self) {
123124
unsafe {
124125
PyUnicodeWriter_Discard(self.as_ptr());
@@ -127,13 +128,13 @@ impl Drop for PyUnicodeWriter {
127128
}
128129

129130
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
130-
impl<'py> IntoPyObject<'py> for PyUnicodeWriter {
131+
impl<'py> IntoPyObject<'py> for PyUnicodeWriter<'py> {
131132
type Target = PyString;
132133
type Output = Bound<'py, Self::Target>;
133134
type Error = PyErr;
134135

135-
fn into_pyobject(self, py: Python<'py>) -> PyResult<Bound<'py, PyString>> {
136-
self.into_py_string(py)
136+
fn into_pyobject(self, _py: Python<'py>) -> PyResult<Bound<'py, PyString>> {
137+
self.into_py_string()
137138
}
138139
}
139140

@@ -153,7 +154,7 @@ mod tests {
153154
let mut writer = PyUnicodeWriter::new(py).unwrap();
154155
write!(writer, "Hello {}!", "world").unwrap();
155156
writer.write_char('😎').unwrap();
156-
let result = writer.into_py_string(py).unwrap();
157+
let result = writer.into_py_string().unwrap();
157158
assert_eq!(result.to_string(), "Hello world!😎");
158159
});
159160
}

src/types/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl PyString {
270270
writer
271271
.write_fmt(args)
272272
.map_err(|_| writer.take_error().expect("expected error"))?;
273-
writer.into_py_string(py)
273+
writer.into_py_string()
274274
}
275275

276276
#[cfg(any(Py_LIMITED_API, PyPy))]

0 commit comments

Comments
 (0)