-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
AsRef/Borrow/BorrowMut need better documentation #44868
Copy link
Copy link
Closed
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call for participation: Hard difficulty. Experience needed to fix: A lot.P-mediumMedium priorityMedium priority
Metadata
Metadata
Assignees
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call for participation: Hard difficulty. Experience needed to fix: A lot.P-mediumMedium priorityMedium priority
Type
Fields
Give feedbackNo fields configured for issues without a type.
Links:
AsRef,Borrow,BorrowMut.Those docs have plenty of text. I've read it dozens of times and felt "uhhh, what" every single time. Well, the text sort of makes sense, but, in the end, it doesn't help me in deciding between
AsRefandBorrow. So I just had to dig myself through the APIs and try figuring the mystery by myself. :)Here are some notes I've collected afterwards. I wonder if we could get at least some of the following stuff into the official docs...
BorrowAsRef&T -> &T&Vec<T> -> &[T]&String -> &str&str -> &Path&Path -> &OsStr&OsStr -> &Path&Path -> &Path(*) should be 'borrows', but cannot be implemented yet due to coherence issues (I believe?)
Key takeaways:
Borrowis simple and strict. The hash of the borrowed reference must stay the same.AsRefconverts to a wider range of different types. The hash of the new reference is allowed to change.Exercise 1
We want to implement a function that creates a directory. It must accept both
&strand&Pathas the argument. Which signature are we looking for?Answer: In order to go from
&strto&Path, we have to create a value of different typePath(&stris more primitive - it cannot be borrowed asPath). SinceAsRefcan borrow and convert, it is the correct option here.Exercise 2
We want to check whether a value exists in a
HashSet. Even if we have a set of typeHashSet<String>, we'd like to be able to just dos.contains("foo")(passing a&str). Which one of the four method signatures is the right one?Answer: We don't want to convert between totally different types, so the hash and structural equality of the value must be preserved after reference conversion. In other words, we only want simple borrowing. Conversion to a different type might potentially change the hash and is thus out of the question.
So
Borrowis the right one here. But is itT: Borrow<Q>orQ: Borrow<T>? Well, ifTisStringand we're passing a&strto the method, we want to borrowTasQ. So the right bound isT: Borrow<Q>.