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

Relax callback bounds #229

Merged
merged 4 commits into from
May 14, 2024
Merged
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
8 changes: 4 additions & 4 deletions crux_core/src/bridge/request_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
// ANCHOR: resolve_serialized
type ResolveOnceSerialized = Box<dyn FnOnce(&mut dyn erased_serde::Deserializer) + Send>;
type ResolveManySerialized =
Box<dyn Fn(&mut dyn erased_serde::Deserializer) -> Result<(), ()> + Send>;
Box<dyn FnMut(&mut dyn erased_serde::Deserializer) -> Result<(), ()> + Send>;

/// A deserializing version of Resolve
///
Expand Down Expand Up @@ -57,7 +57,7 @@ where
/// which is used by the Bridge implementation.
pub fn serialize<F, Eff>(self, effect: F) -> (Eff, ResolveSerialized)
where
F: Fn(Op) -> Eff,
F: FnOnce(Op) -> Eff,
{
// FIXME should Eff be bound as `Serializable`?
let (operation, resolve) = (self.operation, self.resolve);
Expand All @@ -73,9 +73,9 @@ where
impl<Out> Resolve<Out> {
/// Convert this Resolve into a version which deserializes from bytes, consuming it.
/// The `func` argument is a 'deserializer' converting from bytes into the `Out` type.
fn deserializing<F>(self, func: F) -> ResolveSerialized
fn deserializing<F>(self, mut func: F) -> ResolveSerialized
where
F: (Fn(&mut dyn erased_serde::Deserializer) -> Out) + Send + Sync + 'static,
F: (FnMut(&mut dyn erased_serde::Deserializer) -> Out) + Send + Sync + 'static,
Out: 'static,
{
match self {
Expand Down
2 changes: 1 addition & 1 deletion crux_core/src/capabilities/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<Ev> Capability<Ev> for Compose<Ev> {

fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + Copy + 'static,
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static,
{
Expand Down
2 changes: 1 addition & 1 deletion crux_core/src/capabilities/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<Ev> Capability<Ev> for Render<Ev> {

fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + Copy + 'static,
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static,
{
Expand Down
8 changes: 4 additions & 4 deletions crux_core/src/capability/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
//! pub fn get_in_a_row<F>(&self, number_of_ducks: usize, event: F)
//! where
//! Event: 'static,
//! F: Fn(Vec<Duck>) -> Event + Send + 'static,
//! F: FnOnce(Vec<Duck>) -> Event + Send + 'static,
//! {
//! let ctx = self.context.clone();
//! // Start a shell interaction
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Operation for Never {
///
/// fn map_event<F, NewEvent>(&self, f: F) -> Self::MappedSelf<NewEvent>
/// where
/// F: Fn(NewEvent) -> Ev + Send + Sync + Copy + 'static,
/// F: Fn(NewEvent) -> Ev + Send + Sync + 'static,
/// Ev: 'static,
/// NewEvent: 'static,
/// {
Expand All @@ -289,7 +289,7 @@ pub trait Capability<Ev> {

fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + Copy + 'static,
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static + Send;
}
Expand Down Expand Up @@ -384,7 +384,7 @@ where
///
/// pub fn get<F>(&self, callback: F)
/// where
/// F: Fn(TimeResponse) -> Ev + Send + Sync + 'static,
/// F: FnOnce(TimeResponse) -> Ev + Send + Sync + 'static,
/// {
/// let ctx = self.context.clone();
/// self.context.spawn(async move {
Expand Down
4 changes: 2 additions & 2 deletions crux_core/tests/capability_orchestration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub mod capabilities {

pub fn one<F>(&self, number: usize, event: F)
where
F: Fn(usize) -> E + Send + 'static,
F: FnOnce(usize) -> E + Send + 'static,
E: 'static,
{
let this = Clone::clone(self);
Expand Down Expand Up @@ -154,7 +154,7 @@ pub mod capabilities {

pub fn two<F>(&self, number: usize, event: F)
where
F: Fn(usize) -> E + Send + 'static,
F: FnOnce(usize) -> E + Send + 'static,
E: 'static,
{
let this = Clone::clone(self);
Expand Down
2 changes: 1 addition & 1 deletion crux_core/tests/capability_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod capability {

pub fn fetch_tree<F>(&self, id: usize, ev: F)
where
F: Fn(Vec<usize>) -> Ev + Send + 'static,
F: FnOnce(Vec<usize>) -> Ev + Send + 'static,
{
let (results_tx, results_rx) = async_channel::unbounded::<usize>();
let tasks_tx = self.tasks_tx.clone();
Expand Down
9 changes: 1 addition & 8 deletions crux_http/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ where

/// The remainder of a middleware chain, including the endpoint.
#[allow(missing_debug_implementations)]
#[derive(Copy, Clone)]
pub struct Next<'a> {
next_middleware: &'a [Arc<dyn Middleware>],
endpoint: &'a (dyn (Fn(Request, Client) -> BoxFuture<'static, Result<ResponseAsync>>)
Expand All @@ -90,14 +91,6 @@ pub struct Next<'a> {
+ 'static),
}

impl Clone for Next<'_> {
fn clone(&self) -> Self {
*self
}
}

impl Copy for Next<'_> {}

impl<'a> Next<'a> {
/// Create a new instance
pub fn new(
Expand Down
4 changes: 2 additions & 2 deletions crux_kv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ where
/// `KeyValueOutput::Get(KeyValueResult<Option<Vec<u8>>>)` as payload
pub fn get<F>(&self, key: String, make_event: F)
where
F: Fn(KeyValueResult) -> Ev + Send + Sync + 'static,
F: FnOnce(KeyValueResult) -> Ev + Send + Sync + 'static,
{
self.context.spawn({
let context = self.context.clone();
Expand Down Expand Up @@ -114,7 +114,7 @@ where
/// Will dispatch the event with a `KeyValueOutput::Set { result: KeyValueResult<()> }` as payload
pub fn set<F>(&self, key: String, value: Vec<u8>, make_event: F)
where
F: Fn(KeyValueResult) -> Ev + Send + Sync + 'static,
F: FnOnce(KeyValueResult) -> Ev + Send + Sync + 'static,
{
self.context.spawn({
let context = self.context.clone();
Expand Down
4 changes: 2 additions & 2 deletions crux_macros/src/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl ToTokens for CapabilityStructReceiver {

fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + Copy + 'static,
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static,
{
Expand Down Expand Up @@ -115,7 +115,7 @@ mod tests {
type MappedSelf<MappedEv> = Render<MappedEv>;
fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + Copy + 'static,
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static,
{
Expand Down
2 changes: 1 addition & 1 deletion crux_platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ where

pub fn get<F>(&self, callback: F)
where
F: Fn(PlatformResponse) -> Ev + Send + Sync + 'static,
F: FnOnce(PlatformResponse) -> Ev + Send + Sync + 'static,
{
self.context.spawn({
let context = self.context.clone();
Expand Down
6 changes: 3 additions & 3 deletions crux_time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
/// wrapped in the event produced by the `callback`.
pub fn now<F>(&self, callback: F)
where
F: Fn(TimeResponse) -> Ev + Send + Sync + 'static,
F: FnOnce(TimeResponse) -> Ev + Send + Sync + 'static,
{
self.context.spawn({
let context = self.context.clone();
Expand All @@ -86,7 +86,7 @@ where
/// Ask to receive a notification when the specified [`Instant`] has arrived.
pub fn notify_at<F>(&self, instant: Instant, callback: F)
where
F: Fn(TimeResponse) -> Ev + Send + Sync + 'static,
F: FnOnce(TimeResponse) -> Ev + Send + Sync + 'static,
{
self.context.spawn({
let context = self.context.clone();
Expand All @@ -109,7 +109,7 @@ where
/// Ask to receive a notification when the specified duration has elapsed.
pub fn notify_after<F>(&self, duration: Duration, callback: F)
where
F: Fn(TimeResponse) -> Ev + Send + Sync + 'static,
F: FnOnce(TimeResponse) -> Ev + Send + Sync + 'static,
{
self.context.spawn({
let context = self.context.clone();
Expand Down
7 changes: 3 additions & 4 deletions crux_time/tests/time_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ mod shared {

/// Helper to create an event with additional user info captured
/// this is effectively partially applying the event constructor
pub fn event_with_user_info<E, F, U, T>(user_info: U, make_event: F) -> impl Fn(T) -> E
pub fn event_with_user_info<E, F, U, T>(user_info: U, make_event: F) -> impl FnOnce(T) -> E
where
F: Fn(U, T) -> E,
U: Clone,
F: FnOnce(U, T) -> E,
{
move |response| make_event(user_info.clone(), response)
move |response| make_event(user_info, response)
}
}

Expand Down
8 changes: 4 additions & 4 deletions doctest_support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod compose {

pub fn one<F>(&self, number: usize, event: F)
where
F: Fn(usize) -> E + Send + 'static,
F: FnOnce(usize) -> E + Send + 'static,
E: 'static,
{
let this = Clone::clone(self);
Expand Down Expand Up @@ -66,7 +66,7 @@ pub mod compose {

fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + Copy + 'static,
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static,
{
Expand Down Expand Up @@ -110,7 +110,7 @@ pub mod compose {

pub fn two<F>(&self, number: usize, event: F)
where
F: Fn(usize) -> E + Send + 'static,
F: FnOnce(usize) -> E + Send + 'static,
E: 'static,
{
let this = Clone::clone(self);
Expand Down Expand Up @@ -140,7 +140,7 @@ pub mod compose {

fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + Copy + 'static,
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static,
{
Expand Down
2 changes: 1 addition & 1 deletion examples/notes/shared/src/capabilities/pub_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ where

pub fn subscribe<F>(&self, make_event: F)
where
F: Fn(Vec<u8>) -> Ev + Clone + Send + 'static,
F: FnOnce(Vec<u8>) -> Ev + Clone + Send + 'static,
{
self.context.spawn({
let context = self.context.clone();
Expand Down
2 changes: 1 addition & 1 deletion examples/notes/shared/src/capabilities/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where

pub fn start<F>(&self, id: u64, millis: usize, make_event: F)
where
F: Fn(TimerOutput) -> Ev + Clone + Send + 'static,
F: FnOnce(TimerOutput) -> Ev + Clone + Send + 'static,
{
self.context.spawn({
let context = self.context.clone();
Expand Down
Loading