Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice::Windows::as_slice #455

Open
leb-kuchen opened this issue Oct 3, 2024 · 0 comments
Open

slice::Windows::as_slice #455

leb-kuchen opened this issue Oct 3, 2024 · 0 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@leb-kuchen
Copy link

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 leb-kuchen added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

1 participant