-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for Cow::is_borrowed
and Cow::is_owned
#65143
Comments
I'm no expert on how backwards compatibility is handled. But |
Procedural: I’ve edited the title and labels to reflect that this is not implemented. My opinion: I’m not sure that these method carry their weight, since |
@petertodd That's fair; that said, though, I think that having methods named @SimonSapin I agree that Mostly, I figured that adding these methods would be worthwhile because they're unlikely to clash with existing ones, and can incubate in nightly for a while to see if they do carry their weight. If they don't, they can be removed. |
Add Cow::is_borrowed and Cow::is_owned Implements rust-lang#65143.
It appears that this is now merged. |
Add Cow::is_borrowed and Cow::is_owned Implements rust-lang#65143.
@SimonSapin it would make sense to revert the changes to the title and tags, or discuss here whether the change should be reverted. |
Cow::is_borrowed
and Cow::is_owned
Done. |
Sorry for the somewhat-pointless churn, I’d gotten here from https://github.com/rust-lang/rust/issues?q=is%3Aopen+is%3Aissue+label%3AC-tracking-issue+label%3AT-libs and hadn’t realized there was an implementation PR already. |
All good! Thanks for updating things :) |
Constify the following methods of `alloc::borrow::Cow`: - `is_borrowed` - `is_owned` These methods are still unstable under `cow_is_borrowed`. Possible because of rust-lang#49146 (Allow if and match in constants). Tracking issue: rust-lang#65143
…morse Make `cow_is_borrowed` methods const Constify the following methods of `alloc::borrow::Cow`: - `is_borrowed` - `is_owned` Analogous to the const methods `is_some` and `is_none` for Option, and `is_ok` and `is_err` for Result. These methods are still unstable under `cow_is_borrowed`. Possible because of rust-lang#49146 (Allow if and match in constants). Tracking issue: rust-lang#65143
Is anything blocking this from being stabilized? |
Six month check-in: I don't see any issues involving these on the Issue Tracker. Can we move to the next stage? |
Quite sad it's not stabilized yet. Are there any issues? 🥲 I find the method would be pretty convenient when cloning of the content under Example: if my_expensive_thing.is_borrowed() {
return Err(MyError::AttemptToCloneExpensiveThing);
} else {
do_something_heavy(my_expensive_thing.to_mut());
} |
At this point I feel this should be fine to stabilize, but until that happens you can match a pattern: #65143 (comment) |
Going through some old issues. Is there anything stopping this from going into FCP? I found myself using one of these methods again recently. |
I can't see a strong case for the inherent methods over Let-else being stabilized surely plays into this decision, too. |
I would agree, but there exist other methods like EDIT: Note, below may be wrong in most cases: Another important thing to consider is that the method ensures that you're always borrowing the argument, whereas |
That's not the case. See this playground. You'd have to name the inner pattern for there to be an issue. E.g., |
Hmm, I guess you're right then. Still, I question whether such usages are always-applicable (e.g. if there's some usage that might break it), and the original point about other methods existing still stands. IMHO, unless those other methods are deprecated (which seems unlikely), this method should be considered for inclusion. |
One use case for which I wanted these: I'm writing unit tests in which I want to differentiate between a borrowed and owned fn check(expected: Cow<Foo>, input: &str) {
let actual = method_under_test(input);
assert_eq!(expected, actual); // does not differentiate between borrowed or owned
assert_eq!(expected.is_owned(), actual.is_owned());
} It's not hard to work around the absence of this method, and I don't know if it's worth adding the method just for that use case; but I figured I'd mention it. (I'm also a self-taught Rust newbie, so it's possible there's a better way to do this that I just don't know!) |
For the assertion in that unit test, you can assert_eq two More broadly, I don’t think it is reasonable for all enums to provide |
I believe this proposal actually predates Yeah, I guess it's fair to say that pattern-matching is the best way to do this. We'll see what people prefer. |
On Sun, Aug 25, 2024 at 05:03:41AM -0700, Clar Fon wrote:
I believe this proposal actually predates `matches!`, huh.
Yeah, I guess it's fair to say that pattern-matching is the best way to do this. We'll see what people prefer.
IMO `matches!` is sufficient. Having all these little methods clutters up the
documentation. I also recently found out that a friend of mine had someone
missed the fact that `matches!` exists... they might not have if it was used
more often.
|
The only case where |
While We already write All the https://doc.rust-lang.org/std/?search=is_ That's why I'd like to see this stabilized one day, even if it's just a syntax sugar for some. |
…tgross35 remove const_cow_is_borrowed feature gate The two functions guarded by this are still unstable, and there's no reason to require a separate feature gate for their const-ness -- we can just have `cow_is_borrowed` cover both kinds of stability. Cc rust-lang#65143
…tgross35 remove const_cow_is_borrowed feature gate The two functions guarded by this are still unstable, and there's no reason to require a separate feature gate for their const-ness -- we can just have `cow_is_borrowed` cover both kinds of stability. Cc rust-lang#65143
Rollup merge of rust-lang#131617 - RalfJung:const_cow_is_borrowed, r=tgross35 remove const_cow_is_borrowed feature gate The two functions guarded by this are still unstable, and there's no reason to require a separate feature gate for their const-ness -- we can just have `cow_is_borrowed` cover both kinds of stability. Cc rust-lang#65143
+1 from me here -- every time I've tried to use As long as this feature isn't stable, I just end up injecting the methods with an extension trait for readability purposes. |
I just tried to use these in my own code and was kind of shocked they didn't exist.
Justification: this seems like a common Rust pattern. We have
is_some
andis_none
forOption
,is_ok
andis_err
forResult
, etc., so, it seems pretty fair to haveis_borrowed
andis_owned
forCow
.Having
as_borrowed
andas_owned
wouldn't really make much sense, as a simple&
and&mut/to_mut
cover those use cases. But, these check functions are pretty useful on their own.The text was updated successfully, but these errors were encountered: