Skip to content

Methods to read into a buffer should take an uninitialized buffer #288

@lachlansneff

Description

@lachlansneff

Methods like blocking::i2c::Read::read should take &mut [MaybeUninit<u8>] instead of &mut [u8] to allow for uninitialized buffers to be used.

For example:

pub trait Read<A: AddressMode = SevenBitAddress> {
    type Error;
    fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error>;
}

should become

pub trait Read<A: AddressMode = SevenBitAddress> {
    type Error;
    fn read<'a>(&mut self, address: A, buffer: &'a mut [MaybeUninit<u8>]) -> Result<&'a [u8], Self::Error>;
}

Since slices of maybe uninit can only be converted to slices of normal types through unsafe, the methods should return the initiated slice.

Issues raised in #286 apply, namely that the returned slice may refer to a subslice of the input buffer. If that's a safety issue, the traits should be made unsafe to enforce that invariant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions