gpt4 book ai didi

Rust 在专用版本中调用函数的默认实现

转载 作者:行者123 更新时间:2023-12-03 11:27:27 25 4
gpt4 key购买 nike

我在 Rust 中有一个特性,它为其功能提供了一些默认实现。

trait MyTrait {
fn do_something(&self);
fn say_hello(&self) {
println!("Hello I am default");
}
}
一些实现者扩展了这个特性并使用提供的默认值
struct MyNormalImplementor {}

impl MyTrait for MyNormalImplementor {
fn do_something(&self) {
// self.doing_some_normal_stuff();
}
}
现在我想要一个扩展特征行为的实现器,但有时仍使用默认实现。当然默认实现更复杂,我想遵循 DRY 原则。
struct MySpecializedImplementor(bool)

impl MyTrait for MySpecializedImplementor {
fn do_something(&self) {
// self.doing_some_wild_stuff();
}
fn say_hello(&self) {
if self.0 {
println!("hey, I am special");
} else {
MyTrait::say_hello(self);
}
}
}
这里 MyTrait::say_hello(self);立即在无限循环中调用专用函数。我没有找到任何方法来限定函数调用,以便 MyTrait 中的默认实现而是调用。有什么方法可以实现这一点,还是我必须为这种情况创建一个代理函数(也将在我的 trait 的公共(public)接口(interface)中)?

最佳答案

独立的通用函数
将默认实现推迟到一个独立的泛型函数:

fn say_hello<T: Trait + ?Sized>(t: &T) {
println!("Hello I am default")
}

trait Trait {
fn say_hello(&self) {
say_hello(self);
}
}

struct Normal;

impl Trait for Normal {}

struct Special(bool);

impl Trait for Special {
fn say_hello(&self) {
if self.0 {
println!("Hey I am special")
} else {
say_hello(self)
}
}
}

fn main() {
let normal = Normal;
normal.say_hello(); // default

let special = Special(false);
special.say_hello(); // default

let special = Special(true);
special.say_hello(); // special
}
playground
两个默认特征方法
另一种方法可能是定义两个特征方法,一个作为默认实现,另一个遵循默认实现,除非它被覆盖:
trait Trait {
fn say_hello_default(&self) {
println!("Hello I am default");
}
fn say_hello(&self) {
self.say_hello_default();
}
}

struct Normal;

impl Trait for Normal {}

struct Special(bool);

impl Trait for Special {
fn say_hello(&self) {
if self.0 {
println!("Hey I am special");
} else {
self.say_hello_default();
}
}
}

fn main() {
let normal = Normal;
normal.say_hello(); // default

let special = Special(false);
special.say_hello(); // default

let special = Special(true);
special.say_hello(); // special
}
playground

默认关联常量
虽然这有点笨拙,但如果将默认实现和专用实现之间的差异缩小到 const值,那么您可以使用默认关联 const您的特质的特质项目:
trait Trait {
const MSG: &'static str = "Hello I am default";
fn say_hello(&self) {
println!("{}", Self::MSG);
}
}

struct Normal;

impl Trait for Normal {}

struct Special(bool);

impl Trait for Special {
const MSG: &'static str = "Hey I am special";
fn say_hello(&self) {
let msg = if self.0 {
Self::MSG
} else {
<Normal as Trait>::MSG
};
println!("{}", msg);
}
}

fn main() {
let normal = Normal;
normal.say_hello(); // default

let special = Special(false);
special.say_hello(); // default

let special = Special(true);
special.say_hello(); // special
}
playground

通过 AsRef 调用默认实现
如果唯一区别 Special来自 Normal是一些额外的字段,而 Special否则类型可以用作 Normal那么你可能想要实现 AsRef<Normal>对于 Special并以这种方式调用默认实现:
trait Trait {
fn say_hello(&self) {
println!("Hello I am default");
}
}

struct Normal;

impl Trait for Normal {}

struct Special(bool);

impl AsRef<Normal> for Special {
fn as_ref(&self) -> &Normal {
&Normal
}
}

impl Trait for Special {
fn say_hello(&self) {
if self.0 {
println!("Hey I am special");
} else {
<Normal as Trait>::say_hello(self.as_ref());
}
}
}

fn main() {
let normal = Normal;
normal.say_hello(); // default

let special = Special(false);
special.say_hello(); // default

let special = Special(true);
special.say_hello(); // special
}
playground

默认宏实现
像往常一样,如果一切都失败了,让你的代码干燥的最暴力的方法是使用宏:
macro_rules! default_hello {
() => {
println!("Hello I am default");
}
}

trait Trait {
fn say_hello(&self) {
default_hello!();
}
}

struct Normal;

impl Trait for Normal {}

struct Special(bool);

impl Trait for Special {
fn say_hello(&self) {
if self.0 {
println!("Hey I am special");
} else {
default_hello!();
}
}
}

fn main() {
let normal = Normal;
normal.say_hello(); // default

let special = Special(false);
special.say_hello(); // default

let special = Special(true);
special.say_hello(); // special
}
playground

关于Rust 在专用版本中调用函数的默认实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66077211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com