Skip to content

Instantly share code, notes, and snippets.

@Centril
Created July 14, 2016 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Centril/c2c8c32b572455b428f0ef3a0b4d09f5 to your computer and use it in GitHub Desktop.
Save Centril/c2c8c32b572455b428f0ef3a0b4d09f5 to your computer and use it in GitHub Desktop.
Rust: Testing if fluent interfaces are optimized away on releases
#![feature(asm)]
#[inline(never)]
fn noop() {
unsafe {
asm!("NOP");
}
}
struct S { x: usize }
impl S {
fn new() -> Self { S { x: 3 } }
#[inline(never)]
fn a( &mut self ) -> &mut Self {
noop();
self.x *= 5;
noop();
self
}
#[inline(never)]
fn b( &mut self ) -> &mut Self {
noop();
self.x /= 17;
noop();
self
}
}
fn main() {
let n: u32 = "10".parse().unwrap_or( 13 );
let mut s = S::new();
for _ in 0..n {
s.a().b().a();
}
for _ in 0..n {
s.a();
s.b();
s.a();
}
println!( "{}", s.x );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment