Edit: I've clarified this question a bit more in this post below. I think that I'm trying to store a fn which takes a struct with a const variable so that I can access that variable inside the fn. From a language perspective, I feel like this is a reasonable thing to want to do with const generics, would love if somebody could help me understand why or why not.
Hi all, I'm having some trouble with const generics. Not sure if it's just me or it's not available yet.
I'm trying to figure out the Fn
signature for a boxed closure which would take a struct with a const variable. It seems to work fine when using a const variable on a function defn like: fn foo<const T: MyStructType>(bar: MyStruct<T>)
.
#![allow(incomplete_features)]
#![feature(const_generics)]
#[derive(PartialEq, Eq)]
pub enum VehicleType {
Car,
Boat,
}
pub struct Vehicle<const V: VehicleType> {
wheels: u32
}
pub struct World {
compare_transport: Box<dyn Fn(Vehicle<A>, Vehicle<B>)>,
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0412]: cannot find type `A` in this scope
--> src/lib.rs:15:43
|
14 | pub struct World {
| - help: you might be missing a type parameter: `<A>`
15 | compare_transport: Box<dyn Fn(Vehicle<A>, Vehicle<B>)>,
| ^ not found in this scope
error[E0412]: cannot find type `B` in this scope
--> src/lib.rs:15:55
|
15 | compare_transport: Box<dyn Fn(Vehicle<A>, Vehicle<B>)>,
| ^ not found in this scope
|
help: there is an enum variant `unicode_bidi::BidiClass::B`; try using the variant's enum
|
15 | compare_transport: Box<dyn Fn(Vehicle<A>, Vehicle<unicode_bidi::BidiClass>)>,
| ^^^^^^^^^^^^^^^^^^^^^^^
help: you might be missing a type parameter
|
14 | pub struct World<B> {
| ^^^
error[E0107]: wrong number of const arguments: expected 1, found 0
--> src/lib.rs:15:35
|
15 | compare_transport: Box<dyn Fn(Vehicle<A>, Vehicle<B>)>,
| ^^^^^^^^^^ expected 1 const argument
error[E0107]: wrong number of type arguments: expected 0, found 1
--> src/lib.rs:15:43
|
15 | compare_transport: Box<dyn Fn(Vehicle<A>, Vehicle<B>)>,
| ^ unexpected type argument
error[E0107]: wrong number of const arguments: expected 1, found 0
--> src/lib.rs:15:47
|
15 | compare_transport: Box<dyn Fn(Vehicle<A>, Vehicle<B>)>,
| ^^^^^^^^^^ expected 1 const argument
error[E0107]: wrong number of type arguments: expected 0, found 1
--> src/lib.rs:15:55
|
15 | compare_transport: Box<dyn Fn(Vehicle<A>, Vehicle<B>)>,
| ^ unexpected type argument
error: aborting due to 6 previous errors
Some errors have detailed explanations: E0107, E0412.
For more information about an error, try `rustc --explain E0107`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.