]> git.lizzy.rs Git - rust.git/blob - src/librustc/benches/dispatch.rs
Auto merge of #49715 - Mark-Simulacrum:deny-warnings, r=alexcrichton
[rust.git] / src / librustc / benches / dispatch.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use test::Bencher;
12
13 // Static/dynamic method dispatch
14
15 struct Struct {
16     field: isize
17 }
18
19 trait Trait {
20     fn method(&self) -> isize;
21 }
22
23 impl Trait for Struct {
24     fn method(&self) -> isize {
25         self.field
26     }
27 }
28
29 #[bench]
30 fn trait_vtable_method_call(b: &mut Bencher) {
31     let s = Struct { field: 10 };
32     let t = &s as &Trait;
33     b.iter(|| {
34         t.method()
35     });
36 }
37
38 #[bench]
39 fn trait_static_method_call(b: &mut Bencher) {
40     let s = Struct { field: 10 };
41     b.iter(|| {
42         s.method()
43     });
44 }