I'm trying to implement a memory mapped file by combining os::MemoryMap
and fs::File
in some kind of RAII style. Consider the following example:
#![feature(fs, os, io, path, std_misc, core)]
use std::{io, os, mem, raw};
use std::io::{Seek};
use std::fs::{File};
use std::path::{Path};
use std::os::{MemoryMap};
use std::borrow::{Cow};
use std::error::{FromError};
use std::os::unix::{AsRawFd};
struct Mmapped {
file: File,
map: MemoryMap,
map_len: usize,
}
#[derive(Debug)]
enum Error {
IoError(io::Error),
MmapError(os::MapError),
}
impl FromError<io::Error> for Error {
fn from_error(err: io::Error) -> Error { Error::IoError(err) }
}
impl FromError<os::MapError> for Error {
fn from_error(err: os::MapError) -> Error { Error::MmapError(err) }
}
impl Mmapped {
fn new(filename: &str) -> Result<Mmapped, Error> {
let mut file = try!(File::open(Path::new(filename)));
let map_len = try!(file.seek(io::SeekFrom::End(0))) as usize;
let map = try!(MemoryMap::new(map_len, &[os::MapOption::MapReadable, os::MapOption::MapFd(file.as_raw_fd())]));
Ok(Mmapped { file: file, map: map, map_len: map_len })
}
unsafe fn as_string<'a>(&self) -> Cow<'a, String, str> {
String::from_utf8_lossy(mem::transmute(raw::Slice { data: self.map.data() as *const u8,
len: self.map_len }))
}
}
fn main() {
let m = Mmapped::new("test.txt").unwrap();
println!("File contents: {:?}", unsafe { m.as_string() });
}
playpen
It works, but compiler treats the file field in Mmapped object as dead code:
<anon>:13:5: 13:15 warning: struct field is never used: `file`, #[warn(dead_code)] on by default
<anon>:13 file: File,
^~~~~~~~~~
Could I be sure that it will not optimize it out, and file will be closed in new method? And is there any standard way to mark my field "not dead" code?
I believe that the idiomatic approach is to prefix the field name with _
, which would also silence the warning:
struct Mmapped {
_file: File,
map: MemoryMap,
map_len: usize,
}
I definitely noticed such pattern in the standard library code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With