2
votes

I'm baffled by this line of rust:

impl<T: Read + Seek> KaitaiStream for T {}

It looks like it is trying to implement KaitaiStream for some struct "T".

But T is unknown, so what can this line mean? Does it mean all things that implement Read + Seek will now also implement KaitaiStream?

1
The short answer is yes. - Brady Dean
🤯 that's mind blowing. - Matt Crinklaw-Vogt

1 Answers

3
votes

The syntax broken down reads as: implement KaitaiStream for all types T that implement both Read and Seek.

Notice that the trait itself is defined like this:

pub trait KaitaiStream: Read + Seek {
   // methods...
}

which means: all implementations of KaitaiStream must also implement Read and Seek.

These things together effectively make KaitaiStream a synonym for Read + Seek. Any type that implements KaitaiStream must implement Read + Seek and any type that implements Read + Seek gets a KaitaiStream implementation "for free".

Because of the way that it is defined, methods of KaitaiStream may only[1] use methods of Read and Seek, so it makes sense that it provides all default implementations of those.


[1] A future version of Rust, will include trait specialization, which will allow you to additionally implement more efficient (or just different) implementations for specific types. Without specialization, those implementations would conflict with the defaults and are not allowed.