Enum rustc_middle::mir::BorrowKind [−][src]
pub enum BorrowKind {
Shared,
Shallow,
Unique,
Mut {
allow_two_phase_borrow: bool,
},
}Variants
Expand description
Data must be immutable and is aliasable.
Expand description
The immediately borrowed place must be immutable, but projections from
it don’t need to be. For example, a shallow borrow of a.b doesn’t
conflict with a mutable borrow of a.b.c.
This is used when lowering matches: when matching on a place we want to ensure that place have the same value from the start of the match until an arm is selected. This prevents this code from compiling:
let mut x = &Some(0); match *x { None => (), Some(_) if { x = &None; false } => (), Some(_) => (), }
This can’t be a shared borrow because mutably borrowing (*x as Some).0
should not prevent if let None = x { ... }, for example, because the
mutating (*x as Some).0 can’t affect the discriminant of x.
We can also report errors with this kind of borrow differently.
Expand description
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 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 an &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.
Expand description
Data is mutable and not aliasable.
Show fields
Fields of Mut
allow_two_phase_borrow: boolExpand description
true if this borrow arose from method-call auto-ref
(i.e., adjustment::Adjust::Borrow).
Implementations
impl BorrowKind[src]
impl BorrowKind[src]pub fn to_mutbl_lossy(self) -> Mutability[src]
impl BorrowKind[src]
impl BorrowKind[src]pub fn allows_two_phase_borrow(&self) -> bool[src]
pub fn describe_mutability(&self) -> String[src]
Trait Implementations
impl Clone for BorrowKind[src]
impl Clone for BorrowKind[src]fn clone(&self) -> BorrowKind[src]
fn clone(&self) -> BorrowKind[src]Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0[src]
fn clone_from(&mut self, source: &Self)1.0.0[src]Performs copy-assignment from source. Read more
impl Debug for BorrowKind[src]
impl Debug for BorrowKind[src]impl<'tcx, __D: TyDecoder<'tcx>> Decodable<__D> for BorrowKind[src]
impl<'tcx, __D: TyDecoder<'tcx>> Decodable<__D> for BorrowKind[src]impl<'tcx, __E: TyEncoder<'tcx>> Encodable<__E> for BorrowKind[src]
impl<'tcx, __E: TyEncoder<'tcx>> Encodable<__E> for BorrowKind[src]impl Hash for BorrowKind[src]
impl Hash for BorrowKind[src]impl<'__ctx> HashStable<StableHashingContext<'__ctx>> for BorrowKind[src]
impl<'__ctx> HashStable<StableHashingContext<'__ctx>> for BorrowKind[src]fn hash_stable(
&self,
__hcx: &mut StableHashingContext<'__ctx>,
__hasher: &mut StableHasher
)[src]
&self,
__hcx: &mut StableHashingContext<'__ctx>,
__hasher: &mut StableHasher
)
impl Ord for BorrowKind[src]
impl Ord for BorrowKind[src]impl PartialEq<BorrowKind> for BorrowKind[src]
impl PartialEq<BorrowKind> for BorrowKind[src]fn eq(&self, other: &BorrowKind) -> bool[src]
fn eq(&self, other: &BorrowKind) -> bool[src]This method tests for self and other values to be equal, and is used
by ==. Read more
fn ne(&self, other: &BorrowKind) -> bool[src]
fn ne(&self, other: &BorrowKind) -> bool[src]This method tests for !=.
impl PartialOrd<BorrowKind> for BorrowKind[src]
impl PartialOrd<BorrowKind> for BorrowKind[src]fn partial_cmp(&self, other: &BorrowKind) -> Option<Ordering>[src]
fn partial_cmp(&self, other: &BorrowKind) -> Option<Ordering>[src]This method returns an ordering between self and other values if one exists. Read more
#[must_use]fn lt(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]fn lt(&self, other: &Rhs) -> bool1.0.0[src]This method tests less than (for self and other) and is used by the < operator. Read more
#[must_use]fn le(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]fn le(&self, other: &Rhs) -> bool1.0.0[src]This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
impl Copy for BorrowKind[src]
impl Eq for BorrowKind[src]
impl StructuralEq for BorrowKind[src]
impl StructuralPartialEq for BorrowKind[src]
Auto Trait Implementations
impl RefUnwindSafe for BorrowKind
impl Send for BorrowKind
impl Sync for BorrowKind
impl Unpin for BorrowKind
impl UnwindSafe for BorrowKind
Blanket Implementations
impl<'tcx, T> ArenaAllocatable<'tcx, ()> for T where
T: Copy, [src]
impl<'tcx, T> ArenaAllocatable<'tcx, ()> for T where
T: Copy, [src]pub fn allocate_on(Self, &'a Arena<'tcx>) -> &'a mut T[src]
pub fn allocate_from_iter(
&'a Arena<'tcx>,
impl IntoIterator<Item = T>
) -> &'a mut [T]ⓘ[src]
&'a Arena<'tcx>,
impl IntoIterator<Item = T>
) -> &'a mut [T]ⓘ
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]pub fn borrow_mut(&mut self) -> &mut T[src]
pub fn borrow_mut(&mut self) -> &mut T[src]Mutably borrows from an owned value. Read more
impl<T> MaybeResult<T> for T[src]
impl<T> MaybeResult<T> for T[src]impl<T> ToOwned for T where
T: Clone, [src]
impl<T> ToOwned for T where
T: Clone, [src]type Owned = T
type Owned = TThe resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
pub fn to_owned(&self) -> T[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)[src]
pub fn clone_into(&self, target: &mut T)[src]🔬 This is a nightly-only experimental API. (toowned_clone_into)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T> WithConstness for T[src]
impl<T> WithConstness for T[src]fn with_constness(self, constness: Constness) -> ConstnessAnd<Self>[src]
fn with_const(self) -> ConstnessAnd<Self>[src]
fn without_const(self) -> ConstnessAnd<Self>[src]
impl<'a, T> Captures<'a> for T where
T: ?Sized, [src]
T: ?Sized,