我有一个名为 Frame
的基本结构这对一堆计算很有用:。
pub struct Frame<T> {
grid_val: Vec<T>,
grid_space: Vec<[T; 2]>,
calculated_result: Option<Vec<T>>
}
Frame
可以用来描述最基本的计算,但有时会出现更复杂的问题,我需要添加更多的几何信息。所以我对每个几何体都使用了组合:
pub struct Sphere<T> {
grid: Frame<T>,
radius: T
}
pub struct Hyperbola<T> {
top_grid: Frame<T>,
bottom_grid: Frame<T>,
internal_angle: T
}
现在我有了一个有效的 Algorithm
实现对于 Sphere
:
pub trait Algorithm<T> {
fn calculate_something(&self) -> Result<Sphere<T>, Error>
}
impl Algorithm<T> for Hyperbola {
// do things with top_grid, bottom_grid, and internal_angle
}
impl Algorithm<T> for Sphere {
// do things with grid and radius
}
这里填写calculated_result
并返回一个新的 Sphere
.它以这种方式实现,因为 Algorithm
需要使用额外的几何信息来计算 calculated_result
— 从语义上讲,它是几何上的一个实现更有意义,其结果恰好与一个或多个 Frame
相关联
我想实现相同的 Algorithm
对于 Hyperbola
.其实已经很接近了,trait 一样是有道理的,但是返回一个Sphere<T>
就没有意义了.
我知道我可以添加另一个特征,例如 GeometricObject
并添加另一层构图,但这似乎过多。我想我也许可以使用 Box
,但这看起来很笨拙。
我也想到了calculate_something
返回 Vec<T>
手动插入到正在使用的任何结构中,但是返回调用该方法的相同结构类型的人体工程学被破坏了(这在公共(public) impl/trait 中是一种浪费)。
我怎样才能把它组织起来而不让它一直向下发展?
最佳答案
看来您需要一个关联类型:
pub trait Algorithm<T> {
type Output;
fn calculate_something(&self) -> Result<Self::Output, Error>;
}
impl<T> Algorithm<T> for Sphere<T> {
type Output = Sphere<T>;
fn calculate_something(&self) -> Result<Self::Output, Error> {
unimplemented!()
}
}
impl<T> Algorithm<T> for Hyperbola<T> {
type Output = Hyperbola<T>;
fn calculate_something(&self) -> Result<Self::Output, Error> {
unimplemented!()
}
}
关联类型 are described in detail在 The Rust Programming Language .我强烈建议通读整本书以熟悉 Rust 必须提供哪些类型的功能。
另一种解决方案是在特征上定义另一个泛型:
pub trait Algorithm<T, Out = Self> {
fn calculate_something(&self) -> Result<Out, Error>;
}
impl<T> Algorithm<T> for Sphere<T> {
fn calculate_something(&self) -> Result<Sphere<T>, Error> {
unimplemented!()
}
}
impl<T> Algorithm<T> for Hyperbola<T> {
fn calculate_something(&self) -> Result<Hyperbola<T>, Error> {
unimplemented!()
}
}
然后你需要决定When is it appropriate to use an associated type versus a generic type?
关于polymorphism - "polymorphic"返回的 Rust 特征的简单组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41352088/