Skip to content
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
42 changes: 33 additions & 9 deletions doublets/src/data/handler.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
use crate::Link;
use data::{Flow, LinkType};
use std::{marker::PhantomData, mem::MaybeUninit, ops::Try};

Check warning on line 3 in doublets/src/data/handler.rs

View workflow job for this annotation

GitHub Actions / Benchmark

unused import: `mem::MaybeUninit`

/// Trait for handler return types that can be converted to `Try<Output = ()>`.
/// This allows handlers to return `()` (implying `Flow::Continue`) or explicit `Flow` values.
pub trait HandlerResult {
type Try: Try<Output = ()>;

fn try_it(self) -> Self::Try;
}

impl HandlerResult for () {
type Try = Flow;

fn try_it(self) -> Self::Try {
Flow::Continue
}
}

impl<T: Try<Output = ()>> HandlerResult for T {

Check failure on line 21 in doublets/src/data/handler.rs

View workflow job for this annotation

GitHub Actions / Benchmark

conflicting implementations of trait `data::handler::HandlerResult` for type `()`
type Try = T;

fn try_it(self) -> Self::Try {
self
}
}

pub trait Handler<T, R>: FnMut(Link<T>, Link<T>) -> R
where
T: LinkType,
R: Try<Output = ()>,
R: HandlerResult,
{
fn fuse(self) -> Fuse<T, Self, R>
where
Expand All @@ -18,7 +42,7 @@
impl<T, R, All> Handler<T, R> for All
where
T: LinkType,
R: Try<Output = ()>,
R: HandlerResult,
All: FnMut(Link<T>, Link<T>) -> R,
{
}
Expand All @@ -27,7 +51,7 @@
where
T: LinkType,
H: Handler<T, R>,
R: Try<Output = ()>,
R: HandlerResult,
{
handler: H,
done: bool,
Expand All @@ -38,7 +62,7 @@
where
T: LinkType,
F: FnMut(Link<T>, Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
{
pub fn new(handler: F) -> Self {
Self {
Expand All @@ -53,7 +77,7 @@
where
T: LinkType,
H: Handler<T, R>,
R: Try<Output = ()>,
R: HandlerResult,
{
fn from(handler: H) -> Self {
Self::new(handler)
Expand All @@ -63,27 +87,27 @@
impl<T, H, R> FnOnce<(Link<T>, Link<T>)> for Fuse<T, H, R>
where
H: FnMut(Link<T>, Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
T: LinkType,
{
type Output = Flow;

extern "rust-call" fn call_once(self, args: (Link<T>, Link<T>)) -> Self::Output {
self.handler.call_once(args).branch().into()
self.handler.call_once(args).try_it().branch().into()
}
}

impl<T, H, R> FnMut<(Link<T>, Link<T>)> for Fuse<T, H, R>
where
T: LinkType,
H: Handler<T, R>,
R: Try<Output = ()>,
R: HandlerResult,
{
extern "rust-call" fn call_mut(&mut self, args: (Link<T>, Link<T>)) -> Self::Output {
if self.done {
Flow::Break
} else {
let result = self.handler.call_mut(args);
let result = self.handler.call_mut(args).try_it();
if result.branch().is_break() {
self.done = false;
Flow::Break
Expand Down
2 changes: 1 addition & 1 deletion doublets/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod traits;

pub use doublet::Doublet;
pub use error::Error;
pub use handler::{Fuse, Handler};
pub use handler::{Fuse, Handler, HandlerResult};
pub use link::Link;
pub use traits::{Doublets, DoubletsExt, Links, ReadHandler, WriteHandler};

Expand Down
58 changes: 29 additions & 29 deletions doublets/src/data/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bumpalo::Bump;

Check warning on line 1 in doublets/src/data/traits.rs

View workflow job for this annotation

GitHub Actions / Benchmark

unused import: `bumpalo::Bump`
#[cfg(feature = "rayon")]
use rayon::prelude::*;
use std::{
Expand All @@ -6,7 +6,7 @@
ops::{ControlFlow, Try},
};

use crate::{Error, Fuse, Link};
use crate::{Error, Fuse, HandlerResult, Link};
use data::{Flow, LinkType, LinksConstants, ToQuery};

pub type ReadHandler<'a, T> = &'a mut dyn FnMut(Link<T>) -> Flow;
Expand Down Expand Up @@ -53,21 +53,21 @@
&mut self,
query: impl ToQuery<T>,
mut handler: F,
) -> Result<R, Error<T>>
) -> Result<R::Try, Error<T>>
where
F: FnMut(Link<T>, Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
Self: Sized,
{
let mut output = R::from_output(());
let mut output = R::Try::from_output(());
let query = query.to_query();

self.create_links(
&query[..],
&mut |before, after| match handler(before, after).branch() {
&mut |before, after| match handler(before, after).try_it().branch() {
ControlFlow::Continue(_) => Flow::Continue,
ControlFlow::Break(residual) => {
output = R::from_residual(residual);
output = R::Try::from_residual(residual);
Flow::Break
}
},
Expand All @@ -87,10 +87,10 @@
.map(|_| index)
}

fn create_with<F, R>(&mut self, handler: F) -> Result<R, Error<T>>
fn create_with<F, R>(&mut self, handler: F) -> Result<R::Try, Error<T>>
where
F: FnMut(Link<T>, Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
Self: Sized,
{
self.create_by_with([], handler)
Expand All @@ -103,30 +103,30 @@
self.create_by([])
}

fn each_by<F, R>(&self, query: impl ToQuery<T>, mut handler: F) -> R
fn each_by<F, R>(&self, query: impl ToQuery<T>, mut handler: F) -> R::Try
where
F: FnMut(Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
Self: Sized,
{
let mut output = R::from_output(());
let mut output = R::Try::from_output(());
let query = query.to_query();

self.each_links(&query[..], &mut |link| match handler(link).branch() {
self.each_links(&query[..], &mut |link| match handler(link).try_it().branch() {
ControlFlow::Continue(_) => Flow::Continue,
ControlFlow::Break(residual) => {
output = R::from_residual(residual);
output = R::Try::from_residual(residual);
Flow::Break
}
});

output
}

fn each<F, R>(&self, handler: F) -> R
fn each<F, R>(&self, handler: F) -> R::Try
where
F: FnMut(Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
Self: Sized,
{
self.each_by([], handler)
Expand All @@ -137,23 +137,23 @@
query: impl ToQuery<T>,
change: impl ToQuery<T>,
mut handler: H,
) -> Result<R, Error<T>>
) -> Result<R::Try, Error<T>>
where
H: FnMut(Link<T>, Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
Self: Sized,
{
let mut output = R::from_output(());
let mut output = R::Try::from_output(());
let query = query.to_query();
let change = change.to_query();

self.update_links(
&query[..],
&change[..],
&mut |before, after| match handler(before, after).branch() {
&mut |before, after| match handler(before, after).try_it().branch() {
ControlFlow::Continue(_) => Flow::Continue,
ControlFlow::Break(residual) => {
output = R::from_residual(residual);
output = R::Try::from_residual(residual);
Flow::Break
}
},
Expand All @@ -179,10 +179,10 @@
source: T,
target: T,
handler: F,
) -> Result<R, Error<T>>
) -> Result<R::Try, Error<T>>
where
F: FnMut(Link<T>, Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
Self: Sized,
{
self.update_by_with([index], [index, source, target], handler)
Expand All @@ -199,21 +199,21 @@
&mut self,
query: impl ToQuery<T>,
mut handler: F,
) -> Result<R, Error<T>>
) -> Result<R::Try, Error<T>>
where
F: FnMut(Link<T>, Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
Self: Sized,
{
let mut output = R::from_output(());
let mut output = R::Try::from_output(());
let query = query.to_query();

self.delete_links(
&query[..],
&mut |before, after| match handler(before, after).branch() {
&mut |before, after| match handler(before, after).try_it().branch() {
ControlFlow::Continue(_) => Flow::Continue,
ControlFlow::Break(residual) => {
output = R::from_residual(residual);
output = R::Try::from_residual(residual);
Flow::Break
}
},
Expand All @@ -233,10 +233,10 @@
.map(|_| result)
}

fn delete_with<F, R>(&mut self, index: T, handler: F) -> Result<R, Error<T>>
fn delete_with<F, R>(&mut self, index: T, handler: F) -> Result<R::Try, Error<T>>
where
F: FnMut(Link<T>, Link<T>) -> R,
R: Try<Output = ()>,
R: HandlerResult,
Self: Sized,
{
self.delete_by_with([index], handler)
Expand Down
2 changes: 1 addition & 1 deletion doublets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ pub mod mem;

pub use self::mem::{parts, split, unit};

pub use self::data::{Doublet, Doublets, DoubletsExt, Error, Fuse, Handler, Link, Links};
pub use self::data::{Doublet, Doublets, DoubletsExt, Error, Fuse, Handler, HandlerResult, Link, Links};
pub(crate) use self::data::{Error as LinksError, ReadHandler, WriteHandler};
Loading
Loading