3
lorentz
188d

in a moment of enlightenment, I realized what problem Cow (copy-on-write smart pointer) actually solves.

Time to touch every file I guess

Comments
  • 2
    @jestdotty the docs describe what it does perfectly, and I knew about it for years, but I needed to have written the problem tens of times and then felt bad about it for weeks contemplating every other possible solution before I realized that Cow is what I need.
  • 0
    What does it solve? An undo function maybe? I spend a bit of effort preventing my COW objects from copying unintentionally.
  • 1
    @Demolishun My understanding is that it's meant to potentially save an allocation when I can't guarantee that the code path will not need an allocation, by holding either a borrowed or an owned value.

    At the moment this means that I can merge the definition for a message that's sent up the call stack which would best borrow all of its constituent parts, and the equivalent definition that's passed down the stack, which must be completely self-contained.
  • 1
    @Demolishun if you want to deliberately hold onto older versions of an object a Cow might be a handy component, but it's not really required. Even with my newfound understanding of Cow, I'd prefer a simple approach with immutable refcounted data and unwrap_or_clone before mutations.
Add Comment