知识卡片
构造器验证任务可推进
线程池大小用 usize 表示时,负数已被类型排除,零个线程仍能传入。因为零线程不能执行任务,构造器应当在产生无效池之前报告错误。示例用 assert! 并在文档的 Panics 节说明条件。若无效输入对调用者可恢复,则可用返回 Result 的构造接口。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 最终项目:构建多线程 Web 服务器 Improving Throughput with a Thread Pool · 查看原书依据
impl ThreadPool { /// Create a new ThreadPool. /// /// The size is the number of threads in the pool. /// ❶ /// # Panics /// /// The `new` function will panic if the size is zero. pub fn new(size: usize) -> ThreadPool { ❷ assert!(size > 0); ThreadPool } --snip-- }
参考来源
- 位置:《The Rust Programming Language(第2版)》第20章 最终项目:构建多线程 Web 服务器(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c20.xhtml`,块 0177)
- 结论依据:清单将正数断言和公开的 panic 条件绑定在同一个构造函数。
- 原始内容:impl ThreadPool { /// Create a new ThreadPool. /// /// The size is the number of threads in the pool. /// ❶ /// # Panics /// /// The `new` function will panic if the size is zero. pub fn new(size: usize) -> ThreadPool { ❷ assert!(size > 0); ThreadPool } --snip-- }