Refactor around the nightly feature

This commit is contained in:
kngwyu 2020-06-26 18:35:34 +09:00
parent e35622b941
commit a78a832431
3 changed files with 23 additions and 48 deletions

View file

@ -94,16 +94,6 @@ pub trait ToBorrowedObject: ToPyObject {
/// May be more efficient than `to_object` because it does not need /// May be more efficient than `to_object` because it does not need
/// to touch any reference counts when the input object already is a Python object. /// to touch any reference counts when the input object already is a Python object.
fn with_borrowed_ptr<F, R>(&self, py: Python, f: F) -> R fn with_borrowed_ptr<F, R>(&self, py: Python, f: F) -> R
where
F: FnOnce(*mut ffi::PyObject) -> R;
}
#[cfg(feature = "nightly")]
impl<T> ToBorrowedObject for T
where
T: ToPyObject,
{
default fn with_borrowed_ptr<F, R>(&self, py: Python, f: F) -> R
where where
F: FnOnce(*mut ffi::PyObject) -> R, F: FnOnce(*mut ffi::PyObject) -> R,
{ {
@ -116,12 +106,12 @@ where
} }
} }
#[cfg(not(feature = "nightly"))]
impl<T> ToBorrowedObject for T impl<T> ToBorrowedObject for T
where where
T: ToPyObject, T: ToPyObject,
{ {
fn with_borrowed_ptr<F, R>(&self, py: Python, f: F) -> R #[cfg(feature = "nightly")]
default fn with_borrowed_ptr<F, R>(&self, py: Python, f: F) -> R
where where
F: FnOnce(*mut ffi::PyObject) -> R, F: FnOnce(*mut ffi::PyObject) -> R,
{ {

View file

@ -1,7 +1,5 @@
// Copyright (c) 2017-present PyO3 Project and Contributors // Copyright (c) 2017-present PyO3 Project and Contributors
#[cfg(feature = "nightly")]
use crate::buffer;
use crate::err::{self, PyDowncastError, PyErr, PyResult}; use crate::err::{self, PyDowncastError, PyErr, PyResult};
use crate::exceptions; use crate::exceptions;
use crate::ffi::{self, Py_ssize_t}; use crate::ffi::{self, Py_ssize_t};
@ -262,24 +260,19 @@ impl PySequence {
macro_rules! array_impls { macro_rules! array_impls {
($($N:expr),+) => { ($($N:expr),+) => {
$( $(
#[cfg(feature = "nightly")]
impl<'a, T> FromPyObject<'a> for [T; $N] impl<'a, T> FromPyObject<'a> for [T; $N]
where where
T: Copy + Default + FromPyObject<'a>, T: Copy + Default + FromPyObject<'a>,
{ {
default fn extract(obj: &'a PyAny) -> PyResult<Self> { #[cfg(not(feature = "nightly"))]
fn extract(obj: &'a PyAny) -> PyResult<Self> {
let mut array = [T::default(); $N]; let mut array = [T::default(); $N];
extract_sequence_into_slice(obj, &mut array)?; extract_sequence_into_slice(obj, &mut array)?;
Ok(array) Ok(array)
} }
}
#[cfg(not(feature = "nightly"))] #[cfg(feature = "nightly")]
impl<'a, T> FromPyObject<'a> for [T; $N] default fn extract(obj: &'a PyAny) -> PyResult<Self> {
where
T: Copy + Default + FromPyObject<'a>,
{
fn extract(obj: &'a PyAny) -> PyResult<Self> {
let mut array = [T::default(); $N]; let mut array = [T::default(); $N];
extract_sequence_into_slice(obj, &mut array)?; extract_sequence_into_slice(obj, &mut array)?;
Ok(array) Ok(array)
@ -289,12 +282,12 @@ macro_rules! array_impls {
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
impl<'source, T> FromPyObject<'source> for [T; $N] impl<'source, T> FromPyObject<'source> for [T; $N]
where where
for<'a> T: Copy + Default + FromPyObject<'a> + buffer::Element, for<'a> T: Default + FromPyObject<'a> + crate::buffer::Element,
{ {
fn extract(obj: &'source PyAny) -> PyResult<Self> { fn extract(obj: &'source PyAny) -> PyResult<Self> {
let mut array = [T::default(); $N]; let mut array = [T::default(); $N];
// first try buffer protocol // first try buffer protocol
if let Ok(buf) = buffer::PyBuffer::get(obj) { if let Ok(buf) = crate::buffer::PyBuffer::get(obj) {
if buf.dimensions() == 1 && buf.copy_to_slice(obj.py(), &mut array).is_ok() { if buf.dimensions() == 1 && buf.copy_to_slice(obj.py(), &mut array).is_ok() {
buf.release(obj.py()); buf.release(obj.py());
return Ok(array); return Ok(array);
@ -315,21 +308,15 @@ array_impls!(
26, 27, 28, 29, 30, 31, 32 26, 27, 28, 29, 30, 31, 32
); );
#[cfg(not(feature = "nightly"))]
impl<'a, T> FromPyObject<'a> for Vec<T> impl<'a, T> FromPyObject<'a> for Vec<T>
where where
T: FromPyObject<'a>, T: FromPyObject<'a>,
{ {
#[cfg(not(feature = "nightly"))]
fn extract(obj: &'a PyAny) -> PyResult<Self> { fn extract(obj: &'a PyAny) -> PyResult<Self> {
extract_sequence(obj) extract_sequence(obj)
} }
} #[cfg(feature = "nightly")]
#[cfg(feature = "nightly")]
impl<'a, T> FromPyObject<'a> for Vec<T>
where
T: FromPyObject<'a>,
{
default fn extract(obj: &'a PyAny) -> PyResult<Self> { default fn extract(obj: &'a PyAny) -> PyResult<Self> {
extract_sequence(obj) extract_sequence(obj)
} }
@ -338,11 +325,11 @@ where
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
impl<'source, T> FromPyObject<'source> for Vec<T> impl<'source, T> FromPyObject<'source> for Vec<T>
where where
for<'a> T: FromPyObject<'a> + buffer::Element + Copy, for<'a> T: FromPyObject<'a> + crate::buffer::Element,
{ {
fn extract(obj: &'source PyAny) -> PyResult<Self> { fn extract(obj: &'source PyAny) -> PyResult<Self> {
// first try buffer protocol // first try buffer protocol
if let Ok(buf) = buffer::PyBuffer::get(obj) { if let Ok(buf) = crate::buffer::PyBuffer::get(obj) {
if buf.dimensions() == 1 { if buf.dimensions() == 1 {
if let Ok(v) = buf.to_vec(obj.py()) { if let Ok(v) = buf.to_vec(obj.py()) {
buf.release(obj.py()); buf.release(obj.py());

View file

@ -1,22 +1,20 @@
#[rustversion::stable]
#[test] #[test]
fn test_compile_errors() { fn test_compile_errors() {
let t = trybuild::TestCases::new(); let t = trybuild::TestCases::new();
testcase_common(&t); t.compile_fail("tests/ui/invalid_macro_args.rs");
testcase_latest_stable(&t); t.compile_fail("tests/ui/invalid_property_args.rs");
t.compile_fail("tests/ui/invalid_pyclass_args.rs");
t.compile_fail("tests/ui/missing_clone.rs");
t.compile_fail("tests/ui/reject_generics.rs");
t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs");
skip_min_stable(&t);
fn testcase_common(t: &trybuild::TestCases) {
t.compile_fail("tests/ui/invalid_macro_args.rs");
t.compile_fail("tests/ui/invalid_property_args.rs");
t.compile_fail("tests/ui/invalid_pyclass_args.rs");
t.compile_fail("tests/ui/missing_clone.rs");
t.compile_fail("tests/ui/reject_generics.rs");
t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs");
t.compile_fail("tests/ui/invalid_pymethod_names.rs");
}
#[rustversion::since(1.43)] #[rustversion::since(1.43)]
fn testcase_latest_stable(t: &trybuild::TestCases) { fn skip_min_stable(t: &trybuild::TestCases) {
t.compile_fail("tests/ui/static_ref.rs"); t.compile_fail("tests/ui/static_ref.rs");
} }
#[rustversion::before(1.43)] #[rustversion::before(1.43)]
fn testcase_latest_stable(_t: &trybuild::TestCases) {} fn skip_min_stable(_t: &trybuild::TestCases) {}
} }