Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VarZeroCow #5809

Merged
merged 5 commits into from
Nov 13, 2024
Merged

Add VarZeroCow #5809

merged 5 commits into from
Nov 13, 2024

Conversation

Manishearth
Copy link
Member

Fixes #5561

We can add more methods as needed, right now I think it covers most use cases.

@Manishearth Manishearth requested a review from sffc as a code owner November 12, 2024 22:19
@Manishearth Manishearth force-pushed the varzerocow branch 2 times, most recently from 79fe437 to 53f0515 Compare November 12, 2024 22:20
utils/zerovec/src/cow.rs Outdated Show resolved Hide resolved
#[cfg(feature = "serde")]
impl<'a, 'de: 'a, V: VarULE + ?Sized> serde::Deserialize<'de> for VarZeroCow<'a, V>
where
Box<V>: serde::Deserialize<'de>,
Copy link
Member

Choose a reason for hiding this comment

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

Thought: Since the Deserialize impl almost always requires invoking EncodeAsVarULE, it would be nice if we could encapsulate that in VarZeroCow's impl.

But, I don't see a super easy way to do that. VarULE doesn't have an associated Sized type that we can put the bound on. We could add an additional type parameter to VarZeroCow, but maybe that is overkill.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we need to do that, and I'd like to leave the flexibility on this to individual types.

@@ -47,6 +48,35 @@ pub struct VarZeroCow<'a, V: ?Sized> {
unsafe impl<'a, V: ?Sized> Send for VarZeroCow<'a, V> {}
unsafe impl<'a, V: ?Sized> Sync for VarZeroCow<'a, V> {}

impl<'a, V: ?Sized> Clone for VarZeroCow<'a, V> {
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: maybe convert it to an enum PrivateCow<'a, V> { Owned(Box<V>), Borrowed(&'a V) }, where you can #[derive(Clone)], and then you don't need to duplicate the unsafe code

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd still need unsafe code to extract the Box<V> and &'a V. These could be added as methods, but I think it only cleans up one method (this one).

Copy link
Member

Choose a reason for hiding this comment

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

I still think the encapsulation would be better, but not strongly

@Manishearth Manishearth requested a review from sffc November 12, 2024 23:03
sffc
sffc previously approved these changes Nov 12, 2024
@Manishearth Manishearth merged commit 5029ff3 into unicode-org:main Nov 13, 2024
28 checks passed
@Manishearth Manishearth deleted the varzerocow branch November 13, 2024 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ZeroCow: a one-element, owned-or-borrowed VarULE
2 participants