How does one create a version of my_fun below with prototype
fn my_generic<U>(index : usize) -> U
The index keeps increasing for a huge number of conversions and validity
of the conversions should be checked at the end (not for each conversion).
fn my_fn(index : usize) -> u32 {
index as u32
}
fn my_generic< U : From<usize> >(index : usize) -> U {
index.into()
}
fn main()
{ let index : usize = 5;
let index_fn : u32 = my_fn(index);
let index_generic : usize = my_generic(index);
println!( "index_fn = {index_fn}" );
println!( "index_generic = {index_generic}" );
//
// If you uncomment the statement below you get the error
// the trait From<usize> is not implemented for u32.
// let index_generic : u32 = my_generic(index);
}
/*
// The following function results in the compiler error below it.
fn my_generic<U>(index : usize) -> U {
index as U
}
an as expression can only be used to convert between primitive types ..
*/
I'm not sure what your setup is, but maybe you can do the conversion at the very end of your algorithm and do all the index computations solely with usize, before trying to convert the index to u32?
As to why there is no From<usize> for u32 implementation, it is because the conversion can fail with an overflow on 64-bit machines. If you can change the signature of my_generic you could use TryFrom:
use std::convert::TryFrom;
fn my_fn(index : usize) -> u32 {
index as u32
}
fn my_generic< U : TryFrom<usize, Error=E>, E>(index : usize) -> Result<U, E> {
index.try_into()
}
fn main()
{ let index : usize = 5;
let index_fn : u32 = my_fn(index);
let index_generic : usize = my_generic(index).unwrap();
println!( "index_fn = {index_fn}" );
println!( "index_generic = {index_generic}" );
//
// If you uncomment the statement below you get the error
// the trait From<usize> is not implemented for u32.
let index_generic : u32 = my_generic(index).unwrap();
println!( "index_generic = {index_generic}" );
}
I have a tape that stores variable information. If the tape uses u32, I want it to fail at the end when I check the usize that has the value for the next variable. I do not want to check every conversion because this would slow down the taping.
I know how to do this using macros, I am just checking to see if it is possible using generic functions.
I think that try_into is slower that as ?
Since the index for a tape keeps increasing, I can do one check when the tape is done recording and do not need to check every value to see that it is valid.
If you're only using this for usize -> u32 then it doesn't really make sense to use a generic trait. If you need to use it in a generic function with various types then it does make sense.