Struct State

Source
pub struct State<S, A> { /* private fields */ }
Expand description

The State monad represents a stateful computation.

Implementations§

Source§

impl<S, A> State<S, A>
where S: 'static, A: Clone + 'static,

Source

pub fn value(a: A) -> State<S, A>

Creates a new State with a constant value.

This method creates a State that, when run, will return the provided value and the unchanged state.

§Arguments
  • a - The value to be returned by the State
§Returns
  • State<S, A> - A new State that returns the provided value
§Examples
use prop_check_rs::state::State;
let state = State::<i32, String>::value("hello".to_string());
let (value, new_state) = state.run(42);
assert_eq!(value, "hello");
assert_eq!(new_state, 42); // State is unchanged
Source

pub fn new<T, B, F>(f: F) -> State<T, B>
where F: Fn(T) -> (B, T) + 'static,

Creates a new State with a custom state transformation function.

This is the core constructor for State, allowing you to define exactly how the state should be transformed and what value should be produced.

§Arguments
  • f - A function that takes a state and returns a tuple of (value, new_state)
§Returns
  • State<T, B> - A new State that applies the provided function
§Type Parameters
  • T - The type of the state
  • B - The type of the value produced
  • F - The type of the function
Source

pub fn pure<B>(b: B) -> State<S, B>
where B: Clone + 'static,

Creates a new State with a constant value (alias for value).

This method is functionally identical to value but is named to align with the functional programming concept of “pure” or “return”.

§Arguments
  • b - The value to be returned by the State
§Returns
  • State<S, B> - A new State that returns the provided value
§Type Parameters
  • B - The type of the value to be returned
Source

pub fn run(self, s: S) -> (A, S)

Executes the State with the provided initial state.

This method runs the State computation with the given state and returns both the resulting value and the final state.

§Arguments
  • s - The initial state
§Returns
  • (A, S) - A tuple containing the resulting value and the final state
Source

pub fn map<B, F>(self, f: F) -> State<S, B>
where F: Fn(A) -> B + 'static, B: Clone + 'static,

Transforms the value produced by this State using a function.

This method allows you to transform the value produced by a State without affecting how the state itself is transformed.

§Arguments
  • f - A function that transforms the value
§Returns
  • State<S, B> - A new State that produces the transformed value
§Type Parameters
  • B - The type of the transformed value
  • F - The type of the transformation function
Source

pub fn flat_map<B, F>(self, f: F) -> State<S, B>
where F: Fn(A) -> State<S, B> + 'static, B: Clone + 'static,

Chains this State with a function that returns another State.

This method allows for sequential composition of stateful computations. The function f is applied to the value produced by this State, and the resulting State is then run with the updated state.

§Arguments
  • f - A function that takes the value from this State and returns a new State
§Returns
  • State<S, B> - A new State representing the sequential composition
§Type Parameters
  • B - The type of the value produced by the resulting State
  • F - The type of the function
Source

pub fn and_then<B>(self, sb: State<S, B>) -> State<S, (A, B)>
where A: Clone, B: Clone + 'static,

Combines this State with another State, producing both values.

This method runs this State, then runs the provided State with the updated state, and returns both values as a tuple.

§Arguments
  • sb - Another State to run after this one
§Returns
  • State<S, (A, B)> - A new State that produces both values as a tuple
§Type Parameters
  • B - The type of the value produced by the second State
Source

pub fn get<T>() -> State<T, T>
where T: Clone,

Creates a State that returns the current state as its value.

This method is useful for accessing the current state without modifying it.

§Returns
  • State<T, T> - A State that returns the current state as its value
§Type Parameters
  • T - The type of the state
Source

pub fn set<T>(t: T) -> State<T, ()>
where T: Clone + 'static,

Creates a State that replaces the current state with a new value.

This method is useful for setting the state to a specific value, regardless of its current value.

§Arguments
  • t - The new state value
§Returns
  • State<T, ()> - A State that sets the state to the provided value
§Type Parameters
  • T - The type of the state
Source

pub fn modify<T, F>(f: F) -> State<T, ()>
where F: Fn(T) -> T + 'static, T: Clone + 'static,

Creates a State that modifies the current state using a function.

This method allows you to transform the state based on its current value.

§Arguments
  • f - A function that transforms the state
§Returns
  • State<T, ()> - A State that modifies the state using the provided function
§Type Parameters
  • T - The type of the state
  • F - The type of the transformation function
Source

pub fn sequence(sas: Vec<State<S, A>>) -> State<S, Vec<A>>

Executes a sequence of States and collects their results into a vector.

This method runs each State in the provided vector in sequence, threading the state through each computation, and collects all the resulting values.

§Arguments
  • sas - A vector of States to execute in sequence
§Returns
  • State<S, Vec<A>> - A State that produces a vector of all the values

Trait Implementations§

Source§

impl<S, A> Clone for State<S, A>
where S: 'static, A: Clone + 'static,

Source§

fn clone(&self) -> Self

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<S, A> Debug for State<S, A>

Source§

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

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

impl<S, A> Default for State<S, A>
where S: Default + 'static, A: Default + Clone + 'static,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<S, A> Freeze for State<S, A>

§

impl<S, A> !RefUnwindSafe for State<S, A>

§

impl<S, A> !Send for State<S, A>

§

impl<S, A> !Sync for State<S, A>

§

impl<S, A> Unpin for State<S, A>

§

impl<S, A> !UnwindSafe for State<S, A>

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

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

Source§

fn vzip(self) -> V