5

どうにかして生のポインターの形で取得する必要がある構造体があります。

pub struct BufferData {
    /// Memory map for pixel data
    pub map: Arc<Box<memmap::MmapMut>>,
    pub otherdata: i32,
}

そのフィールドに書き込む必要があるmapため、生のポインタを構造体に逆参照してから、そのデータ フィールドに書き込もうとします。しかし、次のエラーが発生しました。

error[E0596]: cannot borrow immutable borrowed content `*map` as mutable
  --> examples/buffer.rs:34:5
   |
34 |     map[0] = 9;
   |     ^^^ cannot borrow as mutable

mapフィールドを可変かつ書き込み可能にするにはどうすればよいですか?

このエラーは、次のコードを使用して再現できます。

extern crate memmap;

use std::fs::File;
use std::sync::Arc;
use std::boxed::Box;
use std::ops::Deref;

pub struct BufferData {
    /// Memory map for pixel data
    pub map: Arc<Box<memmap::MmapMut>>,
    pub otherdata: i32,
}

fn main() -> () {
    // Somewhere on other module
    let mut mmap = Arc::new(Box::new(unsafe {
        memmap::MmapMut::map_mut(&File::open("./shm").expect("file")).expect("MmapMut")
    }));
    let mut bfr = BufferData {
        map: mmap,
        otherdata: 0,
    };
    let ptr: *const _ = &bfr;

    // Here, I must receive and process a pointer
    let mut bdata: &BufferData = unsafe { &*(ptr as *const BufferData) };
    let mut map = bdata.map.deref().deref();

    // Problem lies here: need to write into it, so need to be mutable
    map[0] = 9;
}

箱:memmap = "0.6.2"

4

1 に答える 1