Enum rustc::middle::ty::BorrowKindUnstable [-] [+] [src]

pub enum BorrowKind {
    ImmBorrow,
    UniqueImmBorrow,
    MutBorrow,
}

Variants

ImmBorrow

Data must be immutable and is aliasable.

UniqueImmBorrow

Data must be immutable but not aliasable. This kind of borrow cannot currently be expressed by the user and is used only in implicit closure bindings. It is needed when you the closure is borrowing or mutating a mutable referent, e.g.:

let x: &mut isize = ...; let y = || *x += 5;

If we were to try to translate this closure into a more explicit form, we'd encounter an error with the code as written:

struct Env { x: & &mut isize } let x: &mut isize = ...; let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn fn fn_ptr(env: &mut Env) { **env.x += 5; }

This is then illegal because you cannot mutate a &mut found in an aliasable location. To solve, you'd have to translate with an &mut borrow:

struct Env { x: & &mut isize } let x: &mut isize = ...; let y = (&mut Env { &mut x }, fn_ptr); // changed from &x to &mut x fn fn_ptr(env: &mut Env) { **env.x += 5; }

Now the assignment to **env.x is legal, but creating a mutable pointer to x is not because x is not mutable. We could fix this by declaring x as let mut x. This is ok in user code, if awkward, but extra weird for closures, since the borrow is hidden.

So we introduce a "unique imm" borrow -- the referent is immutable, but not aliasable. This solves the problem. For simplicity, we don't give users the way to express this borrow, it's just used when translating closures.

MutBorrow

Data is mutable and not aliasable.

Methods

impl BorrowKind

fn from_mutbl(m: Mutability) -> BorrowKind

fn to_mutbl_lossy(self) -> Mutability

Returns a mutability m such that an &m T pointer could be used to obtain this borrow kind. Because borrow kinds are richer than mutabilities, we sometimes have to pick a mutability that is stronger than necessary so that it at least would permit the borrow in question.

fn to_user_str(&self) -> &'static str

Trait Implementations

impl<'tcx> Repr<'tcx> for BorrowKind

fn repr(&self, _tcx: &ctxt) -> String

Derived Implementations

impl Copy for BorrowKind

impl Decodable for BorrowKind

fn decode<__D: Decoder>(__arg_0: &mut __D) -> Result<BorrowKind, __D>

impl Encodable for BorrowKind

fn encode<__S: Encoder>(&self, __arg_0: &mut __S) -> Result<(), __S>

impl Debug for BorrowKind

fn fmt(&self, __arg_0: &mut Formatter) -> Result

impl Hash for BorrowKind

fn hash<__H: Hasher>(&self, __arg_0: &mut __H)

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher

impl Eq for BorrowKind

impl PartialEq for BorrowKind

fn eq(&self, __arg_0: &BorrowKind) -> bool

fn ne(&self, __arg_0: &BorrowKind) -> bool

impl Clone for BorrowKind

fn clone(&self) -> BorrowKind

fn clone_from(&mut self, source: &Self)