oxidecomputer / idolatry

An experimental IPC interface definition language for Hubris.
Mozilla Public License 2.0
17 stars 11 forks source link

Add BufReader/Writer and supporting traits #31

Closed mkeeter closed 1 year ago

mkeeter commented 1 year ago

This is a pre-requisite for letting Hubris code be generic across slices and leases.

I added a new BufReader trait, which is implemented for both the existing LeaseBufReader and a new SliceBufReader; the same goes for BufWriter / SliceBufWriter.

This allows us to write code of the form

    pub fn read_one<'b, BufWrite: BufWriter<'b>, Write: Into<BufWrite> + 'b>(
        &self,
        dest: Write,
    ) -> Result<(), ()> {
        let dest: BufWrite = dest.into();
        dest.write(1)
    }
mkeeter commented 1 year ago

Update: I removed SliceBufReader/Writer in favor of implementing BufReader/Writer directly on slices (after some help from John and Rain in fighting variance).

cbiffle commented 1 year ago

FWIW, LeaseBufWriter only has a one-byte read operation simply because that's what I needed at the time -- adding one that copies out N bytes into a slice or equivalent, or an iterator as @andrewjstone suggested, would be more efficient if you need that sort of thing.

mkeeter commented 1 year ago

Thanks! I think I'll leave the one-byte-at-a-time functions for now, since that's what I need, but this should be easy to revisit later.