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

Collect to slice #71387

Open
leonardo-m opened this issue Apr 21, 2020 · 3 comments
Open

Collect to slice #71387

leonardo-m opened this issue Apr 21, 2020 · 3 comments
Labels
A-collections Area: std::collections. A-iterators Area: Iterators C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

This is an enhancement suggestion. Beside the current collect() to collection like Vec, Hash, etc, and the future collect to array:

#69985

I find it useful to also collect to a slice when I don't know at compile-time the exact length of the resulting array, but I know an upper bound of its length.

I have adapted the following code from this crate:
https://github.com/kchmck/collect_slice
https://crates.io/crates/collect_slice

trait CollectSlice<'a>: Iterator {
    fn inner(&mut self, slice: &'a mut [Self::Item]) -> &'a [Self::Item];
    fn inner_mut(&mut self, slice: &'a mut [Self::Item]) -> &'a mut [Self::Item];

    fn collect_slice(&mut self, slice: &'a mut [Self::Item]) -> &'a [Self::Item] {
        let result = self.inner(slice);
        assert!(self.next().is_none());
        result
    }

    fn collect_slice_mut(&mut self, slice: &'a mut [Self::Item]) -> &'a mut [Self::Item] {
        let result = self.inner_mut(slice);
        assert!(self.next().is_none());
        result
    }
}

impl<I: ?Sized> CollectSlice<'a> for I where I: Iterator {
    fn inner(&mut self, slice: &'a mut [Self::Item]) -> &'a [Self::Item] {
        let count = slice.iter_mut().zip(self).fold(0, |count, (dest, item)| {
            *dest = item;
            count + 1
        });
        &slice[.. count]
    }

    fn inner_mut(&mut self, slice: &'a mut [Self::Item]) -> &'a mut [Self::Item] {
        let count = slice.iter_mut().zip(self).fold(0, |count, (dest, item)| {
            *dest = item;
            count + 1
        });
        &mut slice[.. count]
    }
}

This code should be improved and simplified. And as in Issue #69985 we could return a Result<> instead, and remove the asserts.

@jonas-schievink jonas-schievink added A-collections Area: std::collections. A-iterators Area: Iterators C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Apr 21, 2020
@Mark-Simulacrum
Copy link
Member

I think I'm a little uncertain about passing in &mut [T] since that seems prone to "but I don't want to initialize my Ts" -- we'd probably want something like &mut [MaybeUninit<T>] though that raises questions as well.

1 similar comment
@Mark-Simulacrum
Copy link
Member

I think I'm a little uncertain about passing in &mut [T] since that seems prone to "but I don't want to initialize my Ts" -- we'd probably want something like &mut [MaybeUninit<T>] though that raises questions as well.

@leonardo-m
Copy link
Author

leonardo-m commented Apr 21, 2020

I have found collect_slice useful, but I am still unsure if it's worth having in the std lib (while I am sure I want #69985, useful mostly for small arrays) because its usage is not very clean:

let primes = &mut [0; 1000];
let primes = (2_u32 .. 1000)
    .filter(|&b| is_prime(b))
    .collect_slice(primes);

That is equivalent to:

let primes = &mut [0; 1000];
let mut index = 0;
for b in 2_u32 .. 1000 {
    if is_prime(b) {
        primes[index] = b;
        index += 1;
    }
}
let primes = &[.. index];

In all my usage cases I don't use collect_slice in the middle of an iterators chain because of memory ownership issues, so I use it only at the end of iterators chains like in the above example, and only when the number of items is high enough and I don't know it at compile-time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: std::collections. A-iterators Area: Iterators C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants