These are chat archives for rust-lang/rust

17th
Sep 2017
Moggers
@Moggers
Sep 17 2017 12:14
Hi!
Is there really no ? operator for Option?
coz unwrapping Option for std::iter::Iterator::read is reaaaallyy obnoxious
David Harvey-Macaulay
@alteous
Sep 17 2017 12:16
@Moggers No but it's a planned feature in the try trait RFC.
Moggers
@Moggers
Sep 17 2017 12:16
aaaaaa
hm
is it implemented but locked behind a feature flag or? (please :>)
waittt a sec I can try using the ? operator on other things and they get the Try not implemented error
couldn't I just implement it myself?
or does the trait have to be implemented in the crate where Option is defined?
David Harvey-Macaulay
@alteous
Sep 17 2017 12:19
I don't think so, because Option is in another crate, namely std. rust-lang/rust#42526 is the relevant PR, I think.
Moggers
@Moggers
Sep 17 2017 12:19
yep! thought that was the case
Moggers
@Moggers
Sep 17 2017 12:24
well, whats the idiomatic way to handle a bunch of nexts?
I cant really use a for loop, I dont think so anyway
David Harvey-Macaulay
@alteous
Sep 17 2017 12:25
@Moggers What are you trying to achieve?
Moggers
@Moggers
Sep 17 2017 12:25
fn from_slice(src: &[u8]) -> Result<Box<Self>, Box<Error>> { let mut iter = src.iter(); Ok(Box::new(ModEntry( (*iter.next().unwrap_or(&0u8) as i16) + ((*iter.next().unwrap_or(&0u8) as i16) << 8), (*iter.next().unwrap_or(&0u8) as i16) + ((*iter.next().unwrap_or(&0u8) as i16) << 8), iter.fold_while("".to_string(), |mut acc, x| if *x == 0 { Done(acc) } else { acc.push(*x as char); Continue(acc) }).into_inner(), (*iter.next().unwrap_or(&0u8) as u32) + ((*iter.next().unwrap_or(&0u8) as u32) << 8) + ((*iter.next().unwrap_or(&0u8) as u32) << 16) + ((*iter.next().unwrap_or(&0u8) as u32) << 24), ))) }
thats the mess I have right now
gitter cut off the first line :/
David Harvey-Macaulay
@alteous
Sep 17 2017 12:26
wat
Moggers
@Moggers
Sep 17 2017 12:26
yeahhhh
I have a collection of u8 bytes from a socket stream
and I'm trying to read u16,u16,string,u32 from it
and Im not sure if this is normal but the u16s and u32s u8s are little endian, but the u8s are in big endian order
but I'd really like it if I could just use iter.next()? instead of iter.next().unwrap_or(&0u8)
oh right the first two are i16s not u16s
David Harvey-Macaulay
@alteous
Sep 17 2017 12:31
You could create a macro for it.
macro_rules! next {
    ($iter:expr, $ty:ty) => {
        *$iter.next().unwrap_or(&0u8) as $ty
    };
}
Moggers
@Moggers
Sep 17 2017 12:31
hmmm
I nuked my code block somehow
whats $ty in that case?
David Harvey-Macaulay
@alteous
Sep 17 2017 12:32
To avoid the derefs you can use src.iter().cloned().
The i16 etc. in *iter.next().unwrap_or(&0u8) as i16.
Moggers
@Moggers
Sep 17 2017 12:33
Wouldnt clone be allocating a new u8 every time I read?
hmm
David Harvey-Macaulay
@alteous
Sep 17 2017 12:33
You're copying the data anyway.
Moggers
@Moggers
Sep 17 2017 12:33
Yeah thats a good idea
I'm copying out the data with *?
hm you know what I think I'm just gonna write something that just reads a u8 stream and constructs types from it
rather than doing this by hand
David Harvey-Macaulay
@alteous
Sep 17 2017 12:51

I'm copying out the data with *?

Yes.

Moggers
@Moggers
Sep 17 2017 12:51
is it different to just dereferencing a pointer in say, C?
David Harvey-Macaulay
@alteous
Sep 17 2017 12:51
Have you tried bincode?
Moggers
@Moggers
Sep 17 2017 12:52
I had a look at it, but I wanted to make sure I had control over the encoding/decoding format
David Harvey-Macaulay
@alteous
Sep 17 2017 12:52
Deref only copies the data when the data is Copy.
Moggers
@Moggers
Sep 17 2017 12:52
since I need to conform to another program's behaviour
David Harvey-Macaulay
@alteous
Sep 17 2017 12:53
In C, the data is copied regardless of whether it is actually sensible to do so.
Moggers
@Moggers
Sep 17 2017 15:02
Okay I got it
Its really good too
David Harvey-Macaulay
@alteous
Sep 17 2017 15:50
:clap:
Ariel Ben-Yehuda
@arielb1
Sep 17 2017 16:39
join #rust-lang/rust
yay gitter/irc works
Zakarum
@omni-viral
Sep 17 2017 19:39
Why can't I derive serde::Deserialize for Message here?
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
enum Payload<'a> {
    Data(&'a [u8]),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Message<'a> {
    id: Id,
    payload: Payload<'a>,
}
rustc says "cannot infer an appropriate lifetime for lifetime parameter 'de due to conflicting requirements"
It's derived for Payload
Denis Lisov
@tanriol
Sep 17 2017 19:49
Are you missing #[serde(borrow)]?
Zakarum
@omni-viral
Sep 17 2017 19:49
Ah. Thanks!