Created
August 13, 2016 16:48
-
-
Save colin-kiegel/d38e72cc4d5da2b0611e4dc067f02e94 to your computer and use it in GitHub Desktop.
#3: Should builder methods use `mut self` or `&mut self`?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![crate_type = "lib"] | |
#![crate_name = "bar"] | |
extern crate foo; | |
use foo::Channel; | |
pub fn a<VALUE: Into<i32>>(mut builder: Channel, value: VALUE) -> Channel { | |
builder.a(value.into()).clone() | |
} | |
pub fn b<VALUE: Into<i32>>(mut builder: Channel, value: VALUE) -> Channel { | |
builder.b(value.into()).clone() | |
} | |
pub fn c<VALUE: Into<i32>>(mut builder: Channel, value: VALUE) -> Channel { | |
builder.c(value.into()).clone() | |
} | |
/// consume variables with black_box to avoid optimization of trivial code. | |
#[inline(never)] | |
pub fn black_box(_x: Channel) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![crate_type = "bin"] | |
#![crate_name = "baz"] | |
extern crate foo; | |
use foo::Channel; | |
extern crate bar; | |
fn main() { | |
let mut builder = Channel::default(); | |
builder = bar::a(builder, 1); | |
builder = bar::b(builder, 2); | |
builder = bar::c(builder, 3); | |
let ch = builder.build(); | |
bar::black_box(ch); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![crate_type = "lib"] | |
#![crate_name = "foo"] | |
#[derive(Default, Clone)] | |
pub struct Channel { | |
a: i32, | |
b: i32, | |
c: i32, | |
} | |
impl Channel { | |
pub fn a<VALUE: Into<i32>>(&mut self, value: VALUE) -> &mut Self { | |
self.a = value.into(); | |
self | |
} | |
pub fn b<VALUE: Into<i32>>(&mut self, value: VALUE) -> &mut Self { | |
self.b = value.into(); | |
self | |
} | |
pub fn c<VALUE: Into<i32>>(&mut self, value: VALUE) -> &mut Self { | |
self.c = value.into(); | |
self | |
} | |
} | |
impl Channel { | |
pub fn build(&mut self) -> Self { | |
Channel { | |
a: self.a, | |
b: self.b, | |
c: self.c, | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.text | |
.file "baz.cgu-0.rs" | |
.section .text._ZN3baz4main17h1acab71f10722165E,"ax",@progbits | |
.p2align 4, 0x90 | |
.type _ZN3baz4main17h1acab71f10722165E,@function | |
_ZN3baz4main17h1acab71f10722165E: | |
.cfi_startproc | |
pushq %rbx | |
.Ltmp0: | |
.cfi_def_cfa_offset 16 | |
subq $32, %rsp | |
.Ltmp1: | |
.cfi_def_cfa_offset 48 | |
.Ltmp2: | |
.cfi_offset %rbx, -16 | |
movabsq $8589934593, %rax | |
movq %rax, (%rsp) | |
movl $3, 8(%rsp) | |
leaq 16(%rsp), %rbx | |
leaq (%rsp), %rsi | |
movq %rbx, %rdi | |
callq _ZN3foo7Channel5build17hac6b33bde1470760E@PLT | |
movq %rbx, %rdi | |
callq _ZN3bar9black_box17hec8febb0a338c063E@PLT | |
addq $32, %rsp | |
popq %rbx | |
retq | |
.Lfunc_end0: | |
.size _ZN3baz4main17h1acab71f10722165E, .Lfunc_end0-_ZN3baz4main17h1acab71f10722165E | |
.cfi_endproc | |
.section .text.main,"ax",@progbits | |
.globl main | |
.p2align 4, 0x90 | |
.type main,@function | |
main: | |
.cfi_startproc | |
movq %rsi, %rax | |
movq %rdi, %rcx | |
leaq _ZN3baz4main17h1acab71f10722165E(%rip), %rdi | |
movq %rcx, %rsi | |
movq %rax, %rdx | |
jmp _ZN3std2rt10lang_start17hfe9ab243c60ffb9bE@PLT | |
.Lfunc_end1: | |
.size main, .Lfunc_end1-main | |
.cfi_endproc | |
.section ".note.GNU-stack","",@progbits |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment