Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upOpt-in builtin traits, take 2: default and negative impls. #127
Conversation
This comment has been minimized.
This comment has been minimized.
|
cc @flaper87 |
nikomatsakis
added some commits
May 31, 2014
This comment has been minimized.
This comment has been minimized.
|
cc @pnkfelix -- I'd specifically like feedback re: the GC discussion. |
This comment has been minimized.
This comment has been minimized.
|
Can you imagine any other potential use of |
This comment has been minimized.
This comment has been minimized.
|
@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. :) |
This comment has been minimized.
This comment has been minimized.
|
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? |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
Couldn't this be extended to the
Of course, |
This comment has been minimized.
This comment has been minimized.
|
@LeoTestard I don't think it makes sense for |
nikomatsakis
added some commits
Aug 6, 2014
nrc
assigned
nikomatsakis
Sep 4, 2014
alexcrichton
force-pushed the
rust-lang:master
branch
from
6357402
to
e0acdf4
Sep 11, 2014
pcwalton
referenced this pull request
Sep 15, 2014
Open
opt-in built-in bounds traits RFC tracker (optin_builtin_traits) #13231
aturon
force-pushed the
rust-lang:master
branch
from
4c0bebf
to
b1d1bfd
Sep 16, 2014
liigo
reviewed
Sep 17, 2014
|
|
||
| We add a notion of a *default impl*, written: | ||
|
|
||
| impl Trait for .. { } |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
pnkfelix
Sep 17, 2014
Member
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.
This comment has been minimized.
This comment has been minimized.
liigo
Sep 18, 2014
Contributor
_ 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.
This comment has been minimized.
This comment has been minimized.
pnkfelix
Sep 18, 2014
Member
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.
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Sep 19, 2014
Author
Contributor
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.
liigo
reviewed
Sep 17, 2014
|
|
||
| ## The `Copy` and `Sized` traits | ||
|
|
||
| The final two builtin traits are `Copy` and `Share`. This RFC does not |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
From a cursory look, this seems like a great solution! |
pcwalton
merged commit 2860ed8
into
rust-lang:master
Sep 18, 2014
pnkfelix
added a commit
to pnkfelix/rfcs
that referenced
this pull request
Oct 8, 2014
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Oct 21, 2014
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Oct 23, 2014
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Dec 5, 2014
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Dec 5, 2014
flaper87
referenced this pull request
Dec 29, 2014
Merged
Add syntax for negative implementations of traits #20285
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Jan 4, 2015
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Jan 5, 2015
This comment has been minimized.
This comment has been minimized.
keean
commented
Apr 21, 2016
|
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:
|
nikomatsakis commentedJun 17, 2014
The high-level idea is to add language features that simultaneously
achieve three goals:
SendandShareout 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.