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

mut reborrow? #83

Open
Rudxain opened this issue Apr 22, 2024 · 1 comment
Open

mut reborrow? #83

Rudxain opened this issue Apr 22, 2024 · 1 comment

Comments

@Rudxain
Copy link

Rudxain commented Apr 22, 2024

Does the following qualify?

pub fn xor_hasher<'a, T>(inp: &'a [T], sbox: &mut [T])
where
    T: core::ops::BitXorAssign<&'a T>,
{
    let len = sbox.len();
    if len == 0 {
        return;
    };
    for chunk in inp.chunks(len) {
        chunk.iter().zip(sbox).for_each(|(i, s)| *s ^= i);
    }
}

Doesn't compile because sbox moves into zip, so &mut *sbox must be used instead. I find this silly, but "silly" isn't enough to be considered "obscure"

@Rudxain
Copy link
Author

Rudxain commented Apr 25, 2024

I've been experimenting with this. The results are weirder...

// error
fn xor0<'a, T: 'a, I: IntoIterator<Item = &'a T>>(inp: I, sbox: &mut [T])
where
    T: core::ops::BitXorAssign<&'a T>,
{
    for _ in 0..3 {
        inp.into_iter().zip(sbox).for_each(|(i, s)| *s ^= i);
    }
}

// error
fn xor1<I: Iterator<Item = u8> + Copy>(inp: I, sbox: &mut [u8]) {
    for _ in 0..3 {
        inp.zip(sbox).for_each(|(i, s)| *s ^= i);
    }
}

fn drop_ref(sbox: &mut [u8]) {}
// works???
fn drop_multi(sbox: &mut [u8]) {
    for _ in 0..3 {
        // if sbox isn't `Copy`, then why does this compile?
        // it should move into `drop_ref`, requiring a fresh reborrow
        drop_ref(sbox); // replace by `drop` to trigger error
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant