First of all, great post, thank you! Like many of your readers, I've seen Cow
s, known about Cow
s, but never really stopped to look at them long enough to wonder why I might use them. No more! :)
One question I have is around the rationale you provided for the ?Sized
trait given in Cow
's declaration:
?Sized
is a funny one. By default, Rust requires types to be of a known size. ... if you have a reference to [an array of bytes], the reference itself can contain the length. ... Since a Cow should be able to contain a &[u8]
, we need to say “we don’t require this to be Sized
”
My thoughts on this are that a &[u8]
is indeed sized--it's a fat pointer--always exactly 64 or 128 bits (depending on your platform), namely size_of::<*const u8> + size_of::<usize>
. Further, as a fat pointer, the second element provides the size of the array, so one would think we have all the size information we could ever need...
I'll grant that the fat pointer's array size may not be known at compile time. But given that the Cow
stores a &[u8]
(and not a [u8]
), shouldn't the fat pointer (which itself does have a known size at compile time) be all the Cow
needs? I'm wondering why Cow
needs more than this to work, or put another way, re-raises the question, why does Cow
required ?Sized
?
I hope my question makes sense. Thanks again for the excellent post.