#Why rust moves bits physically even for release optimization profile

6 messages · Page 1 of 1 (latest)

tame bane
#

In the below example (extracted from https://doc.rust-lang.org/stable/core/pin/index.html#what-is-pinning) rust panics even in release mode.

Why would rust moves bits physically even though trackera is invalid after trackerb owns the value. Isn't this costly operation ?

#[derive(Default)]
struct AddrTracker(Option<usize>);

impl AddrTracker {
    fn check_for_move(&mut self) {
        let current_addr = self as *mut Self as usize;
        match self.0 {
            None => self.0 = Some(current_addr),
            Some(prev_addr) => assert_eq!(prev_addr, current_addr),
        }
    }
}

pub fn main() {
    let mut trackera = AddrTracker::default();
    trackera.check_for_move();

    let mut trackerb = trackera;

    trackerb.check_for_move();
}

strong jolt
#

something to keep in mind is that looking at addresses affects optimization

#

for example, if — actually, never mind, the example I was going to give was invalid

shell herald
#

Why would rust moves bits physically even though trackera is invalid after trackerb owns the value. Isn't this costly operation ?
Option<usize>, and thus AddrTracker, is 16 bytes. &str is also 16 bytes

#

moving 16 bytes between locals is a surprisingly good approximation of free