I need copy ArrayString<N>
to ArrayString<M>
,
and want check is it possible at compile time. In other words I want validate M >= N
at compile time.
More details:
struct Foo {
s: ArrayString<20>,
...
}
struct Boo {
s: ArrayString<16>,
...
}
fn f(foo: &mut Foo, boo: &Boo) {
// here I want to check that foo.s.capcity() >= boo.s.capacity()
foo.s = boo.s.as_str().try_into().unwrap();
}
Of course simple static assert doesn't compile:
const _: () = assert!(foo.s.capacity() >= boo.s.capacity());
possible solution is this:
const fn all_good() -> bool {
let foo = Foo {
s: ArrayString::new_const(),
...
};
let foo_s_cap = foo.s.capacity();
std::mem::forget(foo);
let boo = Boo {
s: ArrayString::new_const(),
...
};
let boo_s_cap = boo.s.capacity();
std::mem::forget(boo);
foo_s_cap >= boo_s_cap
}
const _:() = assert!(all_good());
this works, but it is too verbose, and required that all other fields of Foo
and Boo
are const initialized.
Is it possible to make compile time validation of Foo::s.capacity() >= Boo::s.capacity()
with 1-5 lines of code?