簡體   English   中英

為實現 trait 的所有類型實現 trait

[英]Implement a trait for all types implementing a trait

我有這個問題:

  • 實現 trait Event多個結構
  • 都可以用同樣的方式實現PartialEq trait

我考慮過寫這個(簡短版本)

type Data = Vec<u8>;

trait Event {
    fn data(&self) -> &Data;
}

struct NoteOn {
    data: Data,
}
struct NoteOff {
    data: Data,
}
impl Event for NoteOn {
    fn data(&self) -> &Data {
        &self.data
    }
}
impl Event for NoteOff {
    fn data(&self) -> &Data {
        &self.data
    }
}
impl<T: Event> PartialEq for T {
    fn eq(&self, b: &T) -> bool {
        self.data() == b.data()
    }
}

fn main() {
    println!("Hello, world!");
}

操場

這無法編譯:

error[E0119]: conflicting implementations of trait `std::cmp::PartialEq<&_>` for type `&_`:
  --> src/main.rs:23:1
   |
23 | impl<T: Event> PartialEq for T {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<A, B> std::cmp::PartialEq<&B> for &A
             where A: std::cmp::PartialEq<B>, A: ?Sized, B: ?Sized;
   = note: downstream crates may implement trait `Event` for type `&_`

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
  --> src/main.rs:23:1
   |
23 | impl<T: Event> PartialEq for T {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type parameter `T` must be used as the type parameter for some local type
   |
   = note: only traits defined in the current crate can be implemented for a type parameter

這里有什么問題? 或者是否有另一種方法可以通用地實現這個PartialEq而不必為NoteOn輸入一次,為Noteff輸入一次?

謝謝你

這是我最好的“嘗試”,以一種可能的方式做我想做的事,但以不同的方式遵循@trentcl 和@Shepmaster 的評論。

我使用@trentctl 提到的 Enum 能夠在不同類型之間切換,我將枚舉值保留在一個公共結構事件中,以便我可以輕松地包裝不同的對象並向事件添加更多代碼。 這樣做也有助於制作一個簡單的 Vec 類型。

在這里,我想我只需要枚舉,而不需要事件和枚舉的屬性,我仍在學習變體枚舉的用法

我也遇到了@trentcl 提到的關於我不擁有 Vec 類型這一事實的問題,所以我將 Vec 包裝在一個 struct Sec 中,而不是簡單地將類型別名化。 這使得我的 PartialEq 實現與 Vec 類型分離(如果我理解,但我不確定)=> 我在這里感到困擾的原因是我認為使用type A = B; 確實創建了一個新類型,但文檔確實說明了它的別名(這可能會導致為什么我無法為 Vec 實現 PartialEq)盡管最后我想我在這方面也可能是非常錯誤的,因為我創建了一個僅包裝 1 個屬性的人工結構似乎也適得其反

=> 所以現在總結一下謝謝大家,我會繼續努力,看看它如何更有效,但我想我只是在 Rust 的上下文中使用了錯誤的東西,我不確定這是一個好的回答並希望得到反饋或其他建議

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d67b7d993fa6b6285962ee58e9b215e5

type Data = Vec<u8>;

#[derive(Debug)]
enum EventImpl{
    NoteOn(u8),
    NoteOff(u8)
}
#[derive(Debug)]
struct Event{
    data:Data,
    i:EventImpl
}

impl Event{
    fn new(data:Data)->Self{
        Event{
            i: match data[0]{
                0 => EventImpl::NoteOn(data[1]),
                1 => EventImpl::NoteOff(data[1]),
                _ => panic!("unk")
            },
            data:data
        }
    }

    fn data(&self)->&Data{
        &self.data
    }
}
#[derive(Debug)]
struct Seq{
    pub things:Vec<Event>
}

impl PartialEq for Seq{
    fn eq(&self,o:&Self)->bool{
    // i have to learn the iterator implementation    
        let mut r=o.things.len()==self.things.len();
        if ! r{
            return false;
        }
        for i in 0..o.things.len() {
            r = r && o.things[i]==self.things[i];
        }
        r
    }
}
impl PartialEq for Event{
    fn eq(&self,o:&Self)->bool{
    // i have to learn the iterator implementation    
        std::mem::discriminant(&self.i) == std::mem::discriminant(&o.i) && o.data()==self.data()
    }
}


fn main() {
    let mut s:Seq=Seq{things:vec![Event::new(vec![1,2,3])]};
    s.things.push(Event::new(vec![0,1,2]));
    let s2:Seq=Seq{things:vec![Event::new(vec![1,2,3]),Event::new(vec![0,1,2])]};

    println!("Hello, world! {:?} {:?}",s, s.things[1].data());
    println!("S1 == S2 ? {}",s==s2);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM