-
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
Inherent trait implementations #1880
Comments
Please explain what happens when calling crate A {
struct PointType {
x: f32,
y: f32,
}
}
crate B {
trait OnePointTrait {
fn a(&self) -> f32;
fn b(&self) -> f32;
}
exposed impl OnePointTrait for PointType {
fn a(&self) -> f32 { self.x }
fn b(&self) -> f32 { self.y }
}
}
crate C {
trait OtherPointTrait {
fn a(&self) -> f32;
fn b(&self) -> f32;
}
exposed impl OtherPointTrait for PointType {
fn a(&self) -> f32 { self.y }
fn b(&self) -> f32 { self.x }
}
} |
Could add a restriction such that On a side note, this could also help out the 'num-traits' crate which IIRC reexports all the trait impls in the 'num' crate. |
You can always put all the traits into one module and do Also, advanced enough IDEs (or rustfix) will be able to automatically insert the necessary imports (I'm working on this at this very moment) |
The correct term here, IMO, is "inherent", and this would have to obey the rules of inherent impls. |
Agree that "inherent" is the correct term. One way to think of this feature is syntactic sugar to generate an |
IMO, the extra effort of having to write an additional import is worth the flexibility of being able to define your own implementations of functions. I can imagine a case where you want to use a struct but you don't like the implementation of a trait provided by the crate. If that implementation was inherent, how would you opt out of using it in favor of an implementation with the same signature of your choosing? Edit: never mind (see below) |
I'm not sure how this idea makes your concern any worse. e.g. It's already possible for |
Inherit impls actually came up when discussing "thin" pointers to traits. One idea (that I liked) was to allow structs to declare which traits they must implement: struct MyStruct: Trait1 + Trait2 + Trait3; // or struct MyStruct impl Trait1 ... This would have two side effects (not including thin pointer stuff).
The nice thing about this alternative is that it keeps everything in one place. |
@Stebalien This is an OK alternative, but it seems a bit redundant with the |
I'd think the syntax for your
or maybe Now |
@frewsxcv In the following example, it prints 0 or 1 depending on which trait you import: struct MyStruct {
val: u32
}
trait Trait1 {
fn get_val(&self) -> u32;
}
trait Trait2 {
fn get_val(&self) -> u32;
}
impl Trait1 for MyStruct {
fn get_val(&self) -> u32 { self.val }
}
impl Trait2 for MyStruct {
fn get_val(&self) -> u32 { self.val + 1 }
}
mod test {
use ::MyStruct;
// use ::Trait1;
use ::Trait2;
fn run() {
let s = MyStruct { val: 0 };
println!("val: {:?}", s.get_val());
}
} it can't compile if you import both because the |
@Wopple You can differentiate via: Trait1::get_val(&s) EDIT: I originally wrote this, but it's wrong: (s as Trait1).get_val() |
@frewsxcv I see, thanks! |
Perhaps something like |
Also, |
One of the down-sides to the current situation (needing to explicitly |
Hmm... I can see why it would be annoying having to find the correct trait, but I have never found this to be an excessive "hassle" before... at least, not to the degree that I would rather not use traits. Could you give an example of where this was a huge burden? |
I would love to see this feature implemented, as for example in the RustCrypto I have to re-export traits in every crate to make it more convenient for users, or otherwise they'll have to list additional crate (containing traits) as explicit dependency. Also it makes harder to write documentation, as I have to explain how traits are organized to users who do not want to write generic code and just want to call several methods and have stuff done. Overall it's a very annoying papercut for crate authors and users. |
It's never been clear to me why RustCrypto uses so many crates instead of features. Yes, crates provide an easier to understand separation, but if the crates all reside in the same repository anyways then most advantages seem moot. There is no shortage of applications for this language feature though of course, so I'm not objecting to anything. :) We rand into issues when discussing |
I would like to see this feature too: I'm currently looking at generating traits and types from javascript IDL files, and I have to choose between generating inherent methods vs trait methods for interfaces, and neither corresponds cleanly to what the javascript API actually looks like. |
I want to point to (now closed) "custom prelude" RFC #890 Should that RFC implemented, the described problem could be solved with a custom prelude: the client could import |
If I could add my suggestion, I would like to see a syntax like this: impl PointTrait for PointType {
fn x(&self) -> f32 { self.x }
pub fn y(&self) -> f32 { self.y }
} You'd have to The advantages of marking the specific function rather then the impl statement are:
|
I've implemented my syntax with a |
Throwing in my +1 for this, I have a type that implements a custom trait containing a single trait method, but the method really makes sense to be inherent to the type. However, I also need to be able to generalize over multiple types that impl that custom trait. @idanarye Your crate looks very close to what I'd need, but I'd want to preserve the arg names and not hide the inherent method from docs. I'm not familiar with proc macros to be able to understand if the former is possible, though? |
@linclelinkpart5 This is probably a discussion for a ticket in my repository, but IIRC I used #[inherent_pub]
impl Foo for Bar {
pub fn foo(&self, _baz: Baz) {
// ...
}
} Would generate this: impl Bar {
pub fn foo(&self, _baz: Baz) {
<Self as Foo>::foo(self, _baz)
}
} Which would be illegal because it uses impl Bar {
pub fn foo(&self, baz: Baz) {
<Self as Foo>::foo(self, baz)
}
} I would have a problem with: #[inherent_pub]
impl Foo for Bar {
pub fn foo(&self, _baz: Baz, baz: Baz) {
// ...
}
} Which would become: impl Bar {
pub fn foo(&self, baz: Baz, baz: Baz) {
<Self as Foo>::foo(self, baz, baz)
}
} So rather than dealing with all the edge cases it was easier to just rename all the arguments. |
It would be nice if it could support this case as well: pub struct PointType { ... }
// Want magnitude to be accessible as an inherent method on PointType
trait Magnitude {
fn magnitude(&self) -> f32;
}
// Helper trait for implementing Magnitude, may or may not want to expose to user
trait Coordinates {
fn coords(&self) -> Vec<f32>;
}
impl<T: Coordinates> Magnitude for T {
...
}
// PointType implements the helper type explicitly, but not Magnitude
impl Coordinates for PointType { ... } I think this is a +1 for |
My problem with this is that if this got implemented i would now have to sit at each case deciding should i make a trait or and inherited implementation. It would also make rust and many pacakages as a language less flexible because you would probably see an overuse of inheritance causing more packages that does the same to show up. My personal opinion is that this kind of inheritance is not a good direction. |
I've been working a lot with traits lately with this rust-geo pull request.
In that pull request, it splits up concrete geospatial types and geospatial operations. A simplified example:
Now, if someone ever wanted to use this, they'd have to import both the type and the trait:
Most of the time, the user is always going to want the associated methods associated with
PointType
, so it's a little unfortunate that the user has to add another import whenever they want this functionality.It'd be cool if I could make a trait implementation as
exposed
so that the triat methods are always available on that specific type. Something like:(There's probably a better keyword to use here than
exposed
, or maybe some other way to indicate this.)Then the user can just do:
I haven't thought too long about this, so I might be overlooking something here. I don't feel strongly at all about this, just expressing a thought.
The text was updated successfully, but these errors were encountered: