我正在尝试new
使用一个带有两个参数的构造函数,但我似乎无法弄清楚如何执行此操作.这个甚至可能吗?
我现在有一个错误,多个适用的项目在范围内(游乐场)
trait __Constructor1<T> {
fn new(T) -> Self;
}
trait __Constructor2<T, U> {
fn new(T, U) -> Self;
}
enum MixedInts {
SmallInt(i32),
TwoSmallInts(i32, i32),
}
impl __Constructor1<i32> for MixedInts {
fn new(__0: i32) -> MixedInts {
MixedInts::SmallInt(__0)
}
}
impl __Constructor2<i32, i32> for MixedInts {
fn new(__0: i32, __1: i32) -> MixedInts {
MixedInts::TwoSmallInts(__0, __1)
}
}
fn main() {
let x = MixedInts::new(2i32);
let y = MixedInts::new(2i32, 2i32);
}
Run Code Online (Sandbox Code Playgroud)
这在技术上是可行的,但不是以实用的方式.您需要使用Rust的通用函数调用语法,以便消除对其的调用的歧义new
.
fn main() {
let x = <MixedInts as __Constructor1<i32>>::new(2i32);
let y = <MixedInts as __Constructor2<i32, i32>>::new(2i32, 2i32);
}
Run Code Online (Sandbox Code Playgroud)
Iron框架有一个有趣的Modifier模式,我认为它可以实现你想要的.虽然它非常聪明,但它最终会让用户感到困惑.
我建议使用标准库中的From
/特征。Into
#[derive(PartialEq, Eq, Debug)]
enum MixedInts {
SmallInt(i32),
TwoSmallInts(i32, i32),
}
impl From<i32> for MixedInts {
fn from(n: i32) -> Self {
MixedInts::SmallInt(n)
}
}
impl From<(i32, i32)> for MixedInts {
fn from((a, b): (i32, i32)) -> Self {
MixedInts::TwoSmallInts(a, b)
}
}
fn main() {
let x: MixedInts = 2_i32.into();
assert_eq!(x, MixedInts::SmallInt(2));
let y: MixedInts = (2_i32, 2_i32).into();
assert_eq!(y, MixedInts::TwoSmallInts(2, 2));
}
Run Code Online (Sandbox Code Playgroud)
Rust不支持重载的函数/方法.作为一种变通方法,您可以使用元组在单个参数中接收多个值.然后,您可以定义特征并为该单个参数的可接受类型实现它,该函数将简单地委托给特征的实现.
enum MixedInts {
SmallInt(i32),
TwoSmallInts(i32, i32),
}
trait IntoMixedInts {
fn into(self) -> MixedInts;
}
impl MixedInts {
fn new<A>(args: A) -> MixedInts
where A: IntoMixedInts
{
args.into()
}
}
impl IntoMixedInts for i32 {
fn into(self) -> MixedInts {
MixedInts::SmallInt(self)
}
}
impl IntoMixedInts for (i32, i32) {
fn into(self) -> MixedInts {
MixedInts::TwoSmallInts(self.0, self.1)
}
}
fn main() {
let x = MixedInts::new(2i32);
let y = MixedInts::new((2i32, 2i32));
}
Run Code Online (Sandbox Code Playgroud)
注意:在此示例中,您可以使用标准From
和Into
特征,而不是定义自己的特征.但是,它可能不适用于其他特征,因为一致性规则(确保某种类型只能存在特定特征的一个实现的规则).
enum MixedInts {
SmallInt(i32),
TwoSmallInts(i32, i32),
}
impl MixedInts {
fn new<A>(args: A) -> MixedInts
where A: Into<MixedInts>
{
args.into()
}
}
impl From<i32> for MixedInts {
fn from(a: i32) -> MixedInts {
MixedInts::SmallInt(a)
}
}
impl From<(i32, i32)> for MixedInts {
fn from((a, b): (i32, i32)) -> MixedInts {
MixedInts::TwoSmallInts(a, b)
}
}
fn main() {
let x = MixedInts::new(2i32);
let y = MixedInts::new((2i32, 2i32));
}
Run Code Online (Sandbox Code Playgroud)