Skip to content

Commit 89a3e09

Browse files
Make PyBytesWriter -> PyBytes conversion infallible
1 parent 993eda8 commit 89a3e09

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/byteswriter.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::{
1010
},
1111
},
1212
ffi_ptr_ext::FfiPtrExt,
13-
py_result_ext::PyResultExt,
1413
};
1514
use crate::{Bound, IntoPyObject, PyErr, PyResult, Python};
15+
use std::convert::Infallible;
1616
use std::io::IoSlice;
1717
#[cfg(not(Py_LIMITED_API))]
1818
use std::{
@@ -85,24 +85,20 @@ impl<'py> PyBytesWriter<'py> {
8585
}
8686
}
8787

88-
#[cfg(not(Py_LIMITED_API))]
89-
impl<'py> TryFrom<PyBytesWriter<'py>> for Bound<'py, PyBytes> {
90-
type Error = PyErr;
91-
88+
impl<'py> From<PyBytesWriter<'py>> for Bound<'py, PyBytes> {
9289
#[inline]
93-
fn try_from(value: PyBytesWriter<'py>) -> Result<Self, Self::Error> {
90+
#[cfg(not(Py_LIMITED_API))]
91+
fn from(value: PyBytesWriter<'py>) -> Self {
9492
let py = value.python;
9593
unsafe {
9694
PyBytesWriter_Finish(ManuallyDrop::new(value).writer.as_ptr())
97-
.assume_owned_or_err(py)
95+
.assume_owned(py)
9896
.cast_into_unchecked()
9997
}
10098
}
101-
}
10299

103-
#[cfg(Py_LIMITED_API)]
104-
impl<'py> From<PyBytesWriter<'py>> for Bound<'py, PyBytes> {
105100
#[inline]
101+
#[cfg(Py_LIMITED_API)]
106102
fn from(writer: PyBytesWriter<'py>) -> Self {
107103
PyBytes::new(writer.python, &writer.buffer)
108104
}
@@ -111,11 +107,11 @@ impl<'py> From<PyBytesWriter<'py>> for Bound<'py, PyBytes> {
111107
impl<'py> IntoPyObject<'py> for PyBytesWriter<'py> {
112108
type Target = PyBytes;
113109
type Output = Bound<'py, PyBytes>;
114-
type Error = PyErr;
110+
type Error = Infallible;
115111

116112
#[inline]
117113
fn into_pyobject(self, _py: Python<'py>) -> Result<Self::Output, Self::Error> {
118-
self.try_into().map_err(Into::into)
114+
Ok(self.into())
119115
}
120116
}
121117

@@ -211,8 +207,8 @@ mod tests {
211207
Python::attach(|py| {
212208
let buf = [1, 2, 3, 4];
213209
let mut writer = PyBytesWriter::new(py).unwrap();
214-
writer.write(&buf).unwrap();
215-
let bytes: Bound<'_, PyBytes> = writer.try_into().unwrap();
210+
assert_eq!(writer.write(&buf).unwrap(), 4);
211+
let bytes: Bound<'_, PyBytes> = writer.into();
216212
assert_eq!(bytes, buf);
217213
})
218214
}
@@ -222,8 +218,8 @@ mod tests {
222218
Python::attach(|py| {
223219
let bufs = [IoSlice::new(&[1, 2]), IoSlice::new(&[3, 4])];
224220
let mut writer = PyBytesWriter::new(py).unwrap();
225-
writer.write_vectored(&bufs).unwrap();
226-
let bytes: Bound<'_, PyBytes> = writer.try_into().unwrap();
221+
assert_eq!(writer.write_vectored(&bufs).unwrap(), 4);
222+
let bytes: Bound<'_, PyBytes> = writer.into();
227223
assert_eq!(bytes, [1, 2, 3, 4]);
228224
})
229225
}

0 commit comments

Comments
 (0)