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

Cow type (Clone-on-write) #404

Open
danakj opened this issue Oct 25, 2023 · 0 comments
Open

Cow type (Clone-on-write) #404

danakj opened this issue Oct 25, 2023 · 0 comments
Labels
design Design of the library systems as a whole, such as concepts

Comments

@danakj
Copy link
Collaborator

danakj commented Oct 25, 2023

Clone-on-write lets you store a reference but on write it can convert to an owned version of the same type.

std.rs/cow

To work with it as an lvalue more, and avoid template writing, we may want to add a method or two:

  • set_owned()

Context and use case: https://chromium.slack.com/archives/CGGPN5GDT/p1698242024888729?thread_ts=1698166154.431099&cid=CGGPN5GDT

The particular case I would've suggested it for is a GURL, so that I can do something like this:

Cow<GURL> request_url = ...; // some const GURL& on the RHS
if (my_condition) {
  request_url = Cow::Owned(some_transformation(request_url));
}
// Now use request_url as usual

Instead of doing:

const GURL* request_url = &some_url;
GURL transformed_url;
if (my_condition) {
  transformed_url = some_transformation(*request_url);
  request_url = &transformed_url;
}
// Now use request_url as usual

The goal is just to minimize work in the common case where my_condition is false; and to nicely encapsulate the lifetime concerns introduced by using a raw pointer in the current approach.

@danakj danakj added the design Design of the library systems as a whole, such as concepts label Oct 25, 2023
@danakj danakj mentioned this issue Oct 25, 2023
17 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of the library systems as a whole, such as concepts
Projects
None yet
Development

No branches or pull requests

1 participant