-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Opt-in builtin traits, take 2: default and negative impls. #127
Conversation
cc @flaper87 |
cc @pnkfelix -- I'd specifically like feedback re: the GC discussion. |
Can you imagine any other potential use of |
@bstrie I think so, yes. On the one hand, any trait with methods can declare those methods as unsafe, but that's not quite the same thing -- that's saying that the methods cannot themselves check all the conditions that would be required to show they are safe (and hence the caller must be relied upon to do so). I imagine you would want to use an unsafe trait to signal that the method is running in an unsafe environment where it is expected not to make use of certain facilities, even though we can't stop it from doing so, or where it is expected to maintain other invariants. For example, imagine a trait To summarize, the role of the unsafe keyword depends on context:
You can use unsafe on both a trait and the methods in a trait to signal extra conditions both ways. :) |
I think the RFC needs to elaborate on the interaction of this feature with trait objects (i.e. bounds). In particular: I assume that something here is going to automatically become a candidate for being listed as a bound on a trait bound (or a closure), to fit with the current usage of The question is: Is it unsafe traits that become such bounds? Or traits with a default impl? |
@pnkfelix Yes, a good point! I thought of that as I was traveling home last night. I was making the assumption that we would permit arbitrary traits to be listed as bounds, but of course that merits another RFC, and for the time being I'd probably be inclined to limit "extra" bounds on trait objects to traits with no methods, so as to avoid having to codegen the vtables. |
Couldn't this be extended to the
Of course, |
@LeoTestard I don't think it makes sense for |
6357402
to
e0acdf4
Compare
|
||
We add a notion of a *default impl*, written: | ||
|
||
impl Trait for .. { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe _
is better? impl Trait for _ {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the motivation is via analogy with the ..
wildcard when matching fields of a struct: ..
stands for any set of values except for the ones listed explicitly, i.e.:
fn main() {
struct S { x: i8, y: i8, z: i8 };
let s = S { x: 1, y: 2, z: 3 };
match s {
S { x, .. } => println!("x: {}", x)
}
}
Because of the precedent illustrated above, I think ..
denotes well the notions that
- many implementations are being introduced by this
impl
, and - there may be exceptions to this
impl
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_
means "any (other) value/type omitting it's name here".
Rust already uses it in let and match statements in variant ways:
let p = box 1;
let _ = p;
let (x, _) = (Some(1), Some(2));
match x {
Some(_) => { }
_ => { }
}
I think _
denotes well the notions 1 and 2 from @pnkfelix above, and
3. _
is shorter than ..
.
4. ..
was already used in rangex..y
array[..N]
and slice[a..b]
syntax, in other significations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does _
denote notion 2? It accepts anything, not "anything except ..."
the fact that one can use it as a catch-all in match
after a series of other clauses seems like more an artifact of the semantics of match
rather than inherent to _
itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Thu, Sep 18, 2014 at 02:15:26AM -0700, Liigo Zhuang wrote:
_
means "any (other) value/type omitting it's name here".
The key point is that impl Trait for Foo { }
is very different from
applying an impl Trait for .. { }
to Foo -- the latter walks down
recursively, applying a test to the parameter types, and the former
does not. _
does not imply this recursive walk to me.
From a cursory look, this seems like a great solution! |
In particular: * The RFC associated with rust-lang#127 should have had a link to rust-lang#19 as well (and has been assigned RFC rust-lang#19); it also was revised to match the markdown href style of other RFCs. * RFC rust-lang#34 needed its header entries filled in, * RFC rust-lang#123 had a typo in its header, and * RC rust-lang#155 was revised to match the markdown href style of other RFCs.
…tsakis This commit introduces the syntax for negative implementations of traits as shown below: `impl !Trait for Type {}` cc #13231 Part of RFC rust-lang/rfcs#127 r? @nikomatsakis
Hope it's not too late to join the conversation. I think Opt-out traits seem to have the same problems as negative trait bounds, for example:
|
The high-level idea is to add language features that simultaneously
achieve three goals:
Send
andShare
out of the language entirely and into thestandard library, providing mechanisms for end users to easily
implement and use similar "marker" traits of their own devising;
the need for explicit opt-in; and,
unsafe pointers or implement special abstractions) to "opt-in" to
sendability and sharability with an unsafe declaration.