pas*_*ugh 1 overloading generic-programming open-closed-principle rust argument-dependent-lookup
有一个库提供了一个通用函数和一些要使用的实现:
#include <iostream>
namespace lib {
struct Impl1 {};
struct Impl2 {};
void process(Impl1) { std::cout << 1; }
void process(Impl2) { std::cout << 2; }
template<typename T> void generalize(T t) { process(t); }
}
Run Code Online (Sandbox Code Playgroud)
我想通过外部代码扩展它。以下是 C++ 允许这样做的方式:
#include <lib.h> // the previous snippet
namespace client {
struct Impl3 {};
void process(Impl3) { std::cout << 3; }
}
int main() { // test
lib::generalize(client::Impl3{}); // it couts 3
}
Run Code Online (Sandbox Code Playgroud)
注意:lib的代码对 的一无所知,client并且不执行动态调度。如何在我的 Rust 代码中实现相同的目标?(如果我不能,是否有类似计划?)
当然,这正是trait的用途:
pub mod lib {
pub trait Impl {
fn process(&self);
}
pub struct Impl1 {}
pub struct Impl2 {}
impl Impl for Impl1 {
fn process(&self) {
println!("1");
}
}
impl Impl for Impl2 {
fn process(&self) {
println!("2");
}
}
pub fn generalize<T: Impl>(t: T) {
t.process();
}
}
mod client {
pub struct Impl3 {}
impl super::lib::Impl for Impl3 {
fn process(&self) {
println!("3");
}
}
}
fn main() {
lib::generalize(client::Impl3 {});
}
Run Code Online (Sandbox Code Playgroud)
在操场上看到它。