libs-team icon indicating copy to clipboard operation
libs-team copied to clipboard

slice::Windows::as_slice

Open leb-kuchen opened this issue 1 year ago • 0 comments

Proposal

Problem statement

When writing an iterator which wraps slice::Windows, it is useful to access the underlying slice, so the iterator can provide methods to reconstruct the underlying data structure

Motivating examples or use cases

pub struct GraphemeStr<'a> {
    inner: &'a str,
    breakpoints: &'a [usize],
}

#[derive(Debug)]
pub struct GraphemeStrIterator<'a> {
    iter: slice::Windows<'a, usize>,
    str: &'a str,
}

impl<'a> GraphemeStrIterator<'a> {
    pub fn new(gstr: &'a GraphemeStr) -> Self {
        Self {
            iter: gstr.breakpoints.windows(2),
            str: gstr.inner,
        }
    }
}
impl<'a> Iterator for GraphemeStrIterator<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|i| &self.str[i[0]..i[1]])
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
    fn count(self) -> usize {
        self.len()
    }
    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.iter.nth(n).map(|i| &self.str[i[0]..i[1]])
    }
    fn last(self) -> Option<Self::Item> {
        self.iter.last().map(|i| &self.str[i[0]..i[1]])
    }
}
impl<'a> DoubleEndedIterator for GraphemeStrIterator<'a> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.iter.next_back().map(|i| &self.str[i[0]..i[1]])
    }
    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        self.iter.nth_back(n).map(|i| &self.str[i[0]..i[1]])
    }
}
impl<'a> ExactSizeIterator for GraphemeStrIterator<'a> {}

impl<'a> GraphemeStrIterator<'a> {
    fn as_grapheme_str(&self) -> GraphemeStr<'a> {
        todo!()
    }
}

Solution sketch

Add the method slice::Windows::as_slice.

Alternatives

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

leb-kuchen avatar Oct 03 '24 13:10 leb-kuchen