#Casting traits through method

37 messages · Page 1 of 1 (latest)

hollow falcon
#

Im trying to cast a generic type (represented as PhantomData<V>) into a trait (which is implemented for PhantomData<V>) through a method. Can someone explain why rust does not implement Into for dyn Trait if the type implements that trait?

pub trait Trait {

}

impl<V> Trait for PhantomData<V> {

}

pub fn test() {
    // Trying to cast PhantomData<f32> to Trait through the method. (PhantomData<f32> implements Trait)
    let output: Box<dyn Trait> = cast::<dyn Trait, f32>();
    //                                             ^^^ the trait `From<PhantomData<f32>>` is not implemented for `Box<dyn Trait>`
}

pub fn cast<A: ?Sized, V>() -> Box<A> where PhantomData<V>: Into<Box<A>> {
    PhantomData::<V>.into()
}
golden notch
#

uh

#

why are you using PhantomData

hollow falcon
#

Because the api level code only has a generic for a method, i need to create a concrete type so i can can save it later into the trait

golden notch
#

how is using a PhantomData relevant

hollow falcon
#

Because dyn Trait needs to have an object behind it which has s generic

golden notch
#

that is not a use case for PhantomData

hollow falcon
#

The trait is supposed to have functions which use the generic type

#

The point is that you can call the type erased Box<dyn Trait> to do concrete type things

golden notch
#

again, PhantomData is not going to help here. that type is mostly useful for directing the variance of the generic

#

not whatever this is

#

it feels like you are solving a different problem using the wrong solution

hollow falcon
#

What else am i supposed to use? I cannot use T because then Trait is not Object safe

golden notch
#

the code example you've shown lacks too much context

#

for example you are not showing any trait method that has a generic

hollow falcon
#

Here is the full context and where this is supposed to be used

#

I just don't like that extra SystemComponent trait and i want it to be auto implemented if A::Manager is PhantomData<T>

golden notch
#

are you implementing an ECS?

hollow falcon
#

It's for a couple modules, one of them is an ECS

golden notch
#

i'd suggest looking at a simple ECS library in rust, like hecs for example

hollow falcon
#

I've already done that and that is not applicable in this instance

#

I need to be able to iterate all of the types on creation of the ECS

#

I cannot do that on an "on insert" basis like hecs does

#

It's very hard for me to explain all of this without talking about the entire codebase and its reasoning

hollow falcon
golden notch
#

because it doesnt make sense

#

it's like asking how do i convert () to Box<dyn Trait>

hollow falcon
#

How so? From a compiler standpoint PhantomData<f32> is clearly able to be represented as dyn Trait

golden notch
#

no? where did you get that idea

#

do you know what a PhantomData is?

hollow falcon
#

Why does the compiler not implement Into<Box<dyn Trait>> for PhantomData<f32>?

#

It really does not matter if it's PhantomData or a different type

#

Into is not automatically implemented for dyn Trait for the objects implementing it

mellow gale
#

Thing is, PhatomData doesn't actually hold data, so there is nothing to convert

hollow falcon
#

Hm i do get that

#

but im confused why as allows conversion but not From