Hi,
I need to read attributes and cast them to types I choose. It should be OK to cast a float to an integer and vice versa. It worked so far before adding floats to the equation. Once I did that, I got the error:
conflicting implementations of trait `ReadAttribute`
The purpose of this is to be able to pass any type, and then get it cast to any other type (except for string) after reading it from file (the file defines the type). In C++, I know how to do this, since C++ has SFINAE. But in rust, I'm using these traits with macros.
Below, if you uncomment that piece of code, it won't compile because of a conflict.
Am I doing this wrong? How can I get floats to be considered in my design?
use num_traits::PrimInt;
use num_traits::float::FloatCore;
trait PrimIntTrait: PrimInt + Default {
fn cast(data: &[u8], type_id: u32) -> Result<Self, Box<dyn std::error::Error>>;
}
trait PrimFloatTrait: FloatCore + Default {
fn cast(data: &[u8], type_id: u32) -> Result<Self, Box<dyn std::error::Error>>;
}
pub struct HDF5Attribute<T> {
pub val: T,
}
trait ReadAttribute {
fn get_attribute_value() -> Result<HDF5Attribute<Box<Self>>, Box<dyn std::error::Error>>;
}
impl ReadAttribute for String {
fn get_attribute_value() -> Result<HDF5Attribute<Box<String>>, Box<dyn std::error::Error>> {
return Err(Box::new(std::io::Error::new(std::io::ErrorKind::InvalidData, "error msg")));
}
}
impl<T: PrimIntTrait> ReadAttribute for T {
fn get_attribute_value() -> Result<HDF5Attribute<Box<T>>, Box<dyn std::error::Error>> {
return Err(Box::new(std::io::Error::new(std::io::ErrorKind::InvalidData, "error msg")));
}
}
//impl<T: PrimFloatTrait> ReadAttribute for T {
// fn get_attribute_value() -> Result<HDF5Attribute<Box<T>>, Box<dyn std::error::Error>> {
// return Err(Box::new(std::io::Error::new(std::io::ErrorKind::InvalidData, "error msg")));
// }
//}
fn main() {
println!("Hello world!");
}
Thanks in advance.