diff --git a/src/conversion.rs b/src/conversion.rs index 07d68c9d..92b43024 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -71,8 +71,8 @@ pub trait ToPyObject<'p> { } /// FromPyObject is implemented by various types that can be extracted from a python object. -pub trait FromPyObject<'p, 's> { - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, Self>; +pub trait FromPyObject<'p> { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, Self>; } // PyObject, PyModule etc. @@ -105,9 +105,9 @@ impl <'p, 's> ToPyObject<'p> for PyObject<'s> { } } -impl <'p, 's, T> FromPyObject<'p, 's> for T where T: PythonObjectWithCheckedDowncast<'p> { +impl <'p, T> FromPyObject<'p> for T where T: PythonObjectWithCheckedDowncast<'p> { #[inline] - fn from_py_object(s : &'s PyObject<'p>) -> PyResult<'p, T> { + fn from_py_object(s : &PyObject<'p>) -> PyResult<'p, T> { Ok(try!(s.clone().cast_into())) } } diff --git a/src/objects/boolobject.rs b/src/objects/boolobject.rs index 30fccd0b..8096ca3b 100644 --- a/src/objects/boolobject.rs +++ b/src/objects/boolobject.rs @@ -36,8 +36,8 @@ impl <'p> ToPyObject<'p> for bool { } } -impl <'p, 'a> FromPyObject<'p, 'a> for bool { - fn from_py_object(s: &'a PyObject<'p>) -> PyResult<'p, bool> { +impl <'p> FromPyObject<'p> for bool { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, bool> { Ok(try!(s.clone().cast_into::()).is_true()) } } diff --git a/src/objects/list.rs b/src/objects/list.rs index cce293b4..379e0837 100644 --- a/src/objects/list.rs +++ b/src/objects/list.rs @@ -91,21 +91,15 @@ impl <'p, T> ToPyObject<'p> for [T] where T: ToPyObject<'p> { } } -/* - This implementation is not possible, because we allow extracting python strings as CowString<'s>, - but there's no guarantee that the list isn't modified while the CowString borrow exists. - Maybe reconsider whether extraction should be able to borrow the contents of the python object? -impl <'p, 's, T> FromPyObject<'p, 's> for Vec where T: FromPyObject<'p, 's> { - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, Vec> { +impl <'p, T> FromPyObject<'p> for Vec where T: FromPyObject<'p> { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, Vec> { let py = s.python(); let list = try!(s.cast_as::()); - let ptr = list.as_ptr(); let mut v = Vec::with_capacity(list.len()); - for i in 0..list.len() { - let obj = unsafe { PyObject::from_borrowed_ptr(py, ffi::PyList_GET_ITEM(ptr, i as Py_ssize_t)) }; - v.push(try!(obj.extract::())); + for i in 0 .. list.len() { + v.push(try!(list.get_item(i).extract::())); } Ok(v) } } -*/ + diff --git a/src/objects/num.rs b/src/objects/num.rs index 49ad56e9..58349d4d 100644 --- a/src/objects/num.rs +++ b/src/objects/num.rs @@ -90,8 +90,8 @@ macro_rules! int_fits_c_long( } #[cfg(feature="python27-sys")] - impl <'p, 's> FromPyObject<'p, 's> for $rust_type { - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, $rust_type> { + impl <'p> FromPyObject<'p> for $rust_type { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, $rust_type> { let py = s.python(); let val = unsafe { ffi::PyInt_AsLong(s.as_ptr()) }; if val == -1 && PyErr::occurred(py) { @@ -105,8 +105,8 @@ macro_rules! int_fits_c_long( } #[cfg(feature="python3-sys")] - impl <'p, 's> FromPyObject<'p, 's> for $rust_type { - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, $rust_type> { + impl <'p> FromPyObject<'p> for $rust_type { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, $rust_type> { let py = s.python(); let val = unsafe { ffi::PyLong_AsLong(s.as_ptr()) }; if val == -1 && PyErr::occurred(py) { @@ -133,8 +133,8 @@ macro_rules! int_fits_larger_int( } } - impl <'p, 's> FromPyObject<'p, 's> for $rust_type { - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, $rust_type> { + impl <'p> FromPyObject<'p> for $rust_type { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, $rust_type> { let py = s.python(); let val = try!(s.extract::<$larger_type>()); match num::traits::cast::<$larger_type, $rust_type>(val) { @@ -208,9 +208,9 @@ fn pylong_as_u64<'p>(obj: &PyObject<'p>) -> PyResult<'p, u64> { } } -impl <'p, 's> FromPyObject<'p, 's> for u64 { +impl <'p> FromPyObject<'p> for u64 { #[cfg(feature="python27-sys")] - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, u64> { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, u64> { let py = s.python(); let ptr = s.as_ptr(); unsafe { @@ -229,7 +229,7 @@ impl <'p, 's> FromPyObject<'p, 's> for u64 { } #[cfg(feature="python3-sys")] - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, u64> { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, u64> { let py = s.python(); let ptr = s.as_ptr(); unsafe { @@ -251,8 +251,8 @@ impl <'p> ToPyObject<'p> for f64 { } } -impl <'p, 's> FromPyObject<'p, 's> for f64 { - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, f64> { +impl <'p> FromPyObject<'p> for f64 { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, f64> { let py = s.python(); let v = unsafe { ffi::PyFloat_AsDouble(s.as_ptr()) }; if v == -1.0 && PyErr::occurred(py) { @@ -275,8 +275,8 @@ impl <'p> ToPyObject<'p> for f32 { } } -impl <'p, 's> FromPyObject<'p, 's> for f32 { - fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, f32> { +impl <'p, 's> FromPyObject<'p> for f32 { + fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, f32> { Ok(try!(s.extract::()) as f32) } } diff --git a/src/objects/object.rs b/src/objects/object.rs index 46b3d227..72f14bfc 100644 --- a/src/objects/object.rs +++ b/src/objects/object.rs @@ -220,7 +220,7 @@ impl <'p> PyObject<'p> { /// Extracts some type from the python object. /// This is a wrapper function around `FromPyObject::from_py_object()`. #[inline] - pub fn extract<'s, T>(&'s self) -> Result> where T: ::conversion::FromPyObject<'p, 's> { + pub fn extract(&self) -> Result> where T: ::conversion::FromPyObject<'p> { ::conversion::FromPyObject::from_py_object(self) } } diff --git a/src/objects/string.rs b/src/objects/string.rs index 18908a21..29f2f9cc 100644 --- a/src/objects/string.rs +++ b/src/objects/string.rs @@ -205,20 +205,11 @@ impl <'p, 'a> ToPyObject<'p> for &'a str { } } -/* Let's disable the Cow extraction for now. We might want to avoid "extract" borrowing the input - python object, see the (also currently disabled) impl FromPyObject for Vec. -impl <'p, 's> FromPyObject<'p, 's> for Cow<'s, str> { - fn from_py_object(o: &'s PyObject<'p>) -> PyResult<'p, Cow<'s, str>> { - PyString::extract(o) - } -} -*/ - /// Allows extracting strings from python objects. /// Accepts python `str` and `unicode` objects. /// In python 2.7, `str` is expected to be UTF-8 encoded. -impl <'p, 's> FromPyObject<'p, 's> for String { - fn from_py_object(o: &'s PyObject<'p>) -> PyResult<'p, String> { +impl <'p> FromPyObject<'p> for String { + fn from_py_object(o: &PyObject<'p>) -> PyResult<'p, String> { PyString::extract(o).map(|s| s.into_owned()) } } diff --git a/src/objects/tuple.rs b/src/objects/tuple.rs index fa23099f..d1f4b1f6 100644 --- a/src/objects/tuple.rs +++ b/src/objects/tuple.rs @@ -148,8 +148,8 @@ impl <'p> ToPyObject<'p> for NoArgs { } } -impl <'p, 's> FromPyObject<'p, 's> for NoArgs { - fn from_py_object(s : &'s PyObject<'p>) -> PyResult<'p, NoArgs> { +impl <'p> FromPyObject<'p> for NoArgs { + fn from_py_object(s : &PyObject<'p>) -> PyResult<'p, NoArgs> { let t = try!(s.cast_as::()); if t.len() == 0 { Ok(NoArgs)