< 返回版块

howbigsea 发表于 2021-06-05 17:56

Tags:pub crate

在阅读开源代码的时候遇到多次 pub(crate) 语法,不知是啥意思?

举例 在mod.rs中:

pub(crate) use task_local::LocalsMap;

举例2:

#[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
pub struct TaskId(pub(crate) usize);

impl TaskId {
    /// Generates a new `TaskId`.
    pub(crate) fn generate() -> TaskId {
        // TODO: find a good version to emulate u64 atomics on 32 bit systems.
        static COUNTER: AtomicUsize = AtomicUsize::new(1);

        let id = COUNTER.fetch_add(1, Ordering::Relaxed);
        if id > usize::max_value() / 2 {
            std::process::abort();
        }
        TaskId(id)
    }
}

impl fmt::Display for TaskId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

评论区

写评论
作者 howbigsea 2021-06-05 18:56

// pub(crate) 使得函数只在当前 crate 中可见

多谢!

1 共 2 条评论, 1 页