Repository navigation
How to implement a function per primitive type? #2847
|
@197g I think you mentioned before that there is a way to implement functionality per Basically, I have some trait, let's call it Right now, I can implement my trait Op {
fn op(self, param: SomeType) -> Self;
}
impl Op for u8 { ... }
impl Op for u16 { ... }
impl Op for f32 { ... }But that obviously doesn't allow me to use it in generic code where I only have a type I want to keep my |
Replies: 1 comment 3 replies
|
The statement was scoped at functionality to be implemented inside the What we don't provide is a way to do this outside |
The statement was scoped at functionality to be implemented inside the
imagecrate (for now). Then you can make your trait a parent of the internalPrimitiveSealedtrait if you want to keep its existence fully private or a parent ofPrimitiveif you want to choose the sealed'ness of the trait yourself. Then, you still have to provide an impl for all types that do implementPrimitive, you can access the parent in any scope whereP: Primitive. In some situations the compiler needs a bit of convincing (seedispatch_transform_from_sealed).What we don't provide is a way to do this outside
image. We might provide a form ofvisitmethod by such a parent trait though, where different visitors ha…