知识卡片
框架按语法类型读写
Rocket 是 Rust Web 框架,使用属性标记 HTTP 路由,例如 #[get("/add/endpoint")]。书中列出的 Rocket 源码片段展示框架中两种可复用的职责划分。输入侧,Invocation 的 Parse 实现读取不同语法节点,并在指定边界要求逗号。每次读取用 ? 上报解析失败,调用者得到解析错误。输出侧,UriExpr 的 ToTokens 识别当前是 URI 还是表达式,再委托对应节点写入同一个流。两段源码分别示范输入和输出的组织方式,各自可迁移到有多个语法形态的宏。
相关卡片 [[用解析特征定义输入语法]]、[[用字段模型委托输出]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Making fields public with attribute macros 框架中的属性宏与解析委托 · 查看原书依据
There are also lots of examples of libraries using the Parse and ToTokens traits. Here are some examples from Rocket, a web framework that uses a lot of macros for generating endpoints, among other things.
参考来源
- 位置:《Write Powerful Rust Macros》第04章《Making fields public with attribute macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-4.xhtml#p115)
- 结论依据:第 4.8 节引入 Rocket 的端点属性,源码例对照 Invocation 的逐段解析与 UriExpr 的分支委托,原文指出 ? 比教学代码中的 unwrap 更适合报告失败。
- 原始内容:There are also lots of examples of libraries using the Parse and ToTokens traits. Here are some examples from Rocket, a web framework that uses a lot of macros for generating endpoints, among other things.