Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove useless IntoRust bound in handlers system #460

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ impl Drop for PythonCallback {
}
}

// the generic type is not useful per se, it just there to make typing
// prettier, e.g. to have `get` returning a `PyResult<HandlerImpl<Reply>>`
pub(crate) enum HandlerImpl<T> {
Rust(Py<Handler>, PhantomData<T>),
Python(PyObject),
Expand Down Expand Up @@ -239,10 +241,7 @@ impl<T> ToPyObject for HandlerImpl<T> {
}
}

impl<T: IntoRust> HandlerImpl<T>
where
T::Into: IntoPython,
{
impl<T> HandlerImpl<T> {
pub(crate) fn try_recv(&self, py: Python) -> PyResult<PyObject> {
match self {
Self::Rust(handler, _) => handler.borrow(py).try_recv(py),
Expand All @@ -258,11 +257,11 @@ where
}
}

struct RustHandler<H: IntoRust, T: IntoRust>
struct RustHandler<H: IntoRust, T: IntoPython>
where
H::Into: IntoHandler<T::Into>,
H::Into: IntoHandler<T>,
{
handler: <H::Into as IntoHandler<T::Into>>::Handler,
handler: <H::Into as IntoHandler<T>>::Handler,
_phantom: PhantomData<T>,
}

Expand Down Expand Up @@ -300,10 +299,7 @@ impl<E: fmt::Display> fmt::Display for DeadlineError<E> {
}
}

impl<T: IntoRust> Receiver for RustHandler<DefaultHandler, T>
where
T::Into: IntoPython,
{
impl<T: IntoPython> Receiver for RustHandler<DefaultHandler, T> {
fn type_name(&self) -> &'static str {
short_type_name::<T>()
}
Expand All @@ -325,10 +321,7 @@ where
}
}

impl<T: IntoRust> Receiver for RustHandler<FifoChannel, T>
where
T::Into: IntoPython,
{
impl<T: IntoPython> Receiver for RustHandler<FifoChannel, T> {
fn type_name(&self) -> &'static str {
short_type_name::<T>()
}
Expand All @@ -350,10 +343,7 @@ where
}
}

impl<T: IntoRust> Receiver for RustHandler<RingChannel, T>
where
T::Into: IntoPython,
{
impl<T: IntoPython> Receiver for RustHandler<RingChannel, T> {
fn type_name(&self) -> &'static str {
short_type_name::<T>()
}
Expand All @@ -375,14 +365,13 @@ where
}
}

fn rust_handler<H: IntoRust, T: IntoRust>(
fn rust_handler<H: IntoRust, T: IntoPython>(
py: Python,
into_handler: H,
) -> (RustCallback<T::Into>, HandlerImpl<T>)
) -> (RustCallback<T>, HandlerImpl<T::Into>)
where
H::Into: IntoHandler<T::Into>,
<H::Into as IntoHandler<T::Into>>::Handler: Send + Sync,
T::Into: IntoPython,
H::Into: IntoHandler<T>,
<H::Into as IntoHandler<T>>::Handler: Send + Sync,
RustHandler<H, T>: Receiver,
{
let (callback, handler) = into_handler.into_rust().into_handler();
Expand Down Expand Up @@ -418,13 +407,10 @@ fn python_callback<T: IntoPython>(callback: &Bound<PyAny>) -> PyResult<RustCallb
})
}

pub(crate) fn into_handler<T: IntoRust>(
pub(crate) fn into_handler<T: IntoPython>(
py: Python,
obj: Option<&Bound<PyAny>>,
) -> PyResult<(impl IntoHandler<T::Into, Handler = HandlerImpl<T>>, bool)>
where
T::Into: IntoPython,
{
) -> PyResult<(impl IntoHandler<T, Handler = HandlerImpl<T::Into>>, bool)> {
let mut background = false;
let Some(obj) = obj else {
return Ok((rust_handler(py, DefaultHandler), background));
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ impl<T, E: IntoPyErr> IntoPyResult<T> for Result<T, E> {
}
}

pub(crate) trait IntoRust: Send + Sync + 'static {
pub(crate) trait IntoRust: 'static {
type Into;
fn into_rust(self) -> Self::Into;
}

into_rust!(bool, Duration);

pub(crate) trait IntoPython: Sized + Send + 'static {
pub(crate) trait IntoPython: Sized + Send + Sync + 'static {
type Into: IntoPy<PyObject>;
fn into_python(self) -> Self::Into;
fn into_pyobject(self, py: Python) -> PyObject {
Expand Down
Loading