While you can’t do that exactly, the usual workaround is to just wrap the type you want in your own type and implement the trait on that.
use somecrate::FooType;
use somecrate::BarTrait;
struct MyType(FooType);
impl BarTrait for MyType {
fn bar(&self) {
// use `self.0` here
}
}
More Related Contents:
- Traits and Rust
- How to get a reference to a concrete type from a trait object?
- Why can impl trait not be used to return multiple / conditional types?
- What makes something a “trait object”?
- Is it possible to use `impl Trait` as a function’s return type in a trait definition?
- How do I return an instance of a trait from a method?
- I implemented a trait for another trait but cannot call methods from both traits
- Why would I implement methods on a trait instead of as part of the trait?
- How does the mechanism behind the creation of boxed traits work?
- Trait implementation for both a trait object and for direct implementors of the trait
- Why is the `Sized` bound necessary in this trait?
- How can I implement Rust’s Copy trait?
- Is there any trait that specifies numeric functionality?
- Understanding Traits and Object Safety
- How do I pass a closure through raw pointers as an argument to a C function?
- Can’t implement a trait I don’t own for all types that implement a trait I do own
- How do I specify that a struct field must implement a trait?
- Rust equivalent to Swift’s extension methods to a protocol?
- Cannot use moved BufReader after for loop with bufreader.lines()
- Do I have to implement a trait twice when implementing it for both reference and non-reference types?
- Lifetime error when creating a function that returns a value implementing serde::Deserialize
- How do I pass disjoint slices from a vector to different threads?
- Compare enums only by variant, not value
- How do you actually use dynamically sized types in Rust?
- How to operate on 2 mutable slices of a Rust array?
- How can I use a module from outside the src folder in a binary project, such as for integration tests or benchmarks?
- How to match on data type in Rust?
- Why does Rust allow mutation through a reference field using an immutable binding?
- Multiple return types from a method
- Why does this mutable borrow live beyond its scope?