简体   繁体   English

你如何在生锈中借用一个可变指针?

[英]How do you borrow a mutable pointer in rust?

I have a function that returns a Result<&'a ~Foo, BarErr>, which I can access using: 我有一个返回Result <&'a~Foo,BarErr>的函数,我可以使用它来访问:

match x.borrow() {
  Ok(ref foo) => println!("Found {}", foo.value),
  Err(Nope) => println!("Bleh")
}

However, I find myself now in a more complex situation in which I wish to borrow a mutable reference instead, such that I might be able to call a function on it: 但是,我发现自己现在处于一个更复杂的情况,我希望借用一个可变引用,这样我就可以在其上调用一个函数:

match x.borrow() {
  Ok(ref foo) => { foo.inc(); trace!("Found {}", foo.value); },
  Err(Nope) => trace!("Bleh")
}

I've tried a few variations on where I need to stick 'mut' in, such as mut ref foo, ref mut foo, -> mut Result<...>, -> Result, but I can't seem to figure out the syntax required. 我已经尝试了一些变化,我需要坚持“mut”,例如mut ref foo,ref mut foo, - > mut Result <...>, - > Result,但我似乎无法想象出所需的语法。

I keep getting: 我一直在:

error: cannot borrow immutable dereference of `~`-pointer `***foo` as mutable

What should it be? 它应该是什么?

Complete code example: 完整的代码示例:

macro_rules! trace(
  ($($arg:tt)*) => (
    { let x = ::std::io::stdout().write_line(format_args!(::std::fmt::format, $($arg)*)); println!("{}", x); }
  );
)

#[deriving(Show)]
struct Foo {
  value: int
}

impl Foo {
  fn inc(&mut self) {
    self.value += 1;
  }
}

#[deriving(Show)]
struct Bar {
  data: Option<~Foo>
}

#[deriving(Show)]
enum BarErr {
  Nope
}

impl Bar {
  fn borrow<'a>(&'a mut self) -> Result<&'a ~Foo, BarErr> {
    match self.data {
      Some(ref e) => return Ok(e),
      None => return Err(Nope)
    }
  }
}

#[test]
fn test_create_indirect() {
  let y = ~Foo { value: 10 };
  let mut x = Bar { data: Some(y) };
  let mut x2 = Bar { data: None };
  {
    match x.borrow() {
      Ok(ref mut foo) => { foo.inc(); trace!("Found {}", foo.value); },
      Err(Nope) => trace!("Bleh")
    }
  }
  {
    let z = x2.borrow();
    trace!("Z: {}", z);
  }
}

The problem is that references do not own data, hence their mutability is inherited. 问题是引用不拥有数据,因此它们的可变性是继承的。 You cannot turn &'a into &'a mut , because the data under that reference is immutable. 你不能把&'a变成&'a mut ,因为那个引用下的数据是不可变的。

You have to return Result<&'a mut ~Foo, BarErr> in order to achieve what you want: 你必须返回Result<&'a mut ~Foo, BarErr>才能达到你想要的效果:

impl Bar {
  fn borrow<'a>(&'a mut self) -> Result<&'a mut ~Foo, BarErr> {
    match self.data {
      Some(ref mut e) => return Ok(e),
      None => return Err(Nope)
    }
  }
}

#[test]
fn test_create_indirect() {
  let y = ~Foo { value: 10 };
  let mut x = Bar { data: Some(y) };
  let mut x2 = Bar { data: None };
  {
    match x.borrow() {
      Ok(foo) => { foo.inc(); trace!("Found {:?}", foo.value); },
      Err(Nope) => trace!("Bleh")
    }
  }
  {
    let z = x2.borrow();
    trace!("Z: {:?}", z);
  }
}

Note that at the usage site I'm matching x.borrow() against Ok(foo) , not Ok(ref mut foo) . 请注意,在使用站点我将x.borrow()Ok(foo)匹配,而不是Ok(ref mut foo) That's OK because foo itself is &mut , so you can access &mut self methods through it. 这没关系因为foo本身就是&mut ,所以你可以通过它访问&mut self方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM