Mutable

Trait Mutable 

Source
pub trait Mutable
where Self: Sized,
{ type Mutation: Clone; // Required method fn cmp(&self, other: &Self) -> Vec<Self::Mutation>; // Provided method fn update(&mut self, other: Self) -> Vec<Self::Mutation> { ... } }
Expand description

An item which state may change

Allows to keep track of the changes of the item

§Simple Mutation

#[derive(Mutable, Clone, Debug, PartialEq)]
 struct Item {
     size: usize,
}

let a0 = Item{ size: 0};
let a1 = Item{ size: 1};

assert_eq!(a0.cmp(&a1), vec![ItemMutation::Size((0, 1))]);

§Nested mutation

#[derive(Mutable)]
struct Simple {
    size: usize,
}

#[derive(Mutable)]
struct Complex{
    id: String,
    value: Simple,
}

let mut c0 = Complex{ id: "a".to_string(), value: Simple { size: 32 } };
let c1 = Complex{ id: "b".to_string(), value: Simple { size: 64 } };

assert_eq!(c0.update(c1), vec![
    ComplexMutation::Id(("a".to_string(), "b".to_string())),
    ComplexMutation::Value(SimpleMutation::Size((32, 64)))
]);

§Vec mutation

#[derive(Mutable, PartialEq, SoftEq, Debug, Clone)]
struct Item {
    #[softeq(uid)]
    id: String,
    value: u32,
}

let a0 = Item{id: "a".to_string(), value: 0};
let a1 = Item{id: "a".to_string(), value: 1};
let b = Item{id: "b".to_string(), value: 0};

let mut v0 = vec![a0];
let v1 = vec![a1, b];

assert_eq!(v0.update(v1), vec![
    VecMutation::Update("a".to_string(), ItemMutation::Value((0, 1))),
    VecMutation::Insertion("b".to_string())
])

§Mutation generation

When derived through mutable_derive::Mutable, the mutation type is generated as follow:

The mutation type is an enum, for which each variant correspond to a field of the struct. Each of them as only one element, which is the mutation type of the attribute type

Example of result:

struct Foo{
    size: usize
}

// Generated
enum FooMutation{
    Size((usize, usize)),
}

struct Item{
    id: u32,
    name: String,
    value: Foo,
}

// Generated
enum ItemMutation{
    Id((u32, u32)),
    Name((String, String)),
    Value(FooMutation),
}

Required Associated Types§

Source

type Mutation: Clone

Type describing any changes that may happen to the item

Required Methods§

Source

fn cmp(&self, other: &Self) -> Vec<Self::Mutation>

Compare self and other, and returns the list of changes to go from self to other. The order of the mutation in the returned vec is undefined
This should not be used outside of the Mutable implementation

Provided Methods§

Source

fn update(&mut self, other: Self) -> Vec<Self::Mutation>

Mutate self into other, and returns the list of all mutations. The order of the mutation in the returned vec is undefined

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Mutable for bool

Source§

type Mutation = (bool, bool)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for f32

Source§

type Mutation = (f32, f32)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for f64

Source§

type Mutation = (f64, f64)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for i8

Source§

type Mutation = (i8, i8)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for i16

Source§

type Mutation = (i16, i16)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for i32

Source§

type Mutation = (i32, i32)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for i64

Source§

type Mutation = (i64, i64)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for i128

Source§

type Mutation = (i128, i128)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for isize

Source§

type Mutation = (isize, isize)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for u8

Source§

type Mutation = (u8, u8)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for u16

Source§

type Mutation = (u16, u16)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for u32

Source§

type Mutation = (u32, u32)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for u64

Source§

type Mutation = (u64, u64)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for u128

Source§

type Mutation = (u128, u128)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for ()

Source§

type Mutation = ()

Source§

fn cmp(&self, _: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for usize

Source§

type Mutation = (usize, usize)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl Mutable for String

Source§

type Mutation = (String, String)

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl<T: Mutable + Clone + SoftEq> Mutable for Vec<T>

Source§

type Mutation = VecMutation<T>

Source§

fn cmp(&self, new: &Self) -> Vec<Self::Mutation>

Source§

impl<T: Mutable + Clone> Mutable for Option<T>

Source§

type Mutation = OptionMutation<T>

Source§

fn cmp(&self, other: &Self) -> Vec<Self::Mutation>

Implementors§