在结构中借用参考 [英] Borrowing reference in structure

查看:36
本文介绍了在结构中借用参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将有关 inotify 事件和 hashmap 的结构信息放入一个结构信息中,其中 inotify watch id 作为键,文件名作为值.

extern crate inotify;使用 inotify::INotify;使用 std::sync::{Arc, Mutex};使用 std::collections::HashMap;结构通知器{inotify: INotify,watch_service: Arc>>,}impl 通知器 {pub fn new() ->通知者{通知者{inotify: INotify::init().unwrap(),watch_service: Arc::new(Mutex::new(HashMap::new())),}}pub fn resolve_events(&mut self) {{让 mut 事件 = self.check_for_events();self.events_execution(events);}}fn check_for_events(&mut self) ->&[inotify::wrapper::Event] {self.inotify.available_events().unwrap()}fn events_execution(&self, events: &[inotify::wrapper::Event]) {对于 events.iter() { 中的事件}}}

在编译期间我收到一个错误

src/main.rs:204:13: 204:17 错误:不能借用 `*self` 作为不可变的,因为它也被借用为可变的 [E0502]src/main.rs:204 self.events_execution(events);

我认为最好的解决方案是以某种方式将 Notificator 结构中的 inotify 变量与 watch_service 分开,但我无法取消引用 self.check_for_events(); 因为我收到了

src/main.rs:203:17: 203:27 错误:特性绑定`[inotify::wrapper::Event]: std::marker::Sized` 不满足 [E0277]src/main.rs:203 let mut events = *self.check_for_events();

我理解问题的核心:我试图通过 check_for_events 借用引用,然后将其用作 events_execution 中的参数,这也需要 self 作为参数,但我不知道如何解决它.

解决方案

可以做的一件事是让你的可变方法使用它的可变借用和 返回一个你可以使用的不可变的:

pub fnresolution_events(&mut self) {让(slf,事件)= self.check_for_events();slf.events_execution(events);}fn check_for_events(&mut self) ->(&Self, &[inotify::wrapper::Event]) {让事件 = self.inotify.available_events().unwrap();(&self, 事件)}

我在 playground(使用u64的vecs作为可变状态,但原理类似).重构您的代码可能会更清晰,以便某些外部客户端可以(可变地)借用 Notifier、生成事件、释放借用并(不可变地)借用它来处理它们......>

I'm trying to put in one structure information about inotify events and hashmap with inotify watch id as key and name of the file as value.

extern crate inotify;
use inotify::INotify;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;

struct Notificator {
    inotify: INotify,
    watch_service: Arc<Mutex<HashMap<inotify::wrapper::Watch, Arc<String>>>>,
}

impl Notificator {
    pub fn new() -> Notificator {
        Notificator {
            inotify: INotify::init().unwrap(),
            watch_service: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    pub fn resolving_events(&mut self) {
        {
            let mut events = self.check_for_events();
            self.events_execution(events);
        }

    }

    fn check_for_events(&mut self) -> &[inotify::wrapper::Event] {
        self.inotify.available_events().unwrap()
    }

    fn events_execution(&self, events: &[inotify::wrapper::Event]) {
        for event in events.iter() {

        }
    }
}

During compilation I am receiving an error

src/main.rs:204:13: 204:17 error: cannot borrow `*self` as immutable because it is also borrowed as mutable [E0502]
src/main.rs:204             self.events_execution(events);

I thought the best solution would be to separate somehow inotify variable in Notificator structure with watch_service, but I can't dereference self.check_for_events(); because I receive

src/main.rs:203:17: 203:27 error: the trait bound `[inotify::wrapper::Event]: std::marker::Sized` is not satisfied [E0277]
src/main.rs:203             let mut events = *self.check_for_events();

I understand the core of the problem: I'm trying to borrow reference by check_for_events and then using it as parameter in events_execution which also requires self as parameter, but I have no idea how to resolve it.

解决方案

One thing you could do, although it's not very elegant, is to have your mutable method consume its mutable borrow and return an immutable one that you can then use:

pub fn resolving_events(&mut self) {
    let (slf, events) = self.check_for_events();
    slf.events_execution(events);

}

fn check_for_events(&mut self) -> (&Self, &[inotify::wrapper::Event]) {
    let events = self.inotify.available_events().unwrap();
    (&self, events)
}

I've made a small proof-of-concept on the playground (using vecs of u64 as the mutable state, but the principle is similar). It might be cleaner to refactor your code so that some external client can (mutably) borrow the Notifier, produce the events, release the borrow, and borrow it (immutably) to process them...

这篇关于在结构中借用参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆