Skip to main content

OpClosure

Struct 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 duplicate 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>

Source§

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> Freeze for OpClosure<T, Op, Id>
where Op: Freeze, Id: Freeze,

§

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

§

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

§

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

§

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

§

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

§

impl<T, Op, Id> UnwindSafe for OpClosure<T, Op, Id>
where Op: UnwindSafe, Id: 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.
§

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

§

fn vzip(self) -> V

Source§

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

Source§

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