Last active
August 13, 2016 17:04
-
-
Save colin-kiegel/7aaf4ec60aff8ace360457f96ffc5e02 to your computer and use it in GitHub Desktop.
#2: 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
mod builder { | |
#[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, | |
} | |
} | |
} | |
} | |
mod foo { | |
use super::builder::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)] | |
fn black_box(_x: builder::Channel) { | |
} | |
fn main() { | |
let mut builder = builder::Channel::default(); | |
builder = foo::a(builder, 1); | |
builder = foo::b(builder, 2); | |
builder = foo::c(builder, 3); | |
let ch = builder.build(); | |
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
; ModuleID = 'rust_out.0.rs' | |
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" | |
target triple = "x86_64-unknown-linux-gnu" | |
%"builder::Channel" = type { i32, i32, i32 } | |
; Function Attrs: noinline norecurse nounwind uwtable | |
define internal fastcc void @_ZN8rust_out9black_box17h4fbb43c364fffe15E(%"builder::Channel"* noalias nocapture dereferenceable(12) %_x) unnamed_addr #0 { | |
entry-block: | |
%0 = bitcast %"builder::Channel"* %_x to i8* | |
tail call void @llvm.lifetime.end(i64 12, i8* %0) | |
ret void | |
} | |
; Function Attrs: argmemonly nounwind | |
declare void @llvm.lifetime.end(i64, i8* nocapture) #1 | |
; Function Attrs: norecurse nounwind uwtable | |
define internal void @_ZN8rust_out4main17h4bd17b1d5c1469adE() unnamed_addr #2 { | |
entry-block: | |
%arg3 = alloca %"builder::Channel", align 8 | |
%0 = bitcast %"builder::Channel"* %arg3 to i8* | |
call void @llvm.lifetime.start(i64 12, i8* %0) | |
%1 = getelementptr inbounds %"builder::Channel", %"builder::Channel"* %arg3, i64 0, i32 0 | |
store i32 1, i32* %1, align 8 | |
%2 = getelementptr inbounds %"builder::Channel", %"builder::Channel"* %arg3, i64 0, i32 1 | |
store i32 2, i32* %2, align 4 | |
%3 = getelementptr inbounds %"builder::Channel", %"builder::Channel"* %arg3, i64 0, i32 2 | |
store i32 3, i32* %3, align 8 | |
call fastcc void @_ZN8rust_out9black_box17h4fbb43c364fffe15E(%"builder::Channel"* noalias nocapture nonnull dereferenceable(12) %arg3) | |
call void @llvm.lifetime.end(i64 12, i8* %0) | |
ret void | |
} | |
; Function Attrs: argmemonly nounwind | |
declare void @llvm.lifetime.start(i64, i8* nocapture) #1 | |
define i64 @main(i64, i8**) unnamed_addr { | |
top: | |
%2 = tail call i64 @_ZN3std2rt10lang_start17hbcefdc316c2fbd45E(i8* bitcast (void ()* @_ZN8rust_out4main17h4bd17b1d5c1469adE to i8*), i64 %0, i8** %1) | |
ret i64 %2 | |
} | |
declare i64 @_ZN3std2rt10lang_start17hbcefdc316c2fbd45E(i8*, i64, i8**) unnamed_addr | |
attributes #0 = { noinline norecurse nounwind uwtable } | |
attributes #1 = { argmemonly nounwind } | |
attributes #2 = { norecurse nounwind uwtable } |
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 "aio.cgu-0.rs" | |
.section .text._ZN3aio4main17h1a1276b84a14ae38E,"ax",@progbits | |
.p2align 4, 0x90 | |
.type _ZN3aio4main17h1a1276b84a14ae38E,@function | |
_ZN3aio4main17h1a1276b84a14ae38E: | |
.cfi_startproc | |
retq | |
.Lfunc_end0: | |
.size _ZN3aio4main17h1a1276b84a14ae38E, .Lfunc_end0-_ZN3aio4main17h1a1276b84a14ae38E | |
.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 _ZN3aio4main17h1a1276b84a14ae38E(%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 |
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 "rust_out.0.rs" | |
.section .text._ZN8rust_out9black_box17h4fbb43c364fffe15E,"ax",@progbits | |
.align 16, 0x90 | |
.type _ZN8rust_out9black_box17h4fbb43c364fffe15E,@function | |
_ZN8rust_out9black_box17h4fbb43c364fffe15E: | |
.cfi_startproc | |
retq | |
.Lfunc_end0: | |
.size _ZN8rust_out9black_box17h4fbb43c364fffe15E, .Lfunc_end0-_ZN8rust_out9black_box17h4fbb43c364fffe15E | |
.cfi_endproc | |
.section .text._ZN8rust_out4main17h4bd17b1d5c1469adE,"ax",@progbits | |
.align 16, 0x90 | |
.type _ZN8rust_out4main17h4bd17b1d5c1469adE,@function | |
_ZN8rust_out4main17h4bd17b1d5c1469adE: | |
.cfi_startproc | |
subq $24, %rsp | |
.Ltmp0: | |
.cfi_def_cfa_offset 32 | |
movabsq $8589934593, %rax | |
movq %rax, 8(%rsp) | |
movl $3, 16(%rsp) | |
leaq 8(%rsp), %rdi | |
callq _ZN8rust_out9black_box17h4fbb43c364fffe15E | |
addq $24, %rsp | |
retq | |
.Lfunc_end1: | |
.size _ZN8rust_out4main17h4bd17b1d5c1469adE, .Lfunc_end1-_ZN8rust_out4main17h4bd17b1d5c1469adE | |
.cfi_endproc | |
.section .text.main,"ax",@progbits | |
.globl main | |
.align 16, 0x90 | |
.type main,@function | |
main: | |
.cfi_startproc | |
movq %rsi, %rax | |
movq %rdi, %rcx | |
leaq _ZN8rust_out4main17h4bd17b1d5c1469adE(%rip), %rdi | |
movq %rcx, %rsi | |
movq %rax, %rdx | |
jmp _ZN3std2rt10lang_start17hbcefdc316c2fbd45E@PLT | |
.Lfunc_end2: | |
.size main, .Lfunc_end2-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