trait ToUsize {
fn to_usize(&self) -> Option<usize>;
}
enum InnerType {
UInt64,
Float64,
}
trait Inner: 'static {}
impl Inner for u64 {}
impl Inner for f64 {}
trait ArrayType {
type Inner: Inner;
const TYPE: InnerType;
}
struct ArrayTypeU64 {}
impl ArrayType for ArrayTypeU64 {
type Inner = u64;
const TYPE: InnerType = InnerType::UInt64;
}
struct ArrayTypeFloat64 {}
impl ArrayType for ArrayTypeFloat64 {
type Inner = f64;
const TYPE: InnerType = InnerType::Float64;
}
trait UsizableArray: ArrayType {}
impl UsizableArray for ArrayTypeU64 {}
fn to_usize<K: UsizableArray>(array: Vec<K>) -> Option<Vec<usize>>
where K::Inner: ToUsize {
// convert array into `Vec<usize>` via to_usize if all items can
// be converted to `usize` via `ToUsize::to_usize()`
todo!()
}
fn main() {}
In this example, I restricted K::Inner to ToUsize. Is there a way of restricting this in the trait declaration, i.e. "UsizableArray::Inner must always be ToUsize".
Something like
trait UsizableArray: ArrayType where ArrayType::Inner: ToUsize {}
In other words, is it possible to restrict the type when declaring derived traits?