Out of the Box Dynamic Dispatch

栏目: IT技术 · 发布时间: 5年前

内容简介:Rust’s generics give us a whole lot of flexibility. A method that takes a trait bound argument does not need to care about the actual type of the argument it is called with. For example:However, this will monomorphize the method: For eachThis makes each ca

Rust’s generics give us a whole lot of flexibility. A method that takes a trait bound argument does not need to care about the actual type of the argument it is called with. For example:

fn parse_read(r: impl Read) -> MyParseableType {
    todo!();
}

However, this will monomorphize the method: For each Read instance, one instance will be created, potentially ballooning up the code size and increasing compile time. But Rust gives us dynamic dispatch, too, e.g.

fn parse_read(r: &mut dyn Read) -> MyParseableType {
    todo!();
}

This makes each call on r dynamic . Only one version of this method needs to be created, but whenever r ’s methods are called, the method must be looked up at runtime. This is done by having one static data per type that tells us where to find the respective methods. This so-called vtable is referenced whenever we need dynamic dispatch.

Now let’s say we want to pass our Read instance through to the method. For our example, we want to use Stdin or some File .

let readable = todo!();
parse_read(readable)

How should we initialize readable ? The novice’s initial attempt is foiled by the type system:

let readable = if arg == "-" {
    io::stdin()
} else {
    fs::File::open(arg)?
};

The type checker is having none of it.

error[E0308]: if and else have incompatible types
 --> src/main.rs:5:9
  |
2 |       let readable = if arg == "-" {
  |  _____________________-
3 | |         std::io::stdin()
  | |         ---------------- expected because of this
4 | |     } else {
5 | |         std::fs::File::open(arg)?
  | |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::io::Stdin`, found struct `std::fs::File`
6 | |     };
  | |_____- if and else have incompatible types

error: aborting due to previous error

Now the novice asks around and finds that Box es can hold dynamic types.

let readable = if arg == "-" {
    Box::new(io::stdin())
} else {
    Box::new(fs::File::open(arg)?)
};

This still doesn’t work, because we haven’t told the compiler to make the type dynamic. And alas, the error message isn’t quite helpful. We can annotate the type to make it work:

let readable: Box<dyn io::Read> = if arg == "-" {
    Box::new(io::stdin())
} else {
    Box::new(fs::File::open(arg)?)
};

Box can have a little dynamic dispatch, as a treat. This will store the vtable pointer along with our Read instance in the Box ’s allocated memory.

However, we are Rustaceans, so we don’t want to Box to get dynamic dispatch. What we need are two places to hold our Read instances that live long enough to use them:

// these must live longer than `readable`
let (mut stdin_read, mut file_read);
let readable: &mut dyn io::Read = if arg == '-' {
    stdin_read = io::stdin();
    &mut stdin_read
} else {
    file_read = fs::File::open(arg)?;
    &mut file_read
};

The readable is out of the box! What happens is that we declare two stack slots that may alternately stay uninitialized or used, depending on arg . Then we take a &mut dyn mutable dynamic reference on it, which means the type will have a non-null pointer to the object (either stdin_read or file_read , depending on the branch taken) and a second reference to the vtable of either the Stdin type or the File type.

As an aside, this technique can also be used to create an Option<&T> where we don’t want to initialize the T unless needed:

let t_holder;
let opt_t = if has_t {
    t_holder = get_t();
    Some(&t_holder)
} else {
    None
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

GUI设计禁忌2.0

GUI设计禁忌2.0

Jeff Johnson / 盛海艳 等 / 机械工业出版社 / 2008 / 49.00元

本书描述软件开发人员在设计图形用户界面(GUI)时经常犯的“禁忌”,并提出避免这些错误的基本原则和理论依据。本书将GUI禁忌分为7种类型:GUI控件禁忌、导航禁忌、文字禁忌、图形设计和布局禁忌、交互禁忌、响应性禁忌以及管理禁忌,并分别进行详述。 本书编排独特,条理清晰,针对性极强,是不可多得的GUI设计优秀资源。本书适合软件开发人员、web站点设计人员、开发经理、用户界面设计人员等阅读。一起来看看 《GUI设计禁忌2.0》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具