refinement icon indicating copy to clipboard operation
refinement copied to clipboard

`DerefMut` breaks type safety

Open T0mstone opened this issue 3 years ago • 2 comments

You can modify the inner value using DerefMut, even to something the predicate wouldn't allow.

Example:

let two: Refinement<u8, LessThanTen> = Refinement::new(2);
*two.deref_mut() = 11;
println!("{two}"); // prints `11`

T0mstone avatar Sep 15 '22 19:09 T0mstone

Hmm good point. Would a immutable model be more appropriate? Or maybe something like map(self, func) -> Optional<Refinement>? Applies func to self and returns a refinement value of the result else None if it doesn't satisfy the condition?

2bdkid avatar Sep 17 '22 21:09 2bdkid

Yes, immutable would definitely be the way to go, since once the user has the power to mutate the inner value, all guarantees are off.

The map function also sounds nice, even though you can already do it with Refinement::new(func(self.into_inner())), but not having to type all that every time would certainly be nice.

T0mstone avatar Sep 18 '22 04:09 T0mstone