简体   繁体   English

即使实现了Trait <&str>,为什么仍无法满足特质绑定的Trait <&String>?

[英]Why is the trait bound Trait<&String> not satisfied even though I've implemented Trait<&str>?

I have written the following code: 我写了以下代码:

pub struct Serializer;

pub trait Serialize<T> {
    fn to_bert(&self, data: T) -> Vec<u8>;
}

pub trait Convert<T> {
    fn to_binary(&self, data: T) -> Vec<u8>;
}

impl<'a> Convert<&'a str> for Serializer {
    fn to_binary(&self, data: &'a str) -> Vec<u8> {
        let binary_string = data.as_bytes();
        let binary_length = binary_string.len() as i16;
        let mut binary = vec![];
        binary.write_i16::<BigEndian>(binary_length).unwrap();
        binary.extend(binary_string.iter().clone());
        binary
    }
}

impl Serialize<String> for Serializer {
    fn to_bert(&self, data: String) -> Vec<u8> {
        let binary_string = self.to_binary(&data);
        self.generate_term(BertTag::String, binary_string)
    }
}

impl<'a> Serialize<&'a str> for Serializer {
    fn to_bert(&self, data: &'a str) -> Vec<u8> {
        let binary_string = self.to_binary(data);
        self.generate_term(BertTag::String, binary_string)
    }
}

When compiling, I receive an error which says the compiler cannot find the right function for a call: 编译时,我收到一条错误消息,提示编译器找不到适合调用的函数:

error: the trait bound `serializers::Serializer: serializers::Convert<&std::string::String>` is not satisfied [E0277]
let binary_string = self.to_binary(&data);
                         ^~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
help: the following implementations were found:
help:   <serializers::Serializer as serializers::Convert<&'a str>>
help:   <serializers::Serializer as serializers::Convert<types::BertType>>

Why can the compiler not find the right implementation when I've specified &str with a lifetime? 当我指定了生命周期的&str时,为什么编译器找不到正确的实现? How can I fix this? 我怎样才能解决这个问题?

How can I fix this? 我怎样才能解决这个问题?

By getting a &str from data : 通过从data获取&str

let binary_string = self.to_binary(&*data);

Like the error says, Convert<&String> is not implemented for Serializer , but Convert<&str> is. 就像错误所说的那样, Convert<&String>不是为Serializer实现的,而Convert<&str>是。 &data is a &String , but you need a &str , writing &*data produces a &str . &data是一个&String ,但是您需要一个&str ,编写&*data会产生一个&str


Note that in general (when the string is not changed) it should not be necessary to implement the same functionality for both String and &str , only the &str version should be enough. 请注意,通常(当不更改字符串时)不必为String&str实现相同的功能,只需&str版本就足够了。 In your example, you can remove the Serialize<String> implementation for Serializer and use Serialize<&str> instead. 在你的榜样,您可以删除Serialize<String>实施Serializer并使用Serialize<&str>代替。

fn main() {
    let mut s = Serializer;
    let a: String = "a".to_string();
    // autoderef &String to &str and call Serialize<&str>::to_bert()
    // I don't know why the autoderef does not work in your original example
    s.to_bert(&a);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM