← [ ABORT TO HUD ]
SEQ. 1
SEQ. 2

Generic Associated Types (GATs) for Streaming Iterators

🧬 Advanced Traits, GATs & Metaprogramming16 min130 BASE XP

The Streaming Iterator Problem

Standard Rust iterators define Item as a fixed associated type: type Item;. This means next(&mut self) -> Option<Self::Item> cannot yield an item that borrows from &mut self, preventing zero-allocation streaming iterators where each step reuses an internal buffer.

Unlocking Zero-Allocation Streams with GATs

Generic Associated Types (stabilized in Rust 1.65) allow associated types to declare generic lifetime parameters:

pub trait StreamingIterator {
    // The Item type is parameterized over the lifetime of the borrow of self!
    type Item<'a> where Self: 'a;

    fn next(&mut self) -> Option>;
}

pub struct ZeroCopyCsvReader {
    buffer: Vec,
    cursor: usize,
}

impl StreamingIterator for ZeroCopyCsvReader {
    type Item<'a> = &'a [u8] where Self: 'a;

    fn next(&mut self) -> Option<&[u8]> {
        if self.cursor >= self.buffer.len() {
            return None;
        }
        let start = self.cursor;
        while self.cursor < self.buffer.len() && self.buffer[self.cursor] != b'
' {
            self.cursor += 1;
        }
        let line = &self.buffer[start..self.cursor];
        self.cursor += 1; // skip newline
        Some(line) // Yields slice borrowed from self.buffer without any string allocations!
    }
}
SYNAPSE VERIFICATION
QUERY 1 // 1
What fundamental capability do Generic Associated Types (GATs) provide over classic associated types?
They allow associated types to accept generic type or lifetime parameters that borrow from self
They allow traits to be called across network boundaries automatically
They bypass the Rust borrow checker during unsafe pointer casts
They convert runtime virtual dispatch into compile-time macros