Struct nekolib::utils::OpClosure

source ·
pub struct OpClosure<T, Op: Fn(T, T) -> T, Id: Fn() -> T>(/* private fields */);
Expand description

任意の結合的な演算を持つ。

結合性については使用者側に保証してもらう。

Examples

use std::cell::RefCell;

use nekolib::traits::{Magma, Identity};
use nekolib::utils::OpClosure;

let runtime_mod = 10;
let op_memo = RefCell::new(vec![]);
let id_times = RefCell::new(0);
let op_cl = OpClosure::new(|x: i32, y| {
    op_memo.borrow_mut().push((x, y));
    (x + y).rem_euclid(runtime_mod)
}, || {
    *id_times.borrow_mut() += 1;
    0
});

assert_eq!(op_cl.op(1, 9), 0);
assert_eq!(op_cl.op(3, 8), 1);
assert_eq!(op_cl.op(-5, -3), 2);
assert_eq!(op_cl.id(), 0);

assert_eq!(*op_memo.borrow(), [(1, 9), (3, 8), (-5, -3)]);
assert_eq!(*id_times.borrow(), 1);

Implementations§

source§

impl<T: Eq, Op: Fn(T, T) -> T, Id: Fn() -> T> OpClosure<T, Op, Id>

source

pub fn new(op: Op, id: Id) -> Self

Trait Implementations§

source§

impl<T: Clone, Op: Clone + Fn(T, T) -> T, Id: Clone + Fn() -> T> Clone for OpClosure<T, Op, Id>

source§

fn clone(&self) -> OpClosure<T, Op, Id>

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<T: Debug, Op: Debug + Fn(T, T) -> T, Id: Debug + Fn() -> T> Debug for OpClosure<T, Op, Id>

source§

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

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

impl<T: Default, Op: Default + Fn(T, T) -> T, Id: Default + Fn() -> T> Default for OpClosure<T, Op, Id>

source§

fn default() -> OpClosure<T, Op, Id>

Returns the “default value” for a type. Read more
source§

impl<T: Eq, Op: Fn(T, T) -> T, Id: Fn() -> T> Identity for OpClosure<T, Op, Id>

source§

fn id(&self) -> T

単位元を返す。
source§

impl<T: Eq, Op: Fn(T, T) -> T, Id: Fn() -> T> Magma for OpClosure<T, Op, Id>

§

type Set = T

集合 $M$ に対応する型。
source§

fn op(&self, lhs: T, rhs: T) -> T

$x \circ y$ を返す。
source§

impl<T: Eq, Op: Fn(T, T) -> T, Id: Fn() -> T> Associative for OpClosure<T, Op, Id>

source§

impl<T: Copy, Op: Copy + Fn(T, T) -> T, Id: Copy + Fn() -> T> Copy for OpClosure<T, Op, Id>

Auto Trait Implementations§

§

impl<T, Op, Id> RefUnwindSafe for OpClosure<T, Op, Id>where Id: RefUnwindSafe, Op: RefUnwindSafe,

§

impl<T, Op, Id> Send for OpClosure<T, Op, Id>where Id: Send, Op: Send,

§

impl<T, Op, Id> Sync for OpClosure<T, Op, Id>where Id: Sync, Op: Sync,

§

impl<T, Op, Id> Unpin for OpClosure<T, Op, Id>where Id: Unpin, Op: Unpin,

§

impl<T, Op, Id> UnwindSafe for OpClosure<T, Op, Id>where Id: UnwindSafe, Op: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<G> Monoid for Gwhere G: Identity + Semigroup,

source§

impl<G> Semigroup for Gwhere G: Associative + Magma,