[][src]Attribute Macro dmutils_derive::wrapper

#[wrapper]
use dmutils_derive::wrapper;
use dmutils::*;
 
#[derive(Clone)]
struct NewPoint {
    x: f64,
    y: f64,
}

// the following two lines can be changed to
// `type Point = std::sync::Arc<NewPoint>`
// and the test still pass.
// This enables you to switch between "value type"
// and "reference type" without too many rewrites.
#[wrapper(ptr)]
struct Point(NewPoint);

let mut p: Point = NewPoint {
    x: 2.0,
    y: -1.5,
}.into();

assert_eq!(p.x, 2.0);
assert_eq!(p.y, -1.5);

p.mutate().x = 0.3;
assert_eq!(p.x, 0.3);
assert_eq!(p.y, -1.5);
use dmutils_derive::wrapper;
use dmutils::*;
use std::convert::TryFrom;
 
// misu = Make Illegal State Unpresentable
#[wrapper(misu)]
pub struct Pos(i32);
impl TryFrom<i32> for Pos {
    type Error = String;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        if value > 0 {
            Ok(Pos(value))
        } else {
            Err(format!("'{}' is not positive", value))
        }
    }
}

let pos = Pos::try_from(2).unwrap();
let _ref = pos.as_ref();
let _ref = &*pos;
let _back_to_primitive: i32 = pos.into();