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:
parent
a106cabc4a
commit
d70479d257
|
@ -71,8 +71,8 @@ pub trait ToPyObject<'p> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// FromPyObject is implemented by various types that can be extracted from a python object.
|
/// FromPyObject is implemented by various types that can be extracted from a python object.
|
||||||
pub trait FromPyObject<'p, 's> {
|
pub trait FromPyObject<'p> {
|
||||||
fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, Self>;
|
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PyObject, PyModule etc.
|
// 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]
|
#[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()))
|
Ok(try!(s.clone().cast_into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,8 @@ impl <'p> ToPyObject<'p> for bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'p, 'a> FromPyObject<'p, 'a> for bool {
|
impl <'p> FromPyObject<'p> for bool {
|
||||||
fn from_py_object(s: &'a PyObject<'p>) -> PyResult<'p, bool> {
|
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, bool> {
|
||||||
Ok(try!(s.clone().cast_into::<PyBool>()).is_true())
|
Ok(try!(s.clone().cast_into::<PyBool>()).is_true())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,21 +91,15 @@ impl <'p, T> ToPyObject<'p> for [T] where T: ToPyObject<'p> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
impl <'p, T> FromPyObject<'p> for Vec<T> where T: FromPyObject<'p> {
|
||||||
This implementation is not possible, because we allow extracting python strings as CowString<'s>,
|
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, Vec<T>> {
|
||||||
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>> {
|
|
||||||
let py = s.python();
|
let py = s.python();
|
||||||
let list = try!(s.cast_as::<PyList>());
|
let list = try!(s.cast_as::<PyList>());
|
||||||
let ptr = list.as_ptr();
|
|
||||||
let mut v = Vec::with_capacity(list.len());
|
let mut v = Vec::with_capacity(list.len());
|
||||||
for i in 0..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!(list.get_item(i).extract::<T>()));
|
||||||
v.push(try!(obj.extract::<T>()));
|
|
||||||
}
|
}
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
|
@ -90,8 +90,8 @@ macro_rules! int_fits_c_long(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="python27-sys")]
|
#[cfg(feature="python27-sys")]
|
||||||
impl <'p, 's> FromPyObject<'p, 's> for $rust_type {
|
impl <'p> FromPyObject<'p> for $rust_type {
|
||||||
fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, $rust_type> {
|
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, $rust_type> {
|
||||||
let py = s.python();
|
let py = s.python();
|
||||||
let val = unsafe { ffi::PyInt_AsLong(s.as_ptr()) };
|
let val = unsafe { ffi::PyInt_AsLong(s.as_ptr()) };
|
||||||
if val == -1 && PyErr::occurred(py) {
|
if val == -1 && PyErr::occurred(py) {
|
||||||
|
@ -105,8 +105,8 @@ macro_rules! int_fits_c_long(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="python3-sys")]
|
#[cfg(feature="python3-sys")]
|
||||||
impl <'p, 's> FromPyObject<'p, 's> for $rust_type {
|
impl <'p> FromPyObject<'p> for $rust_type {
|
||||||
fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, $rust_type> {
|
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, $rust_type> {
|
||||||
let py = s.python();
|
let py = s.python();
|
||||||
let val = unsafe { ffi::PyLong_AsLong(s.as_ptr()) };
|
let val = unsafe { ffi::PyLong_AsLong(s.as_ptr()) };
|
||||||
if val == -1 && PyErr::occurred(py) {
|
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 {
|
impl <'p> FromPyObject<'p> for $rust_type {
|
||||||
fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, $rust_type> {
|
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, $rust_type> {
|
||||||
let py = s.python();
|
let py = s.python();
|
||||||
let val = try!(s.extract::<$larger_type>());
|
let val = try!(s.extract::<$larger_type>());
|
||||||
match num::traits::cast::<$larger_type, $rust_type>(val) {
|
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")]
|
#[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 py = s.python();
|
||||||
let ptr = s.as_ptr();
|
let ptr = s.as_ptr();
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -229,7 +229,7 @@ impl <'p, 's> FromPyObject<'p, 's> for u64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="python3-sys")]
|
#[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 py = s.python();
|
||||||
let ptr = s.as_ptr();
|
let ptr = s.as_ptr();
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -251,8 +251,8 @@ impl <'p> ToPyObject<'p> for f64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'p, 's> FromPyObject<'p, 's> for f64 {
|
impl <'p> FromPyObject<'p> for f64 {
|
||||||
fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, f64> {
|
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, f64> {
|
||||||
let py = s.python();
|
let py = s.python();
|
||||||
let v = unsafe { ffi::PyFloat_AsDouble(s.as_ptr()) };
|
let v = unsafe { ffi::PyFloat_AsDouble(s.as_ptr()) };
|
||||||
if v == -1.0 && PyErr::occurred(py) {
|
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 {
|
impl <'p, 's> FromPyObject<'p> for f32 {
|
||||||
fn from_py_object(s: &'s PyObject<'p>) -> PyResult<'p, f32> {
|
fn from_py_object(s: &PyObject<'p>) -> PyResult<'p, f32> {
|
||||||
Ok(try!(s.extract::<f64>()) as f32)
|
Ok(try!(s.extract::<f64>()) as f32)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -220,7 +220,7 @@ impl <'p> PyObject<'p> {
|
||||||
/// Extracts some type from the python object.
|
/// Extracts some type from the python object.
|
||||||
/// This is a wrapper function around `FromPyObject::from_py_object()`.
|
/// This is a wrapper function around `FromPyObject::from_py_object()`.
|
||||||
#[inline]
|
#[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)
|
::conversion::FromPyObject::from_py_object(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
/// Allows extracting strings from python objects.
|
||||||
/// Accepts python `str` and `unicode` objects.
|
/// Accepts python `str` and `unicode` objects.
|
||||||
/// In python 2.7, `str` is expected to be UTF-8 encoded.
|
/// In python 2.7, `str` is expected to be UTF-8 encoded.
|
||||||
impl <'p, 's> FromPyObject<'p, 's> for String {
|
impl <'p> FromPyObject<'p> for String {
|
||||||
fn from_py_object(o: &'s PyObject<'p>) -> PyResult<'p, String> {
|
fn from_py_object(o: &PyObject<'p>) -> PyResult<'p, String> {
|
||||||
PyString::extract(o).map(|s| s.into_owned())
|
PyString::extract(o).map(|s| s.into_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,8 +148,8 @@ impl <'p> ToPyObject<'p> for NoArgs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'p, 's> FromPyObject<'p, 's> for NoArgs {
|
impl <'p> FromPyObject<'p> for NoArgs {
|
||||||
fn from_py_object(s : &'s PyObject<'p>) -> PyResult<'p, NoArgs> {
|
fn from_py_object(s : &PyObject<'p>) -> PyResult<'p, NoArgs> {
|
||||||
let t = try!(s.cast_as::<PyTuple>());
|
let t = try!(s.cast_as::<PyTuple>());
|
||||||
if t.len() == 0 {
|
if t.len() == 0 {
|
||||||
Ok(NoArgs)
|
Ok(NoArgs)
|
||||||
|
|
Loading…
Reference in New Issue