These are chat archives for rust-lang/rust

1st
Jan 2018
David McGillicuddy
@djmcgill
Jan 01 2018 11:00

I have an enum and need to serialise and deserialise it as u8s. So far I have

pub enum SubmissionStatus {
    NotSubmitted = 0,
    PartiallySubmitted = 1,
    Submitted = 2,
}
impl SubmissionStatus {
    fromU8(x: u8) -> Option<Self> {
        match x {
            0 => Some(SubmissionStatus::NotSubmitted),
            1 => Some(SubmissionStatus::PartiallySubmitted),
            2 => Some(SubmissionStatus::Submitted),
            _ => None,
        }
    }
}

Is there a better way? Seems weird to specify the discriminant in the enum and then have to write out all out again anyway in fromU8.

David Harvey-Macaulay
@alteous
Jan 01 2018 11:01
@djmcgill Perhaps you could use #[repr(u8)]?
David McGillicuddy
@djmcgill
Jan 01 2018 11:01
Note that in general the discriminants aren't nicely in order and adjacent like that
David Harvey-Macaulay
@alteous
Jan 01 2018 11:02
You could just write a macro that generates the boilerplate for you.
David McGillicuddy
@djmcgill
Jan 01 2018 11:05
I think I'll try bincode. It's not exactly an uncommon requirement
Denis Lisov
@tanriol
Jan 01 2018 11:39
@djmcgill There are custom derives like enum-primitive-derive
Restioson
@Restioson
Jan 01 2018 16:40
so i want to comment on the private trait items rfc
but do i comment on the tracking issue (rust-lang/rfcs#275) or the RFC PR (https://github.com/rust-lang/rfcs/pull/52)
Because the RFC PR is closed and marked as postponed
But now we are post-1.0 IMO we should look at impl'ing it
Restioson
@Restioson
Jan 01 2018 17:17
I'm adding a usecase
David Harvey-Macaulay
@alteous
Jan 01 2018 18:59
Is there an iterator adapter that can intersperse every n elements?
let a = [1, 2, 3, 4, 5, 6, 7];
let b = [8, 9];
a.intersperse_every_n(b, 3) => [1, 2, 3, 8, 4, 5, 6, 9, 7]
I have a bizarre scenario that requires this.
Restioson
@Restioson
Jan 01 2018 19:12
hmmm
i feel like you could do it with skip or something
Jeremy Lempereur
@o0Ignition0o
Jan 01 2018 19:14
David Harvey-Macaulay
@alteous
Jan 01 2018 19:14
I might get away with an equally bizarre for loop.
Restioson
@Restioson
Jan 01 2018 19:14
or a fold
now i must do this
Jeremy Lempereur
@o0Ignition0o
Jan 01 2018 19:16
yeah that ought to do
David Harvey-Macaulay
@alteous
Jan 01 2018 19:18
Said for loop worked but it's pretty nasty.
for chunk in raw.chunks(nr_gltf_targets) {
    for (i, value) in chunk.iter().enumerate() {
        for _ in 0 .. nr_three_targets_per_gltf_target[i] {
            values.push(*value)
        }
    }
    for _ in nr_three_targets .. MAX_TARGETS {
        values.push(0.0);
    }
}
Restioson
@Restioson
Jan 01 2018 19:18
a.fold(a, |iter, &i| 
    if acc % n == 0 { 
        iter.extend(i.next()).extend(a.next())
    } else { 
        iter.extend(a.next())
    }
)
David Harvey-Macaulay
@alteous
Jan 01 2018 19:19
The acc % n is a performance issue.
Restioson
@Restioson
Jan 01 2018 19:19
But it is a fold
Folds give me purpose in life
Jeremy Lempereur
@o0Ignition0o
Jan 01 2018 19:19
:joy:
David Harvey-Macaulay
@alteous
Jan 01 2018 19:19
:laughing:
Restioson
@Restioson
Jan 01 2018 19:19
Do not question the folding...
ah now i need to write a nice version
Restioson
@Restioson
Jan 01 2018 19:26
Lets code golf this
Restioson
@Restioson
Jan 01 2018 19:32
How does one get an empty iterator?
red75prime
@red75prime
Jan 01 2018 19:32
None.into_iter()
Restioson
@Restioson
Jan 01 2018 19:33
ah thanks
red75prime
@red75prime
Jan 01 2018 19:33
std::iter::empty() too
David Harvey-Macaulay
@alteous
Jan 01 2018 19:35
@matklad I can't even...
red75prime
@red75prime
Jan 01 2018 19:36
Restioson
@Restioson
Jan 01 2018 19:36
Ah-ha, another fold!
Landing soon...
Well, like 50% sure 100% solution: not sure that adding None is correct always
Restioson
@Restioson
Jan 01 2018 19:39
How to turbofish again?
@matklad why a scan and not a fold?
FOLD
red75prime
@red75prime
Jan 01 2018 19:40
@matklad into_iter() in chain() is superfluous.
Aleksey Kladov
@matklad
Jan 01 2018 19:41
@red75prime indeed! However, I've spent like 10 minutes figuring out why .chain(None) didn't give me the expected results =/
@Restioson nah, I am not clever enough to understand non-monoid folds :)
Restioson
@Restioson
Jan 01 2018 19:41
mine shouldve been chain
idk, fold is just scan but no mutation in the closure, right?
Ah
i also have to resort to the scan i think
red75prime
@red75prime
Jan 01 2018 19:47
@matklad wrong result if b = [8, 9, 10], last chunk is not full, but 10 is added anyway
Restioson
@Restioson
Jan 01 2018 19:56
Agh, borrowchk errors
Rip
red75prime
@red75prime
Jan 01 2018 20:51
@alteous my solution had an error too. The least error prone method is probably to implement your own iterator. https://play.rust-lang.org/?gist=95e6245c0e7033f67fb9d2cd7d67660e&version=stable
red75prime
@red75prime
Jan 01 2018 21:18
It seems I've got a mild case of obsession. Final version: https://play.rust-lang.org/?gist=15a4c3d59585ca72544d67cff62920a4&version=stable
David Harvey-Macaulay
@alteous
Jan 01 2018 21:21
@red75prime Thank you!