maciejhirsz / kobold

Easy declarative web interfaces.
https://docs.rs/kobold/
Mozilla Public License 2.0
385 stars 7 forks source link

`VString`: Versioned String #86

Closed maciejhirsz closed 1 year ago

maciejhirsz commented 1 year ago

Added a new VString type that's a drop-in replacement for a regular String. VString on stack is just a regular String + an extra usize integer denoting current "version" of the string. It functions identically (via Deref and DerefMut traits) to a regular String, with main distinction being incrementing an internal counter on mutable access:

impl DerefMut for VString {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.ver += 1;

        &mut self.inner
    }
}

When used as a view a reference to a VString functions much like { ref some_regular_str } in that it performs no extra allocations and instead only diffs the pointer address + version of the string.

In practice it means that a &VString view should be used everywhere where a { ref string } cannot be used.