These are chat archives for rust-lang/rust

29th
Nov 2016
Nicolas Bigaouette
@nbigaouette
Nov 29 2016 03:44
Anybody tried to serde_urlencoded::to_string() a struct containing a Vec<i64>? I get an Err(Custom("unsupported value")) at runtime...
Igor
@target-san
Nov 29 2016 09:16
@cmsd2 Procedural macros - if you need metaprogramming. Generating code dynamically requires special facilities - like for any other native language.
Please note that for Java huge part of codegen is by-design delayed till runtime.
David McGillicuddy
@djmcgill
Nov 29 2016 12:58
I'm trying to use http://contain-rs.github.io/enum-set/enum_set/trait.CLike.html with an enum (Foo) from an existing library. However I don't see a way to make Foo an impl of CLike due to the orphan impl rules - neither Foo nor CLike is local to my crate. What options do I have to use enumSet? Write a wrapper around Foo or CLike myself, or submit a pull request to Foo's crate to impl CLike?
If I'm converting the things to and from u32 myself I might as well just use BitSet.
Jonas Platte
@jplatte
Nov 29 2016 13:06
@djmcgill The standard solution to this is a "newtype" (single-element tuple struct), e.g.
struct Bar(Foo);
Depending on how you want to use Foo, you probably also want
impl Deref for Bar {
    type Target = Foo;

    fn deref(&self) -> &Foo {
        &self.0
    }
}
And / or other Traits from std::ops.
A quick google search got me https://github.com/arienmalec/newtype_macros, which seems nicer if you have a bunch of arithmetic operations that Foo works with, but if it's just Deref (calling methods of Foo on a Bar without explicit .0 access) I'd personally not use the macro.
Michael Thomas
@Michaelt293
Nov 29 2016 13:56
In Haskell, you can derive Enum and then use range syntax (i.e., [A .. B]) for a sum type. Can something similar be done in Rust for an Enum?
Jonas Platte
@jplatte
Nov 29 2016 14:02
@Michaelt293 Quick search only got me rust-lang/rust#30877
Haven't read it all, but it might be the way to use ranges with custom types. However, in any case it's unstable.
Michael Thomas
@Michaelt293
Nov 29 2016 14:05
Okay, thanks or the info.
Pavel Meledin
@btbvoy
Nov 29 2016 14:21
if one would be doing a web store on rust (just for learning example) would you pick tokio for web server part ?
Jonas Platte
@jplatte
Nov 29 2016 14:28
@btbvoy It depends on what you want to learn. Tokio would be a very low level of abstraction. If you actually want to write some HTML and CSS, you should not start there.
Pavel Meledin
@btbvoy
Nov 29 2016 14:29
@jplatte what would you recommend ? Hyper ?
Jonas Platte
@jplatte
Nov 29 2016 14:29
The highest-level and least badly documented framework I know is iron.
You probably want to read https://wiki.alopex.li/ActuallyUsingIron unless you're familiar with express.js or similar frameworks
Pavel Meledin
@btbvoy
Nov 29 2016 14:31
I’m familiar but don’t really like middleware based solutions since they are pretty quickly became a mess :-D
but anyway thanks
Jonas Platte
@jplatte
Nov 29 2016 14:32
And then you should probably use a template language so you actually write HTML with dynamic parts instead of Rust with HTML snippets all over the place. I'd personally go for https://github.com/sunng87/handlebars-iron because you don't need additional glue code and I hate DSLs that build HTML for me (why learn another syntax for the same thing when you can just embed dynamic contents in the existing language)
But there is https://github.com/Polly-lang/Polly as well, if you like that kind of thing to.
Not sure if it has glue for iron or another web framework
If you don't like middleware-based frameworks, you are kind of screwed at the moment. IIRC @tomaka is building his own simpler web server, but I'd assume it's quite a bit farther from production than iron.
Hm, the docs don't look that bad actually
Pavel Meledin
@btbvoy
Nov 29 2016 14:47
aha, I see. Thanks :-)
and tokio is for building services and stuff like this, right ?
Jonas Platte
@jplatte
Nov 29 2016 14:53
tokio is a collection of crates for async io. The highest level of that is the Service trait, which is an abstraction for things that you can send a request to that will then asynchronously send a response or an error. That's my understanding at least.
tokio itself is protocol agnostic and could be used for anything from http to plain tcp packets or dbus messages AFAIK.
Pavel Meledin
@btbvoy
Nov 29 2016 14:55
gotcha :-) thanks
David McGillicuddy
@djmcgill
Nov 29 2016 14:57
What functions/traits are defined for enums? I see some example code here with as u32 here for example.
Or to ask my actual question: I have
use glutin::{MouseButton, VirtualKeyCode};

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum KeyDown {
    Key(VirtualKeyCode),
    Mouse(MouseButton)
}
Where VirtualKeyCode is ONLY tags and none of the variants have any members, and MouseButton is all tags except for the final member which contains a u8.

And I'd like to implement

pub trait CLike {
    fn to_u32(&self) -> u32;
    unsafe fn from_u32(u32) -> Self;
}

for it.

Jonas Platte
@jplatte
Nov 29 2016 14:59
I think as {integer} just returns the number of the variant.
David McGillicuddy
@djmcgill
Nov 29 2016 15:00
Seems that a straightforward bijection exists there to me (as long as the total number of items in VirtualKeyCode, MouseButton, and u8 don't overflow u32).
Okay, so the as casts are the only helper methods on enums? Is there no .all() or .enumerate() or something?
Peter Atashian
@retep998
Nov 29 2016 15:02
@djmcgill Enums come with no inherent methods by default. All you have is any methods you impl yourself, any traits impl'd for them, and as casts
Jonas Platte
@jplatte
Nov 29 2016 15:03
Oh, hey there is an RFC about it. Don't know why I didn't find it on the first search..
rust-lang/rfcs#284
@Michaelt293 ^ Found what you were searching for :D (if you were searching for it for yourself and not just trying to help..)
David McGillicuddy
@djmcgill
Nov 29 2016 15:03
okay, VirtualKeyCode only has 147 members. A bit of copy-pasting, shouldn't be too bad.
I'm going to implement it using a match, so that I get a compiler warning should an extra case being added I think.
Jonas Platte
@jplatte
Nov 29 2016 15:04
Yeah, as long as you don't use a wildcard pattern that will work
David McGillicuddy
@djmcgill
Nov 29 2016 15:05
I think I'll take the opportunity to write an apology for future me in a comment.
Jonas Platte
@jplatte
Nov 29 2016 15:05
although you don't need to do this manually. As linked on the RFC: https://docs.rs/enum_derive/*/enum_derive/index.html
David McGillicuddy
@djmcgill
Nov 29 2016 15:05
oh great, I missed that
I'm not defining the enum myself though, but using one from a crate.
Jonas Platte
@jplatte
Nov 29 2016 15:07
hm
I'd ask the maintainer if they think it's a good idea to have this by default, and PR if yes.
David McGillicuddy
@djmcgill
Nov 29 2016 15:07
So I could add a pull request for CLike to the enum
yeah, exactly
I think for now I will do it manually, while discussing it on the appropriate gitter. Thanks for your help!
David McGillicuddy
@djmcgill
Nov 29 2016 15:15
    const VIRTUAL_KEY_CODE_MAX: u32 = 146;
    fn to_u32(&self) -> u32 {
        match *self {
            KeyDown::Key(key) => key as u32,
            KeyDown::Mouse(MouseButton::Left) => VIRTUAL_KEY_CODE_MAX + 1,
            KeyDown::Mouse(MouseButton::Right) => VIRTUAL_KEY_CODE_MAX + 2,
            KeyDown::Mouse(MouseButton::Middle) => VIRTUAL_KEY_CODE_MAX + 3,
            KeyDown::Mouse(MouseButton::Other(code)) =>
                VIRTUAL_KEY_CODE_MAX + 4 + code as u32,
        }
    }
Quick, dirty, and prone to breaking.
Jonas Platte
@jplatte
Nov 29 2016 15:16
:D
Care to explain why you want to implement CLike for KeyDown?
David McGillicuddy
@djmcgill
Nov 29 2016 15:18
I want to put it in an EnumSet
Jonas Platte
@jplatte
Nov 29 2016 15:19
okay.... why?
David McGillicuddy
@djmcgill
Nov 29 2016 15:20
I can't see a method in glutin for which keys/mouse buttons are currently held down. So I'm making my own:
pub struct App {
    bundle: Bundle<R, pipe::Data<R>>,
    mapping: gfx::mapping::RWable<R, Instance>,
    keys_down: EnumSet<KeyDown>,
    last_instant: time::Instant,
    ...
}
every frame, the set keys_down is updated for any KeyboardInput or MouseInput events.
Jonas Platte
@jplatte
Nov 29 2016 15:22
okay... that still doesn't explain why you'd use an EnumSet for that though
David McGillicuddy
@djmcgill
Nov 29 2016 15:23
Because it's a static set of values that fit into a u32, and EnumSet or BitSet seem like they'd be better suited to this use case than a HashSet.
Jonas Platte
@jplatte
Nov 29 2016 15:24
don't you think using something less efficient and trying to find out what the proper way of doing this is would be a better option? :D
David McGillicuddy
@djmcgill
Nov 29 2016 15:26
I suspect so, yes. Do note that is a hobby project. I suppose writing code that's inefficient is better than code that is (potentially) broken.
whoops - EDIT: Fixed link
David McGillicuddy
@djmcgill
Nov 29 2016 15:35
I hadn't, but it's pretty much the same thing. It's a fairly common pattern really. Not sure why they've implement a HashMap where the value can only ever be KeyState::Pressed but there you go.
Mine is a little more general anyway.
Jonas Platte
@jplatte
Nov 29 2016 15:37
alright
Emil Gardström
@Emilgardis
Nov 29 2016 23:33
I'm a little late but something similar can be found here: https://github.com/amethyst/amethyst/blob/develop/src/context/src/input.rs