Struct Closure

Source
pub struct Closure<Capture, In, Out> { /* private fields */ }
Expand description

Closure strictly separating the captured data from the function, and hence, having two components:

  • Capture is any captured data,
  • fn(&Capture, In) -> Out is the transformation.

It represents the transformation In -> Out.

Note that, unlike trait objects of fn-traits, Closure auto-implements Clone given that captured data is cloneable.

§Example

use orx_closure::*;

let name = String::from("morgana");

// nth_char: Closure<String, usize, Option<char>>
let nth_char = Capture(name).fun(|n, i| n.chars().nth(i));

assert_eq!(Some('m'), nth_char.call(0));

// alternatively
let fun = nth_char.as_fn();
assert_eq!(Some('g'), fun(3));

Implementations§

Source§

impl<Capture, In, Out> Closure<Capture, In, Out>

Source

pub fn call(&self, input: In) -> Out

Calls the closure with the given input.

§Example
use orx_closure::Capture;

let base = 2;
let modulo = Capture(base).fun(|b, n| n % b);

assert_eq!(0, modulo.call(42));
assert_eq!(1, modulo.call(7));
Source

pub fn captured_data(&self) -> &Capture

Returns a reference to the captured data.

Source

pub fn into_captured_data(self) -> Capture

Consumes the closure and returns back the captured data.

use orx_closure::Capture;

struct ExpensiveData(Vec<i32>);

let data = ExpensiveData(vec![0, 1, 2]);

let get_number = Capture(data).fun(|data, i| 42 + data.0[i]);

assert_eq!(42, get_number.call(0));
assert_eq!(44, get_number.call(2));

let _data: ExpensiveData = get_number.into_captured_data();
Source

pub fn as_fn(&self) -> impl Fn(In) -> Out + '_

Returns the closure as an impl Fn(In) -> Out struct, allowing the convenience

  • to avoid the call method,
  • or pass the closure to functions accepting a function generic over the Fn.
§Example
use orx_closure::Capture;

let base = 2;
let modulo = Capture(base).fun(|b, n| n % b);

// with call method
assert_eq!(0, modulo.call(42));
assert_eq!(1, modulo.call(7));

// getting it as 'Fn' and directly calling the closure
let module_fn = modulo.as_fn();
assert_eq!(0, module_fn(42));
assert_eq!(1, module_fn(7));
Source§

impl<Capture, In, Out> Closure<Capture, In, Out>

Source

pub fn into_oneof2_var1<Var2>(self) -> ClosureOneOf2<Capture, Var2, In, Out>

Transforms Closure<C1, In, Out> into the more general ClosureOneOf2<C1, C2, In, Out> for any C2.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant1(closure: Closure<(), Edge, bool>) -> Self {
        // transforms        : Closure<(), Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var1())
    }
}
Source

pub fn into_oneof2_var2<Var1>(self) -> ClosureOneOf2<Var1, Capture, In, Out>

Transforms Closure<C2, In, Out> into the more general ClosureOneOf2<C1, C2, In, Out> for any C1.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant2(closure: Closure<Vec<HashSet<Node>>, Edge, bool>) -> Self {
        // transforms        : Closure<Vec<HashSet<Node>>, Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var2())
    }
}
Source§

impl<Capture, In, Out> Closure<Capture, In, Out>

Source

pub fn into_oneof3_var1<Var2, Var3>( self, ) -> ClosureOneOf3<Capture, Var2, Var3, In, Out>

Transforms Closure<C1, In, Out> into the more general ClosureOneOf3<C1, C2, C3, In, Out> for any C2 and C3.

The example below illustrates the usage of the closure over two possible types of captures; however, ClosureOneOf3 is only a generalization of the below for three different capture types.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant1(closure: Closure<(), Edge, bool>) -> Self {
        // transforms        : Closure<(), Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var1())
    }
}
Source

pub fn into_oneof3_var2<Var1, Var3>( self, ) -> ClosureOneOf3<Var1, Capture, Var3, In, Out>

Transforms Closure<C2, In, Out> into the more general ClosureOneOf3<C1, C2, C3, In, Out> for any C1 and C3.

The example below illustrates the usage of the closure over two possible types of captures; however, ClosureOneOf3 is only a generalization of the below for three different capture types.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant2(closure: Closure<Vec<HashSet<Node>>, Edge, bool>) -> Self {
        // transforms        : Closure<Vec<HashSet<Node>>, Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var2())
    }
}
Source

pub fn into_oneof3_var3<Var1, Var2>( self, ) -> ClosureOneOf3<Var1, Var2, Capture, In, Out>

Transforms Closure<C3, In, Out> into the more general ClosureOneOf3<C1, C2, C3, In, Out> for any C1 and C2.

The example below illustrates the usage of the closure over two possible types of captures; however, ClosureOneOf3 is only a generalization of the below for three different capture types.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant2(closure: Closure<Vec<HashSet<Node>>, Edge, bool>) -> Self {
        // transforms        : Closure<Vec<HashSet<Node>>, Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var2())
    }
}
Source§

impl<Capture, In, Out> Closure<Capture, In, Out>

Source

pub fn into_oneof4_var1<Var2, Var3, Var4>( self, ) -> ClosureOneOf4<Capture, Var2, Var3, Var4, In, Out>

Transforms Closure<C1, In, Out> into the more general ClosureOneOf4<C1, C2, C3, C4, In, Out> for any C2, C3 and C4.

The example below illustrates the usage of the closure over two possible types of captures; however, ClosureOneOf3 is only a generalization of the below for three different capture types.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant1(closure: Closure<(), Edge, bool>) -> Self {
        // transforms        : Closure<(), Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var1())
    }
}
Source

pub fn into_oneof4_var2<Var1, Var3, Var4>( self, ) -> ClosureOneOf4<Var1, Capture, Var3, Var4, In, Out>

Transforms Closure<C2, In, Out> into the more general ClosureOneOf4<C1, C2, C3, C4, In, Out> for any C1, C3 and C4.

The example below illustrates the usage of the closure over two possible types of captures; however, ClosureOneOf4 is only a generalization of the below for three different capture types.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant2(closure: Closure<Vec<HashSet<Node>>, Edge, bool>) -> Self {
        // transforms        : Closure<Vec<HashSet<Node>>, Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var2())
    }
}
Source

pub fn into_oneof4_var3<Var1, Var2, Var4>( self, ) -> ClosureOneOf4<Var1, Var2, Capture, Var4, In, Out>

Transforms Closure<C3, In, Out> into the more general ClosureOneOf4<C1, C2, C3, C4, In, Out> for any C1, C2 and C4.

The example below illustrates the usage of the closure over two possible types of captures; however, ClosureOneOf4 is only a generalization of the below for three different capture types.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant2(closure: Closure<Vec<HashSet<Node>>, Edge, bool>) -> Self {
        // transforms        : Closure<Vec<HashSet<Node>>, Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var2())
    }
}
Source

pub fn into_oneof4_var4<Var1, Var2, Var3>( self, ) -> ClosureOneOf4<Var1, Var2, Var3, Capture, In, Out>

Transforms Closure<C4, In, Out> into the more general ClosureOneOf4<C1, C2, C3, C4, In, Out> for any C1, C2 and C3.

The example below illustrates the usage of the closure over two possible types of captures; however, ClosureOneOf4 is only a generalization of the below for three different capture types.

§Example
use orx_closure::*;
use std::collections::HashSet;

type Node = usize; // for brevity
type Edge = (Node, Node); // for brevity

// captures either () or Vec<HashSet<Node>>
type PrecedenceClosure = ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>;

struct Precedence(PrecedenceClosure);

impl Precedence {
    fn new_variant2(closure: Closure<Vec<HashSet<Node>>, Edge, bool>) -> Self {
        // transforms        : Closure<Vec<HashSet<Node>>, Edge, bool>
        // into more general : ClosureOneOf2<(), Vec<HashSet<Node>>, Edge, bool>
        Self(closure.into_oneof2_var2())
    }
}

Trait Implementations§

Source§

impl<Capture: Clone, In: Clone, Out: Clone> Clone for Closure<Capture, In, Out>

Source§

fn clone(&self) -> Closure<Capture, In, Out>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Capture: Debug, In, Out> Debug for Closure<Capture, In, Out>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Capture, In, Out> Fun<In, Out> for Closure<Capture, In, Out>

Source§

fn call(&self, input: In) -> Out

Calls the function with the given input and returns the produced output.

Auto Trait Implementations§

§

impl<Capture, In, Out> Freeze for Closure<Capture, In, Out>
where Capture: Freeze,

§

impl<Capture, In, Out> RefUnwindSafe for Closure<Capture, In, Out>
where Capture: RefUnwindSafe,

§

impl<Capture, In, Out> Send for Closure<Capture, In, Out>
where Capture: Send,

§

impl<Capture, In, Out> Sync for Closure<Capture, In, Out>
where Capture: Sync,

§

impl<Capture, In, Out> Unpin for Closure<Capture, In, Out>
where Capture: Unpin,

§

impl<Capture, In, Out> UnwindSafe for Closure<Capture, In, Out>
where Capture: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.