如何使包含Arc可写的结构字段?

时间:2018-02-05 01:45:36

标签: rust immutability dereference raw-pointer

我有一个结构,必须以原始指针的形式检索。

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

我需要写入其map字段,因此我将原始指针解引用到struct中,然后尝试写入其数据字段。但是,我收到了以下错误。

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;
}

Crate:memmap = "0.6.2"

1 个答案:

答案 0 :(得分:2)

作为@Shepmaster pointed outArc在设计上是不可变的:

  

Rust中的共享引用默认情况下禁止突变,Arc也不例外:您通常无法获得对Arc内部内容的可变引用。如果您需要通过Arc变异,请使用MutexRwLock或其中一种Atomic类型。

我添加了Mutex来解决问题:

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

要访问该字段,我需要先锁定它。我添加了一个额外的块来在块完成时自动解锁Mutex

{
    let mut map = bdata.map.lock().unwrap();
    map[0] = 9;
}