Skip to content

Commit

Permalink
Remove many specialization uses
Browse files Browse the repository at this point in the history
From over a hundret "default fn" uses down to 17
  • Loading branch information
konstin committed Aug 25, 2018
1 parent 33e72a2 commit 7c0379b
Show file tree
Hide file tree
Showing 34 changed files with 526 additions and 1,037 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

* Slowly removing specialization uses
* All exceptions are consturcted with `py_err` instead of `new`, as they return `PyErr` and not `Self`.
* `as_mut` and friends take and `&mut self` instead of `&self`
* `ObjectProtocol::call` now takes an `Option<PyDict>` for the kwargs instead of an `IntoPyDictPointer`.
* `IntoPyDictPointer` was replace by `IntoPyDict` which doesn't convert `PyDict` itself anymore and returns a `PyDict` instead of `*mut PyObject`.

### Fixed

Expand Down
9 changes: 3 additions & 6 deletions guide/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ fn main() {
}
```

`kwargs` argument is generate over
[`IntoPyDictPointer`][IntoPyDictPointer] trait. `HashMap` or `BTreeMap` could be used as
keyword arguments. rust tuple with up to 10 elements where each element is tuple with size 2
could be used as kwargs as well. Or `NoArgs` object can be used to indicate that
no keywords arguments are provided.
`kwargs` can by `None` or `Some(PyDict)`. You can use the
[`IntoPyDict`][IntoPyDict] trait to convert other dict-like containers, e.g. `HashMap`, `BTreeMap` as well as tuples with up to 10 elements and `Vec`s where each element is a two element tuple.

```rust
extern crate pyo3;
Expand Down Expand Up @@ -130,4 +127,4 @@ TODO
[IntoPyTuple]: https://docs.rs/pyo3/0.2.7/trait.IntoPyTuple.html
[PyTuple]: https://docs.rs/pyo3/0.2.7/struct.PyTuple.html
[ObjectProtocol]: https://docs.rs/pyo3/0.2.7/trait.ObjectProtocol.html
[IntoPyDictPointer]: https://docs.rs/pyo3/0.2.7/trait.IntoPyDictPointer.html
[IntoPyDict]: https://docs.rs/pyo3/0.2.7/trait.IntoPyDict.html
2 changes: 1 addition & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ mod test {
let array = py
.import("array")
.unwrap()
.call_method("array", ("f", (1.0, 1.5, 2.0, 2.5)), ::NoArgs)
.call_method("array", ("f", (1.0, 1.5, 2.0, 2.5)), None)
.unwrap();
let buffer = PyBuffer::get(py, array.into()).unwrap();
assert_eq!(buffer.dimensions(), 1);
Expand Down
76 changes: 20 additions & 56 deletions src/class/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,18 @@ pub trait PyAsyncAexitProtocol<'p>: PyAsyncProtocol<'p> {
#[cfg(Py_3)]
#[doc(hidden)]
pub trait PyAsyncProtocolImpl {
fn tp_as_async() -> Option<ffi::PyAsyncMethods>;

fn methods() -> Vec<PyMethodDef>;
}

#[cfg(Py_3)]
impl<T> PyAsyncProtocolImpl for T {
#[inline]
default fn tp_as_async() -> Option<ffi::PyAsyncMethods> {
fn tp_as_async() -> Option<ffi::PyAsyncMethods> {
None
}

#[inline]
default fn methods() -> Vec<PyMethodDef> {
fn methods() -> Vec<PyMethodDef> {
Vec::new()
}
}

#[cfg(Py_3)]
impl<T> PyAsyncProtocolImpl for T {}

#[cfg(Py_3)]
impl<'p, T> PyAsyncProtocolImpl for T
where
Expand Down Expand Up @@ -139,19 +133,13 @@ where
}

trait PyAsyncAwaitProtocolImpl {
fn am_await() -> Option<ffi::unaryfunc>;
}

impl<'p, T> PyAsyncAwaitProtocolImpl for T
where
T: PyAsyncProtocol<'p>,
{
#[inline]
default fn am_await() -> Option<ffi::unaryfunc> {
fn am_await() -> Option<ffi::unaryfunc> {
None
}
}

impl<'p, T> PyAsyncAwaitProtocolImpl for T where T: PyAsyncProtocol<'p> {}

impl<T> PyAsyncAwaitProtocolImpl for T
where
T: for<'p> PyAsyncAwaitProtocol<'p>,
Expand All @@ -168,19 +156,13 @@ where
}

trait PyAsyncAiterProtocolImpl {
fn am_aiter() -> Option<ffi::unaryfunc>;
}

impl<'p, T> PyAsyncAiterProtocolImpl for T
where
T: PyAsyncProtocol<'p>,
{
#[inline]
default fn am_aiter() -> Option<ffi::unaryfunc> {
fn am_aiter() -> Option<ffi::unaryfunc> {
None
}
}

impl<'p, T> PyAsyncAiterProtocolImpl for T where T: PyAsyncProtocol<'p> {}

impl<T> PyAsyncAiterProtocolImpl for T
where
T: for<'p> PyAsyncAiterProtocol<'p>,
Expand All @@ -197,19 +179,13 @@ where
}

trait PyAsyncAnextProtocolImpl {
fn am_anext() -> Option<ffi::unaryfunc>;
}

impl<'p, T> PyAsyncAnextProtocolImpl for T
where
T: PyAsyncProtocol<'p>,
{
#[inline]
default fn am_anext() -> Option<ffi::unaryfunc> {
fn am_anext() -> Option<ffi::unaryfunc> {
None
}
}

impl<'p, T> PyAsyncAnextProtocolImpl for T where T: PyAsyncProtocol<'p> {}

#[cfg(Py_3)]
mod anext {
use super::{PyAsyncAnextProtocol, PyAsyncAnextProtocolImpl};
Expand Down Expand Up @@ -260,29 +236,17 @@ mod anext {
}

trait PyAsyncAenterProtocolImpl {
fn __aenter__() -> Option<PyMethodDef>;
}

impl<'p, T> PyAsyncAenterProtocolImpl for T
where
T: PyAsyncProtocol<'p>,
{
#[inline]
default fn __aenter__() -> Option<PyMethodDef> {
fn __aenter__() -> Option<PyMethodDef> {
None
}
}

trait PyAsyncAexitProtocolImpl {
fn __aexit__() -> Option<PyMethodDef>;
}
impl<'p, T> PyAsyncAenterProtocolImpl for T where T: PyAsyncProtocol<'p> {}

impl<'p, T> PyAsyncAexitProtocolImpl for T
where
T: PyAsyncProtocol<'p>,
{
#[inline]
default fn __aexit__() -> Option<PyMethodDef> {
trait PyAsyncAexitProtocolImpl {
fn __aexit__() -> Option<PyMethodDef> {
None
}
}

impl<'p, T> PyAsyncAexitProtocolImpl for T where T: PyAsyncProtocol<'p> {}
Loading

0 comments on commit 7c0379b

Please sign in to comment.