Let's say I want to convert one type to another, and I have enough info to know that it will work, but the compiler doesn't.
In other words I have A: From<B> but I can't specify this in the signature because of how the data is structured.
Is there any way, even if unsafe to do this? Here's an example:
//Don't need a return type, `convert()` is a side-effect thing
fn main() {
convert(A{}, Kind::B);
}
//Ultimately, here's my problem:
fn convert<T: Foo>(x: T, kind: Kind) {
match kind {
Kind::B => {
//fails
let b:B = x.into();
}
_ => {}
}
}
//I have this original data
struct A {}
//But all I know about it is that it satisfies this trait
trait Foo {}
impl Foo for A {}
//enum describing the type of operation
//as you can see it's decoupled from the actual data type
enum Kind {
B,
C
}
//These match the Kind 1:1 but that's knowledge I have, not the compiler
struct B {}
struct C {}
//We can associate them like this, but it doesn't really help
//since I can't add these constraints to `Foo`
impl From<A> for B {
fn from(a:A) -> Self {
B {}
}
}
impl From<A> for C {
fn from(a:A) -> Self {
C {}
}
}