-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Deref documentation: Remove references to "smart pointers" #91004
Comments
An alternative could be to actually define the term "smart pointer" somewhere. The reference contains a paragraph on smart pointers in section 4.1.13 on "pointer types", but lacks a definition. However, it is at least questionable whether such a definition could
and at the same time
|
Hm from a teaching standpoint, I'd definitely agree the docs shouldn't be introducing jargon without first defining it. And in this case I'm uncertain of the benefit of introducing it at all. In Rust, is the term actually used outside of It may be better to directly explain what kinds of types |
Follow up: The Rust book does actually have a chapter on smart pointers. And then the term is used in the next chapter on Fearless Concurrency. Some quotes from the former:
|
Incidentally, |
Perhaps a better way is to describe when Currently some people say anything that implements If the warning was removed, the Rust community and the Rust reference could provide a clear definition what a smart pointer (or smart reference, see link above) actually is: Something that implements |
Following my previous comment, the warning could be changed to something like: "Warning: Implementing |
I feel like actually writing out a definition of smart pointer could be helpful here. Something like "Implementing |
While "smart pointer" was never a precise technical term in any language community, I think what the Deref docs are trying to get at by using the phrase is: A user/library-defined type which is generic over a type T, and which conceptually wraps a single T with indirection. So my strawman rewrite of this paragraph:
would be something like this:
The TRPL chapter on Smart Pointers I think is acceptable, since that intro-level writing contains a lot of similar simplifications and vague concepts that an expert user would eventually discard, but we can definitely tweak the introduction to make it clearer that "smart pointer" is not a precise term. For example, changing "We’ve already encountered a few smart pointers" to "We've already encountered a few types that could be considered smart pointers". |
This is definitely an improvement, but it would be nice to elaborate some on "to avoid confusion:" How might this confusion manifest itself if There's also the question of whether any non-"pointer-like" types can reasonably implement
In many ways, this behaves like |
I think that's an assumption that isn't necessarily true. There have been different opinions whether a "smart pointer" needs to be generic (see also the aboved linked discussion thread "What are smart pointers?"). Most prominent example is Introduction in Chapter 15 ("Smart Pointers"):
Also note that a |
Perhaps it could help to elaborate on the "confusion", rather than reference smart pointers. For example, it could discuss how any method you add to something which implements |
We discussed this in our T-libs meeting today and our consensus was that we would welcome a PR that clarifies this, but that we don't feel comfortable making a decisions regarding the discussion so far in the absence of such a PR that introduces concrete wording. We definitely agree with the general direction of clarifying when to use Deref and how it restricts your API design decisions going forward, and that we shouldn't center jargon terminology like "smart pointer". |
Also relevant to the discussion: currently the standard library implements In general, I find this whole warning to be a bit too opinionated and subjective, since, as we can see, it's difficult to come up with hard rules. I thus second @scottmcm's more practical approach of: "beware that implementing |
See also Christopher Durham's attempt to define a smart pointer here. |
A bit more complete; here's how I currently lean towards defining a "smart pointer:" /// A smart pointer which can can temporarily suspend being smart.
///
/// # Safety
///
/// - `from_raw(into_raw(self))` must return `self` without any change
/// in validity / permissions / provenance / aliasing / etc.
/// - `into_raw(self)` must return a valid pointer to the same object
/// as as dereferencing `self`.
/// - If `Self: DerefMut`, then the pointer returned by `into_raw` must
/// be valid for writes as if through `DerefMut` as well as for reads.
pub unsafe trait Tartare: Deref {
/// Convert this smart pointer into a raw one.
///
/// It is valid to dereference the raw pointer
/// as if it were the smart pointer.
fn into_raw(this: Self) -> NonNull<Self::Target>;
/// Convert a raw pointer back into a smart one.
///
/// Moves ownership back into `Self`, invalidating all copies of the
/// raw pointer. Note that this means that it is *not valid* to do
/// `forget(from_raw(ptr))` and continue using `ptr` for all `Tartare`.
///
/// # Safety
///
/// - `raw` must have come from `Self::into_raw`.
unsafe fn from_raw(raw: NonNull<Self::Target>) -> Self;
} (From a crate1 where I'm experimenting with providing a better(?)2 version of Details are of course extremely fungible (and docs are still incomplete), but the main idea is that a "smart pointer" is "raw pointer + smarts," so it should be defined by conversions into/from a raw pointer. Footnotes
|
I've taken a crack at drafting something that I think it clearer and gives more helpful guidance: #110340 Comments very much welcome and if this direction seems good I will polish for proper review. |
Rollup merge of rust-lang#110340 - jmaargh:jmaargh/deref-docs, r=Mark-Simulacrum Deref docs: expand and remove "smart pointer" qualifier **Ready for review** ~~This is an unpolished draft to be sanity-checked~~ Fixes rust-lang#91004 ~~Comments on substance and content of this are welcome. This is deliberately unpolished until ready to review so please try to stay focused on the big-picture.~~ ~~Once this has been sanity checked, I will similarly update `DerefMut` and polish for review.~~
The documentation for
Deref
reads, in part:This warning has sparked significant confusion, which seems to stem from there being no consensus of what actually is a "smart pointer". The warning should be either removed or rewritten in terms of well-understood properties.
Relevant URLO discussions:
Deref
The text was updated successfully, but these errors were encountered: