Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 使用静态生存期触发器“;闭包可能比函数“更有效”;当self在闭包中被借用时_Rust_Borrow Checker - Fatal编程技术网

Rust 使用静态生存期触发器“;闭包可能比函数“更有效”;当self在闭包中被借用时

Rust 使用静态生存期触发器“;闭包可能比函数“更有效”;当self在闭包中被借用时,rust,borrow-checker,Rust,Borrow Checker,我的程序使用变量的内存地址作为唯一标识符。我知道这非常难看,但这是一种非常轻量级的获取唯一标识符的方法只有当我将这些变量设为静态变量,使其唯一id(即地址)永远存在时,此模式才有效。这意味着我有几个函数需要引用一个静态的 我使用的是,它提供了一种方法,将处理器置于允许函数在无中断的关键部分中运行的状态。这是由一个函数完成的,该函数将调用包装到需要在关键部分中使用适当的程序集调用执行的函数 在这个精心设计的示例中,包装函数被称为run\u In\u special\u state。我需要在特殊状态

我的程序使用变量的内存地址作为唯一标识符。我知道这非常难看,但这是一种非常轻量级的获取唯一标识符的方法只有当我将这些变量设为静态变量,使其唯一id(即地址)永远存在时,此模式才有效。这意味着我有几个函数需要引用一个静态的

我使用的是,它提供了一种方法,将处理器置于允许函数在无中断的关键部分中运行的状态。这是由一个函数完成的,该函数将调用包装到需要在关键部分中使用适当的程序集调用执行的函数

在这个精心设计的示例中,包装函数被称为
run\u In\u special\u state
。我需要在特殊状态下执行
foo
方法。但是,它需要一个
静态设计的
。下面是一个说明错误的示例:

fn foo(_: &'static Contrived) {}

fn run_in_special_state<F, R>(f: F) -> R
where
    F: FnOnce() -> R,
{
    // Some stuff happens before the function
    let r = f();
    // Some stuff happens after the function
    r
}

struct Contrived {
    value: u32,
}

impl Contrived {
    fn func(&'static mut self) {
        run_in_special_state(|| foo(self));

        self.value = 6;
    }
}

static mut INSTANCE: Contrived = Contrived { value: 4 };

fn main() {
    unsafe { INSTANCE.func() };
}
我知道
FnOnce
将在
run\u in\u special\u state
退出之前被调用。我相信这也意味着闭包不会比当前函数(
func
?)更有效,因为它(闭包)将在当前函数(
func
)退出之前执行并丢弃。如何将此信息传达给借阅检查器?这里还有别的事吗?我注意到,如果我在
foo
上删除
静态
要求,错误就会消失


我无法执行建议的修复,因为我需要在调用
run\u in\u special\u state
后使用
self

这些函数的签名:

  • fn-foo(&'static-artived)

  • fn函数(&'static mut self)

需要在整个程序期间借用其值的引用,而您需要借用其值足够长的引用

删除“静态”
,程序将编译:

fn foo(_: &Contrived) {}

fn run_in_special_state<F, R>(f: F) -> R
where
    F: FnOnce() -> R,
{
    // Some stuff happens before the function
    let r = f();
    // Some stuff happens after the function
    r
}

struct Contrived {
    value: u32,
}

impl Contrived {
    fn func(&mut self) {
        run_in_special_state(|| foo(self));

        self.value = 6;
    }
}

static mut INSTANCE: Contrived = Contrived { value: 4 };

fn main() {
    unsafe { INSTANCE.func() };
}

contriveId
也可以使用
和静态T
mod key {
    use super::Contrived;

    #[derive(Debug, Hash)]
    pub struct ContrivedId(usize);

    impl ContrivedId {
        pub fn new(r: &'static Contrived) -> Self {
            ContrivedId(r as *const _ as usize)
        }
    }
}

use key::ContrivedId;

fn foo(_: ContrivedId) {}

fn run_in_special_state<F, R>(f: F) -> R
where
    F: FnOnce() -> R,
{
    // Some stuff happens before the function
    let r = f();
    // Some stuff happens after the function
    r
}

pub struct Contrived {
    value: u32,
}

impl Contrived {
    fn func(&mut self, id: ContrivedId) {
        run_in_special_state(|| foo(id));

        self.value = 6;
    }
}

static mut INSTANCE: Contrived = Contrived { value: 4 };

fn main() {
    unsafe {
        let id = ContrivedId::new(&INSTANCE);
        INSTANCE.func(id)
    };
}