2bdkid / refinement

Convenient creation of type-safe refinement types.
17 stars 3 forks source link

`DerefMut` breaks type safety #6

Open T0mstone opened 1 year ago

T0mstone commented 1 year ago

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`
2bdkid commented 1 year ago

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?

T0mstone commented 1 year ago

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.