proc-macro-workshop: builder-9

审题

// Does your macro still work if some of the standard library prelude item names
// mean something different in the caller's code?
//
// It may seem unreasonable to consider this case, but it does arise in
// practice. Most commonly for Result, where crates sometimes use a Result type
// alias with a single type parameter which assumes their crate's error type.
// Such a type alias would break macro-generated code that expects Result to
// have two type parameters. As another example, Hyper 0.10 used to define
// hyper::Ok as a re-export of hyper::status::StatusCode::Ok which is totally
// different from Result::Ok. This caused problems in code doing `use hyper::*`
// together with macro-generated code referring to Ok.
//
// Generally all macros (procedural as well as macro_rules) designed to be used
// by other people should refer to every single thing in their expanded code
// through an absolute path, such as std::result::Result.

use derive_builder::Builder;

type Option = ();
type Some = ();
type None = ();
type Result = ();
type Box = ();

#[derive(Builder)]
pub struct Command {
    executable: String,
}

fn main() {}

从代码结构上来看,我们就已经能够猜到大致的考点

namespace

因为宏可能使用在任何地方,如果指定含义不明,那么命名就会冲突。

题解

这里无需对代码做任何的改动,因为我们在书写代码的时候始终严格使用绝对路径。
因此这道题考点对我们毫无影响

整体

这个在这里就无需重复粘贴了。

这里之所以还提示一下,一方面是为了强调一下知识点;另一方面,就是正式的对builder做一个完整的结束声明。

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!