These are chat archives for rust-lang/rust

25th
Apr 2017
Ashley Mannix
@KodrAus
Apr 25 2017 04:22

Hey all! I've got a library that deserialises lots of json structures with repeated strings that arrive over the network (http response). The data is chunked arbitrarily, so I can't use serde_json::from_slice for some non-copy goodness unless I copy the whole body into a contiguous slice anyways.

So my current idea is to use some kind of scoped thread local storage where I can intern those strings for a short time and copy them to owned Strings if the user needs to Send them.

Does anyone have any experience with scoped_tls or have any other recommendations/gotchas to watch out for?

Zachary Golba
@zacharygolba
Apr 25 2017 21:51
Hey :wave:
out of curiosity I was playing around and trying to figure out what function composition looks like in rust
a basic example of what I was trying to accomplish can be found below. I'm wondering if the example I'm providing is idiomatic and also what a more "real world" example looks like. I assume 'static for all three generics wouldn't get far in a real application.
fn compose<X, Y, Z>(a: Box<Fn(Y) -> Z>, b: Box<Fn(X) -> Y>) -> Box<Fn(X) -> Z>
    where X: 'static,
          Y: 'static,
          Z: 'static
{
    Box::new(move |c| a(b(c)))
}

fn main() {
    let composed = compose(Box::new(move |num| num - 1), Box::new(move |num| num * 2));

    println!("{}", composed(10));
    //=> 19
}
Zachary Golba
@zacharygolba
Apr 25 2017 21:58
I would be super excited if there is a way to acheive the flow of a(b(c)) via iter.rev().fold()
the thought of attempting to express that in the type system kind of makes my head hurt though
Denis Lisov
@tanriol
Apr 25 2017 22:08
@zacharygolba I've never seen this kind of code in Rust except for proofs of concept showing something is possible...
Zachary Golba
@zacharygolba
Apr 25 2017 22:08
interesting
this may be completely useless in real rust applications I was just kind of curious how somthing like this would work in the type system
I write a lot of functionally flavored js at work and I'm always wondering how patterns and techniques like higher order functions, function composition, partials/currying work in rust
Denis Lisov
@tanriol
Apr 25 2017 22:15
If you're interested in functional-like type system tricks, frunk may be worth looking at, IIUC.
Zachary Golba
@zacharygolba
Apr 25 2017 22:16
nice! thanks for sharing :)
Denis Lisov
@tanriol
Apr 25 2017 22:18
Thanks to its author :-) I prefer less functional style in general.
So I know it exists, but not much more.
Zachary Golba
@zacharygolba
Apr 25 2017 22:22
that's cool, it's nice you get to pick your prefered style when writting rust