honeycomb_core::attributes

Trait AttributeUpdate

Source
pub trait AttributeUpdate:
    Sized
    + Send
    + Sync
    + Clone
    + Copy {
    // Required methods
    fn merge(attr1: Self, attr2: Self) -> Self;
    fn split(attr: Self) -> (Self, Self);

    // Provided methods
    fn merge_incomplete(attr: Self) -> CMapResult<Self> { ... }
    fn merge_from_none() -> CMapResult<Self> { ... }
    fn split_from_none() -> CMapResult<(Self, Self)> { ... }
}
Expand description

§Generic attribute trait

This trait is used to describe how a values of a given attribute are merged and split during sewing and unsewing operations.

§Example

For an intensive property of a system (e.g. a temperature), an implementation would look like this:

use honeycomb_core::prelude::{AttributeUpdate, CMapResult};

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Temperature {
    pub val: f32
}

impl AttributeUpdate for Temperature {
    fn merge(attr1: Self, attr2: Self) -> Self {
        Temperature { val: (attr1.val + attr2.val) / 2.0 }
    }

    fn split(attr: Self) -> (Self, Self) {
        (attr, attr)
    }

    fn merge_incomplete(attr: Self) -> CMapResult<Self> {
        Ok(Temperature { val: attr.val / 2.0 })
    }

    fn merge_from_none() -> CMapResult<Self> {
        Ok(Temperature { val: 0.0 })
    }
}

let t1 = Temperature { val: 273.0 };
let t2 = Temperature { val: 298.0 };

let t_new = AttributeUpdate::merge(t1, t2); // use AttributeUpdate::_
let t_ref = Temperature { val: 285.5 };

assert_eq!(Temperature::split(t_new), (t_ref, t_ref)); // or Temperature::_

Required Methods§

Source

fn merge(attr1: Self, attr2: Self) -> Self

Merging routine, i.e. how to obtain a new value from two existing ones.

Source

fn split(attr: Self) -> (Self, Self)

Splitting routine, i.e. how to obtain the two new values from a single one.

Provided Methods§

Source

fn merge_incomplete(attr: Self) -> CMapResult<Self>

Fallback merging routine, i.e. how to obtain a new value from a single existing one.

The returned value directly affects the behavior of sewing methods: For example, if this method returns an error for a given attribute, the sew method will fail. This allows the user to define some attribute-specific behavior and enable fallbacks when it makes sense.

§Return / Errors

The default implementation succeeds and simply returns the passed value.

Source

fn merge_from_none() -> CMapResult<Self>

Fallback merging routine, i.e. how to obtain a new value from no existing one.

The returned value directly affects the behavior of sewing methods: For example, if this method returns an error for a given attribute, the sew method will fail. This allows the user to define some attribute-specific behavior and enable fallbacks when it makes sense.

§Errors

The default implementation fails with Err(CMapError::FailedAttributeMerge).

Source

fn split_from_none() -> CMapResult<(Self, Self)>

Fallback splitting routine, i.e. how to obtain two new values from no existing one.

The returned value directly affects the behavior of sewing methods: For example, if this method returns an error for a given attribute, the unsew method will fail. This allows the user to define some attribute-specific behavior and enable fallbacks when it makes sense. value).

§Errors

The default implementation fails with Err(CMapError::FailedAttributeSplit).

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.

Implementors§