rust - 在公共(public)函数中使用私有(private)类型的最佳方法是什么?

标签 rust private public

我有以下代码:

use std::convert::{From, Into};

#[derive(PartialEq, Debug)]
enum FindBy<'f> {
    U(&'f usize),
    S(&'f str),
    ST(&'f String),
}

impl<'f> From<&'f usize> for FindBy<'f> {
    fn from(v: &'f usize) -> Self {
        Self::U(v)
    }
}

impl<'f> From<&'f str> for FindBy<'f> {
    fn from(v: &'f str) -> Self {
        Self::S(v)
    }
}

impl TileSet {
    pub fn find<'r, 'ts: 'r, K: Into<FindBy<'r>>>(&'ts self, key: K) -> &'r Tile {
        match key.into() {
            FindBy::S(k) => &self.list.get(k).unwrap(),
            FindBy::ST(k) => &self.list.get(k).unwrap(),
            FindBy::U(k) => match &self.list.get_index(*k) {
                Some((_, v)) => &v,
                _ => todo!(),
            },
        }
    }
}

导致此警告:

warning: private type `prelude::sys::element::tile_set::FindBy<'r>` in public interface (error E0446)
  --> src/sys/element/tile_set.rs:46:5
   |
46 | /     pub fn find<'r, 'ts: 'r, K: Into<FindBy<'r>>>(&'ts self, key: K) -> &'r Tile {
47 | |         match key.into() {
48 | |             FindBy::S(k) => &self.list.get(k).unwrap(),
49 | |             FindBy::ST(k) => &self.list.get(k).unwrap(),
...  |
54 | |         }
55 | |     }
   | |_____^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>

FindBy永远不会暴露 - 它的目的是提供一个白名单以允许一个参数采用多种类型,但类型本身从来不打算在外部使用,仅内部使用,但它提示公共(public)接口(interface)中的私有(private)类型。

请允许我澄清一下,FindBy永远不会在它所在的模块/文件之外使用,但它是函数签名的一部分,并且函数是 public

我不想暴露FindBy但它从来都不是这样,但 Rust 提示说,因为它在公共(public)函数中用于为参数提供类型白名单。

解决这个问题的最佳方法是什么?

最佳答案

将参数限制为几种可能类型之一的通常解决方案是使用 Sealed traits .

因此,对于您的 find 函数,您可以拥有一个特征 FindBy (正如链接中所解释的那样密封的,因此没有其他人可以实现它),它封装了每种类型的不同逻辑,大致如下(未测试):

impl TileSet {
    pub fn find<K: FindBy>(&self, key: K) -> &Tile {
        key.find_in(self)
    }
}

pub trait FindBy: private::Sealed {
    fn find_in<'ts>(self, _: &'ts TileSet) -> &'ts Tile;
}

impl FindBy for &'_ usize {
    fn find_in(self, tileset: &'ts TileSet) -> &'ts Tile {
        match &tileset.list.get_index(*self) {
            Some((_, v)) => &v,
            _ => todo!(),
        }
    }
}

// impl FindBy for &'_ str { ... }
// impl FindBy for &'_ String { ... }

mod private {
    pub trait Sealed {}

    impl Sealed for &'_ usize {}
    impl Sealed for &'_ str {}
    impl Sealed for &'_ String {}
}

如果您希望该方法(我称为 find_in)只能通过 TileSet::find 使用,您也可以将该方法移至私有(private)特征。另外,您可能需要考虑实现 usize 的特征,而不是 &'_ usize (但也许您有充分的理由将其作为引用)。

关于rust - 在公共(public)函数中使用私有(private)类型的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60417334/

相关文章:

floating-point - 如何解码和编码 Rust 中的 float ?

rust - wgpu-rs : Putting a Matrix3 into a vertex shader results in odd behavior but using a Matrix4 works fine

java - 混淆何时使用私有(private)字段与 protected 字段

module - 如何从子模块中的结构访问私有(private)字段?

java - 如何在 Android Java 中创建一个介于最小值和最大值之间的随机数,不包括介于最小值和最大值之间的一些数字?

rust - 我应该在新类型上实现 AddAssign 吗?

compilation - 我怎样才能激活我所有箱子中的功能?

从 child 访问 PHP 私有(private)变量

c++ - 私有(private)/保护变量 "error: within this context"

android - 在 Android 中导出 Activity 是否允许其他应用程序也从该 Activity 调用任何公共(public)方法?