Re: [PATCH 1/2] rust: harden index manipulation using ownership
From: Trevor Gross
Date: Sun Sep 29 2024 - 19:16:36 EST
On Fri, Sep 13, 2024 at 5:01 PM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> +//! Utilities for working with ranges of indices.
> +
> +/// A range of indices.
> +///
> +/// This utility is useful for ensuring that no index in the range is used more than once.
> +#[derive(Debug)]
> +pub struct Range {
> + offset: usize,
> + length: usize,
> +}
Would a name like "DataRange" or "CheckedRange" be better here, to
avoid confusion with core::ops::Range?
> + /// Use this range of indices.
> + ///
> + /// This destroys the `Range` object, so these indices cannot be used again after this call.
> + pub fn use_range(self) -> UsedRange {
Maybe just `.use()`?
> + /// Assert that this range is aligned properly.
> + pub fn assert_aligned(&self, alignment: usize) -> Result<(), RangeError> {
It would probably be good to warn that this alignment is relative to
the offset, i.e. if you split a range at an unaligned point then this
may not be useful.
This is a pretty cool API idea.
- Trevor