Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

ices/71805.rs: fixed with errors #434

Merged
merged 1 commit into from
Jul 16, 2020
Merged

ices/71805.rs: fixed with errors #434

merged 1 commit into from
Jul 16, 2020

Conversation

github-actions[bot]
Copy link
Contributor

Issue: rust-lang/rust#71805

#![feature(const_generics)]
#![allow(incomplete_features)]

use std::mem::MaybeUninit;

trait CollectSlice<'a>: Iterator {
    fn inner_array<const N: usize>(&mut self) -> [Self::Item; N];

    fn collect_array<const N: usize>(&mut self) -> [Self::Item; N] {
        let result = self.inner_array();
        assert!(self.next().is_none());
        result
    }
}

impl<'a, I: ?Sized> CollectSlice<'a> for I where I: Iterator {
    fn inner_array<const N: usize>(&mut self) -> [Self::Item; N] {
        let mut result: [MaybeUninit<Self::Item>; N] = unsafe {
            MaybeUninit::uninit().assume_init()
        };

        let mut count = 0;
        for (dest, item) in result.iter_mut().zip(self) {
            *dest = MaybeUninit::new(item);
            count += 1;
        }

        assert_eq!(N, count);

        let temp_ptr: *const [MaybeUninit<Self::Item>; N] = &result;
        unsafe { std::ptr::read(temp_ptr as *const [Self::Item; N]) }
    }
}

fn main() {
    let foos = [0_u64; 9].iter().cloned();
    let _bar: [u64; 9] = foos.collect_array::<9_usize>();
}
=== stdout ===
=== stderr ===
error[E0596]: cannot borrow `foos` as mutable, as it is not declared as mutable
  --> /home/runner/work/glacier/glacier/ices/71805.rs:37:26
   |
36 |     let foos = [0_u64; 9].iter().cloned();
   |         ---- help: consider changing this to be mutable: `mut foos`
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                          ^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
==============

=== stdout ===
=== stderr ===
error[E0596]: cannot borrow `foos` as mutable, as it is not declared as mutable
  --> /home/runner/work/glacier/glacier/ices/71805.rs:37:26
   |
36 |     let foos = [0_u64; 9].iter().cloned();
   |         ---- help: consider changing this to be mutable: `mut foos`
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                          ^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
==============
@Alexendoo Alexendoo merged commit 2b07e0a into master Jul 16, 2020
@Alexendoo Alexendoo deleted the autofix/ices/71805.rs branch July 16, 2020 12:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants