Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/compact_bytestrings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,18 +481,15 @@ impl CompactBytestrings {

impl Clone for CompactBytestrings {
fn clone(&self) -> Self {
let mut data = Vec::with_capacity(self.meta.iter().map(|m| m.len).sum());
let mut meta = Vec::with_capacity(self.meta.len());

for bytes in self.iter() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this was done this way was to trim on clones - which is fine imo because Clone does not require a byte-for-byte copy. This is unrelated by my main concern with this function was actually whether i should bother making an extend_from_slice_unchecked lol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to violate the principle of least surprise. I wouldn't assume a clone implementation to trim

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, but I wouldn't expect a clone of something to also clone useless bytes.

meta.push(Metadata {
start: data.len(),
len: bytes.len(),
});
data.extend_from_slice(bytes);
Self {
data: self.data.clone(),
meta: self.meta.clone(),
}
}

Self { data, meta }
fn clone_from(&mut self, source: &Self) {
self.data.clone_from(&source.data);
self.meta.clone_from(&source.meta)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Clone, Copy)]
pub(crate) struct Metadata {
pub(crate) start: usize,
pub(crate) len: usize,
Expand Down