These are chat archives for rust-lang/rust

21st
Sep 2018
Elliot Stern
@PipocaQuemada
Sep 21 2018 01:20 UTC
What's the best way to read a single char in from std in? There's some old stuff pointing at io::stdin().chars(), but it looks like chars is deprecated.
Iurii Malchenko
@yurique
Sep 21 2018 02:10 UTC

Hi everyone!

I’ve spent the last couple of hours trying to figure this out, but failed.

So I have this situation: I’m putting together some code to run on aws lambda, and I spin up a server (lambda::gateway::start) which accepts a closure from request to response. That’s working. But now a problem: I have a data-set in a file (it’s quite large, and will take time to load; so I don’t want to load it over and over again on each request), it’s going to be read-only, and I need to pass it to a function that gets called inside the closure.

Is there a right way to do that?

I tried this:

static mut global_read_only_data: Option<Vec<Template>> = None;

fn main() {
  // … 
    unsafe {
        global_read_only_data = Some(Vec::new());
    }

// …

    lambda::gateway::start(|req: Request<Body>| {
       find_something_in_data(unsafe { &all_templates.unwrap() });
       // getting “cannot move out of static item"

      // ...

    }
// ...
}

I have a feeling I’m doing it wrong, but I have no idea.

I will really appreciate any suggestions/hints/links-to-blogs :)

mtak-
@mtak-
Sep 21 2018 02:13 UTC
might need to change it to .as_ref().unwrap() because unwrap() takes self by value
Iurii Malchenko
@yurique
Sep 21 2018 02:14 UTC
@mtak- hey it worked! :) (at least it compiled)
Thanks!
mtak-
@mtak-
Sep 21 2018 02:15 UTC
nice. i hope that makes sense. it's a pattern i wind up using a lot. .as_ref() creates a new Option with a reference inside that unwrap() is able to move out of.
Iurii Malchenko
@yurique
Sep 21 2018 02:20 UTC

it’s like my second day with rust, converting hundreds of lines of scala code; my head has been spinning for hours now, figuring out all the goodness of ownership, borrowing, boxes and matching

I have a tree-like structure (immutable), but because it’s recursive, I have to have the boxes (maybe there’s a better way), and though I’m not modifying anything, just copying and map'ping, I now have dozens of box ref somehing and .clone() all over the place. And the global var thing was the tipping point for my brain to stop understanding a thing (and 5am on the clock).

Anyways, thanks a LOT, @mtak- , I’ll get back to this after I get some sleep.

mtak-
@mtak-
Sep 21 2018 02:23 UTC
haha @yurique that's a lot of work for being so new to rust! perhaps lazy_static is something you'd wanna look into for that global. in general trees require heap allocation so there has to be a Box/Arc/Rc somewhere in there.
have a good night
Farzeen
@happycoder97
Sep 21 2018 04:13 UTC

Is this a typo or some special syntax?

let header = Header {
        headers: CustomHeaders {
            kid: "0001",) // < ---- comma paranthesis
            typ: "JWT",)
        }
        ..Default::default()
    }

source: https://crates.io/crates/medallion

David Vo
@auscompgeek
Sep 21 2018 04:47 UTC
that looks like a typo or some other copy-edit error
Ash
@ashthespy
Sep 21 2018 09:53 UTC
How would one backtrace in rust?
I cheat with panic, but is there a better method?
Denis Lisov
@tanriol
Sep 21 2018 09:55 UTC
How about the backtrace crate?
Ash
@ashthespy
Sep 21 2018 09:59 UTC
Would like to avoid additional crates - but will give it a look!
std::rt::backtrace::write looks like something!
Ichoran
@Ichoran
Sep 21 2018 13:10 UTC
@yurique - Why, may I ask, are you converting Scala to Rust? Just an exercise? Otherwise it would be one of my last targets for "from" languages, given that the two both have a lot of the same advantages compared to, say, C or Haskell or Python.
Also, dealing with immutable data structures is rather awkward in Rust compared to Scala; you tend to do a lot of reallocation of data with those patterns, so having a GC take care of the mess is really helpful.
Ichoran
@Ichoran
Sep 21 2018 13:16 UTC
Also, if you're doing things immutably, you may want to look at the im-rc crate (and im, the slower but thread-safe version): https://crates.io/crates/im-rc
Michal 'vorner' Vaner
@vorner
Sep 21 2018 13:17 UTC
Well, there might be valid reasons to convert scala to Rust. Like performance/memory consumption. But probably not 1:1.
Ichoran
@Ichoran
Sep 21 2018 13:19 UTC
Yeah, I would generally not try to convert immutable-structure-heavy code from Scala to Rust if I was after improved performance. It might even get worse. Much of the beauty of Rust is in its taming of mutability so you don't need immutable data structures (as often) to prevent you from doing something wrong.
Iurii Malchenko
@yurique
Sep 21 2018 13:30 UTC

@Ichoran hey!

Yeah, I’m pretty happy with Scala for most of the things :).

I only converted one piece that we have that I want to deploy on lambda. And on lambda there are drawbacks with Scala/jvm:

  • like cold vm will be slow for a while, and lambda will likely get restarted every now and then;
  • the initial load time adds latency;
  • and the code is cpu-heavy;
  • also, though most of the data is immutble, there is a lot of boxing/unboxing happening - functions passing/returning data - and GC is pretty busy.

So it feels rather slow on lambda (and we want a low latency).

And I’m not sure how this venture with Rust ends up - but I guess I’ll know soon :) The benchmarks look promising.

(btw, to read the data from disk (binary+gzip) I used flate2 and nom - and I must say I’m pretty impressed with how rich the ecosystem is (and how powerful nom is, though it required a bit of time and a bit of brain-pain to actually figure it out :) ).

Ichoran
@Ichoran
Sep 21 2018 13:40 UTC
@yurique - Okay, that makes sense! It's worth considering alternate strategies if you are CPU-bound with immutable data structures.
Michal 'vorner' Vaner
@vorner
Sep 21 2018 13:43 UTC
Also, there are crates for immutable data structures in rust, for example im and rpds. They likely use Arcs under the hood, with some optimisations.
Iurii Malchenko
@yurique
Sep 21 2018 13:44 UTC
@vorner cool! thanks, I’ll definitely take a look there
Ichoran
@Ichoran
Sep 21 2018 13:46 UTC
im uses Arc heavily. Not sure about rpds.
Sam A. Horvath-Hunt
@SamHH
Sep 21 2018 23:06 UTC
quick question.. I have a trait method and I want it to be able to return either BTreeMap or HashMap (as the latter is preferable but the former is sometimes required). given that Rust doesn't support unions (I think! - in the TypeScript sense anyway of just being able to say BTreeMap | HashMap), how can I accomplish this? I tried to be clever and implement an empty Map trait on each of those collections but I'm running into issues in usage whereby Rust says it doesn't know the size at compile time
Thiez
@Thiez
Sep 21 2018 23:07 UTC
enum MapResult<T, U> { BTree(BTreeMap<T, U>), Hash(HashMap<T, U>) } ? :)
Iurii Malchenko
@yurique
Sep 21 2018 23:07 UTC

hey all, I’m back with a quick status update

so I managed to make everything work (btw, how cool is this - include_bytes!, I’d be in trouble without it on lambda)
the only thing I have missing - is utilizing two available cpu cores, right now it’s single-threaded)

but - rust implementation (after some optimizations) does 200 "units of work" in 500ms, one cpu
scala on jvm, with all the overheads, managed to do less than 10units in over a second! (plus the cold start is much slower, like 4-6 seconds vs a couple of hundred milliseconds for rust)

a couple of days well spent :)

(btw, @mtak-, I did use the lazy_static lib - works like a charm, thanks for the suggestion! )

Sam A. Horvath-Hunt
@SamHH
Sep 21 2018 23:07 UTC
I suspect that'll work, let's see! thanks @Thiez ! :)
Thiez
@Thiez
Sep 21 2018 23:09 UTC
I suppose it's better to name the generic parameters K and V, as that is what the map implementations also seem to use
Iurii Malchenko
@yurique
Sep 21 2018 23:11 UTC
@Ichoran I looked into those libs for immutable data; they seem nice, but they provide some ready structures, and I have one of my own (a tree-like one, not binary tree); so couldn’t really use them. But I’m planning to study the code to see how it’s done the right way.
mtak-
@mtak-
Sep 21 2018 23:23 UTC
@yurique congrats on your success!