Skip to main content

Builder

Struct Builder 

Source
pub struct Builder<T> { /* private fields */ }
Expand description

With this builder you can create a fluent API around an object.

§Example

use generic_builders::immutable::Builder;

#[derive(Debug, PartialEq)]
struct Test {
    field1: i32,
    field2: i32,
}

assert_eq!(
    Builder::new(Test {field1: 0, field2: 0})
        .update(|v| Test {field1: 1, field2: v.field2})
        .update_if(|v| v.field1 > 0, |v| Test {field1: v.field1, field2: v.field1})
        .update_if_some(|_| Some(1), |v, n| Test {field1: v.field1 + n, field2: v.field2 + n})
        .build(),
    Test {field1: 2, field2: 2})

Implementations§

Source§

impl<T> Builder<T>

Source

pub fn new(object: T) -> Self

Source

pub fn build(self) -> T

Source

pub fn update<F>(self, set: F) -> Builder<T>
where F: Fn(T) -> T,

Source

pub fn update_if<F, P>(self, predicate: P, set: F) -> Builder<T>
where F: Fn(T) -> T, P: Fn(&T) -> bool,

Source

pub fn update_if_some<F, P, U>(self, maybe: P, set: F) -> Builder<T>
where F: Fn(T, &U) -> T, P: Fn(&T) -> Option<U>,

Auto Trait Implementations§

§

impl<T> Freeze for Builder<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Builder<T>
where T: RefUnwindSafe,

§

impl<T> Send for Builder<T>
where T: Send,

§

impl<T> Sync for Builder<T>
where T: Sync,

§

impl<T> Unpin for Builder<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Builder<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Builder<T>
where T: 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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.