These are chat archives for rust-lang/rust

10th
Sep 2018
Kelly Thomas Kline
@kellytk
Sep 10 2018 04:07
Where can I read about using attributes on if expressions?
Kelly Thomas Kline
@kellytk
Sep 10 2018 04:15
Until that's supported, can match contain a single branch which covers all cases except the one specified? I'd like a block to run if a var isn't Myenum::Specificvariant
matrixbot
@matrixbot
Sep 10 2018 04:24
bspeice You should be able to use the _ case to handle "everything other than what I've already specified"
bspeice I think this may be what you're looking for: https://doc.rust-lang.org/book/second-edition/ch06-02-match.html#the-_-placeholder
Kelly Thomas Kline
@kellytk
Sep 10 2018 04:27
@matrixbot I'm familiar with that feature however that results in two branches being necessary. Something like !Myenum::Specificvariant => {...} would result in one
matrixbot
@matrixbot
Sep 10 2018 04:28
bspeice Then what you're talking about should just be an if x == Myenum::Specificvariant, no? You'd be able to use if let ... to destructure, but a match { _ => {} } doesn't make a whole lot of sense.
bspeice Noting that the enum would need #[derive(PartialEq)] to do the equality check, but ultimately I don't think you actually want to use match for the case you're talking about.
Kelly Thomas Kline
@kellytk
Sep 10 2018 04:50
@matrixbot I originally tried an if condition but it generated an error that attributes are not yet allowed on them. I need to use #[serde(with = ...)] for a part of the condition
Zakarum
@omni-viral
Sep 10 2018 06:06
@sam0x17 dyn Trait + 'static doesn't mean that object lives until the end of a program, but that it can live until the end of program. Any object will be dropped at it's scope end. So maybe you don't need lifetime parameter on Stream. If you want to make it like map then check std::Iterator::map implementation.
You should generally avoid dyn Trait
Kelly Thomas Kline
@kellytk
Sep 10 2018 06:31
Can Clone be implemented for a pub enum from a 3rd party crate?
Zakarum
@omni-viral
Sep 10 2018 07:19
@kellytk Yes. But only in that 3rd party crate :smile:
You can't implement foreign trait for foreign type
Obviously because if someone else depends on your crate and implements Clone for that type and you add Clone implementation then implementation will be ambiguous
What you can do is to wrap 3rd party type and implement Clone for wrapper
Or just define cloning function
This of course requires all fields of the type to be public and clonnable.
trsh
@trsh
Sep 10 2018 07:31
What does ir mean if fn call has )?; at end?
trsh
@trsh
Sep 10 2018 07:41
OK its an result
Kelly Thomas Kline
@kellytk
Sep 10 2018 07:49
@omni-viral What does it mean to "wrap 3rd party type"? What I was doing before I tried to use WebSocketStatus directly was, when encountering a WebSocketStatus value, reinterpret it using an enum of my own with corresponding values. That works fine however it would be more direct to use WebSocketStatus. Unfortunately it seems Rust is not quite there yet in that regard
Zakarum
@omni-viral
Sep 10 2018 12:21
Like this struct Foo(Bar)
Where Foo wraps Bar
Ash
@ashthespy
Sep 10 2018 12:47

Hello everyone again,

        let index: u32 = env::var("THE_INDEX").unwrap_or(String::from("0")).parse::<u32>().unwrap();

Is there a more efficient way of doing the above?

Denis Lisov
@tanriol
Sep 10 2018 12:58
By "efficient" do you mean more readable?
Ash
@ashthespy
Sep 10 2018 13:01
well, more 'rustish' ? ;-)
A match() would be more readable - but was just wondering if there was a nicer oneliner
but right now, I am parsing the "0" string, rather than using an unwrap_or() on the parse?
Denis Lisov
@tanriol
Sep 10 2018 13:07
Actually, your snippet's logic seems off... note that env::var returns an error if the variable is missing (so you want to use the default) or if the variable is not valid Unicode (you may prefer to panic in this case).
So I'd prefer either a normal match...
Denis Lisov
@tanriol
Sep 10 2018 13:13
...or using something like envy if there are multiple variables I need...
Ash
@ashthespy
Sep 10 2018 13:18
Ah, yes didn't think of an invalid unicode input.
let index: u32 = env::var("THE_INDEX").unwrap_or(String::from("0")).parse::<u32>().unwrap_or(0);
Will also look at envy. thanks!
Vitaly
@Virtuos86
Sep 10 2018 13:53
@ashthespy let index: u32 = env::var("THE_INDEX").unwrap_or(String::from("0")).parse().unwrap_or(0);
Ash
@ashthespy
Sep 10 2018 13:57
@Virtuos86 parse() decides the type automatically based on index: u32 ?
Vitaly
@Virtuos86
Sep 10 2018 13:59
@ashthespy yes. or even let index = env::var("THE_INDEX").unwrap_or(String::from("0")).parse().unwrap_or(0_u32);
Ash
@ashthespy
Sep 10 2018 14:00
Cool thanks~
Sam Johnson
@sam0x17
Sep 10 2018 14:54
how can I make a recursive &mut self method for a Struct and/or Trait?
I keep getting "error[E0502]: cannot borrow *self as mutable because self.data_callback is also borrowed as immutable"
I could unroll the recursion into a while loop but I'd rather not
Lyle Mantooth
@IslandUsurper
Sep 10 2018 15:23
@sam0x17, I doubt you can safely. But maybe you can take self instead and return the changed self?
Ichoran
@Ichoran
Sep 10 2018 15:32
@sam0x17 - Rust doesn't have tail call elimination AFAIK, so you should unroll it into a while loop if you can and if it might not be relatively shallow recursion.
Ichoran
@Ichoran
Sep 10 2018 15:37
Anyway, can you give an example of the simplest possible thing that shows your failing use case?
Recursive &mut self is not universally impossible, so it's presumably got something to do with what else you're trying to do.
Ash
@ashthespy
Sep 10 2018 16:08
Is it not possible to poll a Future from outside a future? I.e poll it from a thread?
I am trying to poll a futures::sync::mpsc::UnboundedReceiver from a separate thread. I can achieve this in a blocking fashion by doing futures::sync::mpsc::UnboundedReceiver::into_future().wait() but when I attempt to do it in a non blocking way futures::sync::mpsc::UnboundedReceiver::poll() I run into thread '<unnamed>' panicked at 'no Task is currently running'
Any pointers on some method to poll this future?
Zakarum
@omni-viral
Sep 10 2018 17:14
@ashthespy for the first question -
env::var("THE_INDEX").and_then(parse).unwrap_or(0_u32);
You can't poll future ouside task
Use executor
Ash
@ashthespy
Sep 10 2018 17:16
neat.
Kelly Thomas Kline
@kellytk
Sep 10 2018 20:09
@omni-viral Thanks for the suggestion of tuple structs to wrap the 3rd party enum. Unfortunately it appears that the reason why I couldn't use the enum directly holds for using it wrapped in a tuple struct. I've implemented the tuple struct and the code looks nice however it's giving the familiar errors about the enum not satisfying trait bounds. Unless I'm missing something I'll need to stay with translating WebSocketStatus to an enum of my own at initial encounters
Denis Lisov
@tanriol
Sep 10 2018 20:10
@kellytk Which crate is that?
Kelly Thomas Kline
@kellytk
Sep 10 2018 20:10
@tanriol Yew
Denis Lisov
@tanriol
Sep 10 2018 20:14
This one looks alive... have you considered opening a pull request adding the #[derive]s you need? I'd guess such a trivial change can be merged soon :-)
Zakarum
@omni-viral
Sep 10 2018 20:15
@kellytk Well. You can't implement traits via derive if they are missing for wrapped type. But you can implement them manually.
Kelly Thomas Kline
@kellytk
Sep 10 2018 20:15
The list of derives needed includes Clone, Copy, Eq, PartialEq, Serialize, Deserialize. Do you think that that's a reasonable request?
I'll try that @omni-viral
@omni-viral The first error I encounter with #[derive(Debug, Serialize, Deserialize)] pub struct Mystatus (WebSocketStatus); is Deserialize is not implemented by WebSocketStatus. How would I correct that?
Zakarum
@omni-viral
Sep 10 2018 20:21
Either impl Deserialize for MyStatus { ... } or use this feature
Denis Lisov
@tanriol
Sep 10 2018 20:21
All the builtin ones look ok, but not Serialize/Deserialize. However, serde has tricks for that :-)
Zakarum
@omni-viral
Sep 10 2018 20:22
This message was deleted
Kelly Thomas Kline
@kellytk
Sep 10 2018 20:24
I tried remote-derive and it mostly worked however because statement attributes aren't yet in Rust I couldn't use a simple if expression and had to use a match expression with two branches when one should suffice
Zakarum
@omni-viral
Sep 10 2018 20:25
you can use cfg!() macro instead of attribute for branching
if cfg!(feature = "bla") && foo.bar { ... }
Kelly Thomas Kline
@kellytk
Sep 10 2018 20:26
That's interesting
Zakarum
@omni-viral
Sep 10 2018 20:26
Unless code inside if-block uses the feature as well
Kelly Thomas Kline
@kellytk
Sep 10 2018 20:29
I appreciate your and @tanriol's time and ideas. For simplicity sake I'll continue with the hack of translating the enum when my code encounters it until Rust adds statement attributes
Zakarum
@omni-viral
Sep 10 2018 20:30
You can workaround statements attributes by stripping those statements into separate function an overload the function by attribute
Kelly Thomas Kline
@kellytk
Sep 10 2018 20:30
:-D
Sylwester Rąpała
@xoac
Sep 10 2018 22:01
How can I tell serde to parse integer as string without err? My error
Syntax("invalid type: integer `130`, expected f32")
Ichoran
@Ichoran
Sep 10 2018 22:04
Can you include a little more about the code that produces that error?
Sylwester Rąpała
@xoac
Sep 10 2018 22:07

of course

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct Threshold {
    pub up: Option<f32>,   // threshold 1
}

I use msgpack as deserializer and I want to specify value as int and be converted to float

Ichoran
@Ichoran
Sep 10 2018 22:10
Oh. Hm. I don't know how to do that. For JSON I'd use a with = compiler directive. But it's a bit iffy to take something stored in binary as an i32 and just silently transform it to f32.
Also, I'm not sure what you mean about "parse integer as string". It's not a string; that's the point of msgpack.
Sylwester Rąpała
@xoac
Sep 10 2018 22:13
my bad: "parse integer as string" -> should be "parse integer as float".
It's strange in serde_json it's not a problem. I want to use msgpack in way that is convertable to json.
Ichoran
@Ichoran
Sep 10 2018 22:14
That's because it's just text; it doesn't know what type it's supposed to be until you try to parse it.
But msgpack specifies the numeric type.
Sylwester Rąpała
@xoac
Sep 10 2018 22:15
Yep. But msgpack also say that you store type in most smallest possible way so for example i64 that is equal 5 will be stored as u8 or i8.
I thought I can convert easy between 130 and 130.0
Ichoran
@Ichoran
Sep 10 2018 22:17
Yeah. That would make sense, if it's actually in range. I don't know, sorry; I've never used msgpack serialization with serde.
Sylwester Rąpała
@xoac
Sep 10 2018 22:24
There is also defined https://doc.rust-lang.org/std/primitive.f32.html#impl-From%3Cu16%3E From<u16> for f32 so it's safe to load small numbers.
I am not sure my thoughts and will make issue on rmp_serde
Zakarum
@omni-viral
Sep 10 2018 22:30
@xoac you should use the same type that was serialized. If you follow some particular data scheme - follow it precisely. Define your field with correct integer type