Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"struct field is never used", but needed by RAII

Tags:

rust

raii

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?

like image 254
swizard Avatar asked Dec 01 '22 15:12

swizard


1 Answers

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.

like image 197
Vladimir Matveev Avatar answered May 16 '23 07:05

Vladimir Matveev