简体   繁体   English

如何为实现特征的所有类型实现From特质,但对某些类型使用特定的实现?

[英]How can I implement the From trait for all types implementing a trait but use a specific implementation for certain types?

I am implementing the std::convert::From trait for a struct containing a Cow<str> . 我正在为包含Cow<str>的结构实现std::convert::From特征。 Is there a way to use the same implementation for all different kinds of integers ( u8 , u16 , u32 , usize and so on)? 有没有办法使用了各种不同类型相同的整数执行(的方式u8u16u32usize等)?

use std::borrow::Cow;

pub struct Luhn<'a> {
    code: Cow<'a, str>,
}

I can easily implement code for all integers using a trait bound on the ToString trait, but then I cannot use a specific implementation for str and String - this way the benefits of Cow cannot be exploited. 我可以使用绑定在ToString特征上的特征来轻松地为所有整数实现代码,但是随后我无法对strString使用特定的实现-这样就无法利用Cow的好处。 When I write specific implementations for str and String , I get a compile error: 当我为strString编写特定的实现时,出现编译错误:

 error[E0119]: conflicting implementations of trait `std::convert::From<&str>` for type `Luhn<'_>`: --> src/lib.rs:23:1 | 7 | impl<'a> From<&'a str> for Luhn<'a> { | ----------------------------------- first implementation here ... 23 | impl<'a, T: ToString> From<T> for Luhn<'a> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Luhn<'_>` 

I understand that this is due to the fact that Rust does not offer function overloading. 我知道这是由于Rust不提供函数重载这一事实。 How could this be solved in an elegant way? 如何以一种优雅的方式解决这个问题?

impl<'a> From<&'a str> for Luhn<'a> {
    fn from(input: &'a str) -> Self {
        Luhn {
            code: Cow::Borrowed(input),
        }
    }
}

impl<'a> From<String> for Luhn<'a> {
    fn from(input: String) -> Self {
        Luhn {
            code: Cow::Owned(input),
        }
    }
}

impl<'a, T: ToString> From<T> for Luhn<'a> {
    fn from(input: T) -> Self {
        Luhn {
            code: Cow::Owned(input.to_string()),
        }
    }
}

Since &str and String both implement ToString , you can use the unstable specialization feature: 由于&strString都实现ToString ,因此可以使用不稳定的专业化功能:

#![feature(specialization)]

use std::borrow::Cow;

pub struct Luhn<'a> {
    code: Cow<'a, str>,
}

impl<'a, T: ToString> From<T> for Luhn<'a> {
    default fn from(input: T) -> Self {
//  ^^^^^^^
        Luhn {
            code: Cow::Owned(input.to_string()),
        }
    }
}

impl<'a> From<&'a str> for Luhn<'a> {
    fn from(input: &'a str) -> Self {
        Luhn {
            code: Cow::Borrowed(input),
        }
    }
}

impl<'a> From<String> for Luhn<'a> {
    fn from(input: String) -> Self {
        Luhn {
            code: Cow::Owned(input),
        }
    }
}

That being said, you cannot implement Display for Luhn because you'd run into How is there a conflicting implementation of `From` when using a generic type? 话虽如此,您不能实现Display for Luhn因为您在使用泛型类型时会遇到“ From”的冲突实现吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为实现 trait 的所有类型实现 trait - Implement a trait for all types implementing a trait 如果两个特征都是为引用实现的,那么如何为实现特征A的所有类型实现特征B? - How can I implement trait B for all types that implement trait A if both traits are implemented for references? 如何为实现特定特征的所有类型批量实现反序列化? - How can I mass implement Deserialize for all types that implement a specific trait? 如何为实现另一个特征的类型实现特征而不冲突的实现 - How to implement trait for types implementing another trait without conflicting implementations 为特定类型实现特征方法 - Implementing trait methods for specific types 无法为实现我拥有的特征的所有类型实现我不拥有的特征 - Can't implement a trait I don't own for all types that implement a trait I do own 如何对包含不同类型的链表进行静态调度,所有类型都实现了一个特征? - How can I have static dispatch for a linked list containing different types all implementing a trait? 如何获得在Rust中实现特定特征的类型列表? - How can I get a list of types that implement a particular trait in Rust? 如何为自定义错误类型实现From trait? - How to implement From trait for custom error types? 如何为实现该特征的现有类型的枚举实现范围内的特征? - How can I implement a trait in scope for an enum of existing types for which the trait is implemented?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM