I'm looking for help with the correct syntax or Rust approach. My use case: I have a generic struct FileData
, which has a variable called provider
. Provider must implement AsRef<[u8]>
so that data may come from static bytes, heap allocated memory, memory mapped, and possibly others. I have a couple methods to create FileData
and they seem to be working well. But there is one
// ERROR: This is the line that I do not get right
pub fn from_file<P: AsRef<Path>>(filename: P, mmap: bool) -> Result<FileData<T>, Box<dyn Error>> {
if mmap == true {
return FileData::mmap_file(filename)
} else {
return FileData::read_file(filename)
}
}
which I don't get right. The method always returns a FileData, back depending on the 'mmap' argument, <T>
is different. It can either be <Box<[u8]>
or <Mmap>
.
I searched for similar questions and articles, but could find one that matches my situation, e.g. (1), (2), (3).
#[derive(Debug)]
pub struct FileData<T: AsRef<[u8]>> {
pub filename: String,
pub provider: T, // data block, file read, mmap, and potentially more
pub fsize: u64,
pub mmap: bool,
}
impl FileData<&[u8]> {
/// Useful for testing. Create a FileData builder based on some bytes.
#[allow(dead_code)]
pub fn from_bytes(data: &'static [u8]) -> Self {
FileData {
filename: String::new(),
provider: data,
fsize: data.len() as _,
mmap: false,
}
}
}
pub fn path_to_string<P: AsRef<Path>>(filename: P) -> String {
return String::from(filename.as_ref().to_str().unwrap_or_default());
}
pub fn file_size(file: &File) -> Result<u64, Box<dyn Error>> {
Ok(file.metadata()?.len())
}
impl FileData<Box<[u8]>> {
/// Read the full file content into memory, which will be allocated on the heap.
#[allow(dead_code)]
pub fn read_file<P: AsRef<Path>>(filename: P) -> Result<Self, Box<dyn Error>> {
let mut file = File::open(&filename)?;
let fsize = file_size(&file)?;
let mut provider = vec![0_u8; fsize as usize].into_boxed_slice();
let n = file.read(&mut provider)? as u64;
assert!(fsize == n, "Failed to read all data from file: {} vs {}", n, fsize);
Ok(FileData {
filename: path_to_string(&filename),
provider: provider,
fsize: fsize,
mmap: false,
})
}
}
impl FileData<Mmap> {
/// Memory Map the file content
#[allow(dead_code)]
pub fn mmap_file<P: AsRef<Path>>(filename: P) -> Result<Self, Box<dyn Error>> {
let file = File::open(&filename)?;
let fsize = file_size(&file)?;
let provider = unsafe { MmapOptions::new().map(&file)? };
Ok(FileData {
filename: path_to_string(&filename),
provider: provider,
fsize: fsize,
mmap: true,
})
}
}
impl<T: AsRef<[u8]>> FileData<T> {
#[allow(dead_code)]
pub fn from_file<P: AsRef<Path>>(filename: P, mmap: bool) -> Result<FileData<_>, Box<dyn Error>> {
if mmap == true {
return FileData::mmap_file(filename)
} else {
return FileData::read_file(filename)
}
}
pub fn as_ref(&self) -> &[u8] {
return self.provider.as_ref()
}
}
The error message is:
error[E0308]: mismatched types
--> src\data_files\file_data.rs:87:20
|
83 | impl<T: AsRef<[u8]>> FileData<T> {
| - this type parameter
84 | #[allow(dead_code)]
85 | pub fn from_file<P: AsRef<Path>>(filename: P, mmap: bool) -> Result<FileData<T>, Box<dyn Error>> {
| ----------------------------------- expected `std::result::Result<file_data::FileData<T>,
std::boxed::Box<(dyn std::error::Error + 'static)>>` because of return type
86 | if mmap == true {
87 | return FileData::mmap_file(filename)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found struct `Mmap`
|
= note: expected enum `std::result::Result<file_data::FileData<T>, _>`
found enum `std::result::Result<file_data::FileData<Mmap>, _>`