特征可以为父特征的*某些*方法提供默认实现吗?
Can a trait give default implementation for *some* methods of a parent trait?
假设我们有一个基本特征和一个高级特征如下:
pub trait BasicTrait {
fn key_method(&self);
fn other_method(&self);
}
pub trait AdvancedTrait: BasicTrait {
fn key_method_with_argument(&self, parameter: u32);
}
现在,每次有人实施 AdvancedTrait
时,最有可能实施 BasicTrait::key_method(&self)
的是使用一些默认参数调用 key_method_with_argument
。我如何(惯用地)提供此默认实现,以便任何实现 AdvancedTrait
的人将 (1) 只需要实现 key_method_with_argument
和 BasicTrait
中的任何其他必需方法,以及 (2) 可选择地实施key_method()
并仅在需要时覆盖默认实施?
相关问题:
答案 中提出的 impl
块不起作用,因为代码预计会实现 BasicTrait
.
的所有其他方法
您可以通过明确地将 BasicTrait
方法复制到 AdvancedTrait
中来实现,要求您的用户 仅 实现 AdvancedTrait
,然后对任何实现 AdvancedTrait
的东西做一个全面的实现也实现 BasicTrait
。可能建议给重复的方法一个名称,表明它们仅用于实现 BasicTrait
,并防止模棱两可的调用:
pub trait BasicTrait {
fn key_method(&self);
fn other_method(&self);
}
pub trait AdvancedTrait: BasicTrait {
fn key_method_impl(&self) {
self.key_method_with_argument(0)
}
fn other_method_impl(&self);
fn key_method_with_argument(&self, parameter: u32);
}
impl<T: AdvancedTrait> BasicTrait for T {
fn key_method(&self) {
AdvancedTrait::key_method_impl(self)
}
fn other_method(&self) {
AdvancedTrait::other_method_impl(self)
}
}
您也可以改为调用 key_method_impl
basic_key_method
,存在大量命名选项。
您想要的功能称为 specialization, and unfortunately it is nightly-only and has known soundness holes,所以不要指望它会很快稳定下来。但是,这是它的样子:
#![feature(specialization)]
pub trait BasicTrait {
fn key_method(&self);
fn other_method(&self);
}
pub trait AdvancedTrait: BasicTrait {
fn key_method_with_argument(&self, parameter: u32);
}
default impl<T: AdvancedTrait> BasicTrait for T {
default fn key_method(&self) {
self.key_method_with_argument(12345);
}
}
假设我们有一个基本特征和一个高级特征如下:
pub trait BasicTrait {
fn key_method(&self);
fn other_method(&self);
}
pub trait AdvancedTrait: BasicTrait {
fn key_method_with_argument(&self, parameter: u32);
}
现在,每次有人实施 AdvancedTrait
时,最有可能实施 BasicTrait::key_method(&self)
的是使用一些默认参数调用 key_method_with_argument
。我如何(惯用地)提供此默认实现,以便任何实现 AdvancedTrait
的人将 (1) 只需要实现 key_method_with_argument
和 BasicTrait
中的任何其他必需方法,以及 (2) 可选择地实施key_method()
并仅在需要时覆盖默认实施?
相关问题:
答案 impl
块不起作用,因为代码预计会实现 BasicTrait
.
您可以通过明确地将 BasicTrait
方法复制到 AdvancedTrait
中来实现,要求您的用户 仅 实现 AdvancedTrait
,然后对任何实现 AdvancedTrait
的东西做一个全面的实现也实现 BasicTrait
。可能建议给重复的方法一个名称,表明它们仅用于实现 BasicTrait
,并防止模棱两可的调用:
pub trait BasicTrait {
fn key_method(&self);
fn other_method(&self);
}
pub trait AdvancedTrait: BasicTrait {
fn key_method_impl(&self) {
self.key_method_with_argument(0)
}
fn other_method_impl(&self);
fn key_method_with_argument(&self, parameter: u32);
}
impl<T: AdvancedTrait> BasicTrait for T {
fn key_method(&self) {
AdvancedTrait::key_method_impl(self)
}
fn other_method(&self) {
AdvancedTrait::other_method_impl(self)
}
}
您也可以改为调用 key_method_impl
basic_key_method
,存在大量命名选项。
您想要的功能称为 specialization, and unfortunately it is nightly-only and has known soundness holes,所以不要指望它会很快稳定下来。但是,这是它的样子:
#![feature(specialization)]
pub trait BasicTrait {
fn key_method(&self);
fn other_method(&self);
}
pub trait AdvancedTrait: BasicTrait {
fn key_method_with_argument(&self, parameter: u32);
}
default impl<T: AdvancedTrait> BasicTrait for T {
default fn key_method(&self) {
self.key_method_with_argument(12345);
}
}