flense 0.2.2

Purpose-oriented lensing
Documentation
//! Tests for [`LensMut`].

#![allow(clippy::float_cmp)]

#[path = "./common.rs"]
mod common;

use assert2::assert;
use common::*;
use flense::prelude::*;

#[test]
fn split_lens_mut() {
    let mut v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xCAFE,
    };
    {
        let lens: LensMut<'_, (Normal, Other, Color, Position)> = (&mut v).lens_mut();
        let (mut lhs, mut rhs) = lens.split::<(Position, Normal), _>();

        *lhs.as_mut::<Position, _>() = [10.0, 20.0, 30.0];
        *lhs.as_mut::<Normal, _>() = [99, 100];
        *rhs.as_mut::<Color, _>() = [40.0, 50.0, 60.0];
        *rhs.as_mut::<Other, _>() = 0xBEEF;
    }
    assert!(v.position == [10.0, 20.0, 30.0]);
    assert!(v.color == [40.0, 50.0, 60.0]);
    assert!(v.normal == [99, 100]);
    assert!(v.other == 0xBEEF);
}

#[expect(clippy::float_cmp)]
#[test]
fn lens_mut_as_ref_as_mut() {
    let mut v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xCAFE,
    };
    {
        let mut lens: LensMut<'_, (Normal, Other, Color, Position)> = (&mut v).lens_mut();
        *lens.as_mut::<Position, _>() = [10.0, 20.0, 30.0];
        *lens.as_mut::<Other, _>() = 0xBEEF;
        assert!(*lens.as_ref::<Position, _>() == [10.0, 20.0, 30.0]);
        assert!(*lens.as_ref::<Other, _>() == 0xBEEF);
    }
    assert!(v.position == [10.0, 20.0, 30.0]);
    assert!(v.other == 0xBEEF);
}

/// After `LensMut::split`, each half can hand out independent `&mut` references
/// to its disjoint fields.
#[expect(clippy::float_cmp)]
#[test]
fn lens_mut_split_as_mut() {
    let mut v = Vertex {
        position: [1.0, 2.0, 3.0],
        color: [4.0, 5.0, 6.0],
        normal: [7, 8],
        other: 0xCAFE,
    };
    {
        let lens: LensMut<'_, (Normal, Other, Color, Position)> = (&mut v).lens_mut();
        let (mut lhs, mut rhs) = lens.split::<(Position, Normal), _>();
        *lhs.as_mut::<Position, _>() = [10.0, 20.0, 30.0];
        *lhs.as_mut::<Normal, _>() = [99, 100];
        *rhs.as_mut::<Color, _>() = [40.0, 50.0, 60.0];
        *rhs.as_mut::<Other, _>() = 0xBEEF;
    }
    assert!(v.position == [10.0, 20.0, 30.0]);
    assert!(v.color == [40.0, 50.0, 60.0]);
    assert!(v.normal == [99, 100]);
    assert!(v.other == 0xBEEF);
}