Remove lifetime 's from `FromPyObject`.

This allows implementing `FromPyObject` for `Vec<T>`.
It also means that `obj.extract::<Cow<str>>()` won't be supported, but we
have `PyString::extract()` for that now.
This commit is contained in:
Daniel Grunwald 2015-05-24 17:12:12 +02:00
parent a106cabc4a
commit d70479d257
7 changed files with 29 additions and 44 deletions

View File

@ -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()))
}
}

View File

@ -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::<PyBool>()).is_true())
}
}

View File

@ -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<T> where T: FromPyObject<'p, 's> {
fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, Vec<T>> {
impl <'p, T> FromPyObject<'p> for Vec<T> where T: FromPyObject<'p> {
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, Vec<T>> {
let py = s.python();
let list = try!(s.cast_as::<PyList>());
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::<T>()));
for i in 0 .. list.len() {
v.push(try!(list.get_item(i).extract::<T>()));
}
Ok(v)
}
}
*/

View File

@ -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::<f64>()) as f32)
}
}

View File

@ -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<T, PyErr<'p>> where T: ::conversion::FromPyObject<'p, 's> {
pub fn extract<T>(&self) -> Result<T, PyErr<'p>> where T: ::conversion::FromPyObject<'p> {
::conversion::FromPyObject::from_py_object(self)
}
}

View File

@ -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<T>.
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())
}
}

View File

@ -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::<PyTuple>());
if t.len() == 0 {
Ok(NoArgs)