These are chat archives for rust-lang/rust

27th
Nov 2017
Judson Lester
@nyarly
Nov 27 2017 06:35
Working with a uritemplate crate, and I'd like to do:
impl IntoTemplateVar
  for IntoIterator<Item = ComponentType, IntoIter = Iterator<Item = ComponentType>> {
  fn into_template_var(self) -> TemplateVar {
    TemplateVar::List(self.into_iter().map(|el| (el as i32).to_string()).collect())
  }
}
But of course IntoIterator isn't my trait to implement traits on.
AFAICT, my options are: convince crate-owner to add an implementation of IntoTemplateVar for IntoIterator and/or just write local conversion function.
Robert Collins
@rbtcollins
Nov 27 2017 07:09
I thought you could add an implementation of it yourself
Zakarum
@omni-viral
Nov 27 2017 07:56
@nyarly Is IntoTemplateVar yours trait?
If that so you can do
impl<T> IntoTemplateVar for T
where T: IntoIterator<Item = ComponentType> {
  fn into_template_var(self) -> TemplateVar {
    TemplateVar::List(self.into_iter().map(|el| (el as i32).to_string()).collect())
  }
}
Jeroen Bollen
@Binero
Nov 27 2017 10:20
Does anyone know why this function would stack overflow? https://github.com/Binero/pointer-iteration-benchmarks/blob/master/src/lib.rs#L102
Jeroen Bollen
@Binero
Nov 27 2017 10:25
@jplatte That's a heap-allocated box though?
Jonas Platte
@jplatte
Nov 27 2017 10:25
You're creating a huge array on the stack ([0; SIZE]), then moving it to the heap (Box::new())
Jeroen Bollen
@Binero
Nov 27 2017 10:25
Does it first allocate on the stack and then move onto the heap?
pff
Jonas Platte
@jplatte
Nov 27 2017 10:26
Yup
Jeroen Bollen
@Binero
Nov 27 2017 10:26
Do I have to use a Vec then?
Jonas Platte
@jplatte
Nov 27 2017 10:26
That's why there is box_syntax
Jeroen Bollen
@Binero
Nov 27 2017 10:26
aha, and I am on nightly anyway
Jonas Platte
@jplatte
Nov 27 2017 10:26
You can use #![feature(box_syntax)] if you're okay with depending on nightly
Jeroen Bollen
@Binero
Nov 27 2017 10:26
This is something new by the way, because when it was written it worked
Jonas Platte
@jplatte
Nov 27 2017 10:26
And then write box [0; SIZE]
Jeroen Bollen
@Binero
Nov 27 2017 10:26
It should be box!
@jplatte It's on nightly, I am using #[bench] :)
Jonas Platte
@jplatte
Nov 27 2017 10:27
Ah, right
Maybe the stack allocation was optimized out before, but now isn't anymore for some reason
Jeroen Bollen
@Binero
Nov 27 2017 10:28
Oh box syntax changed, you're right. It's box [] now, not box!
Jonas Platte
@jplatte
Nov 27 2017 10:28
It's just box
the [] is because you're allocating an array
Aleksey Kladov
@matklad
Nov 27 2017 10:29
@Binero why do you need Box<[T; SIZE]> though? It's almost equivalent to Vec<T>, the latter also has .into_boxed_slice method which you can use on stable.
Jeroen Bollen
@Binero
Nov 27 2017 10:30
@matklad My line of thinking at the time must've been something like "Why use Vec if I really just want an array"
Ilya Bogdanov
@vitvakatu
Nov 27 2017 10:30
You want an array in the heap, so why not Vec?
Jeroen Bollen
@Binero
Nov 27 2017 10:47
@vitvakatu Because an array is not a Vec.
A slice is a slice, a vector is a vector and an array is an array.
If I don't ever need to resize it, why bring in the entire Vec, and lose some of the guarantees an array is.
Zakarum
@omni-viral
Nov 27 2017 10:48

You're creating a huge array on the stack ([0; SIZE]), then moving it to the heap (Box::new())

Shouldn't it be oprimized away?

Jeroen Bollen
@Binero
Nov 27 2017 10:49
@omni-viral It sure used to, when I wrote this two years ago I did not have any issues with stack overflow.
Zakarum
@omni-viral
Nov 27 2017 10:49
Have you tried it in release mode?
Jeroen Bollen
@Binero
Nov 27 2017 10:49
@omni-viral It's in cargo bench, so release mode is the only mode it can run in.
Zakarum
@omni-viral
Nov 27 2017 10:49
hmm
Jeroen Bollen
@Binero
Nov 27 2017 10:49
opt-level=3, I checked
Jonas Platte
@jplatte
Nov 27 2017 10:51
@Binero Since you didn't say anything, I assume using box syntax fixed it, right?
Jeroen Bollen
@Binero
Nov 27 2017 10:51
@jplatte Ah, yes, it did.
Zakarum
@omni-viral
Nov 27 2017 10:51
I thought that when you write Box::new(foo()) then foo will write result directly to the heap allocated space.
Jonas Platte
@jplatte
Nov 27 2017 10:51
@omni-viral If that was the case, box_syntax wouldn't exist
AFAIK, it exists specifically as a guarantee that no stack allocation is happening
Zakarum
@omni-viral
Nov 27 2017 10:52
Isn't box EXPR be synonim for Box::new(EPXR)?
Jeroen Bollen
@Binero
Nov 27 2017 10:52
@jplatte Box syntax even existed before I wrote that code
Jonas Platte
@jplatte
Nov 27 2017 10:52
@omni-viral Well it obviously isn't, otherwise it wouldn't have helped in @Binero's case
Jeroen Bollen
@Binero
Nov 27 2017 10:52
I also was under the impression it was synonymous to Box::new(EXPR), but you might be right.
Zakarum
@omni-viral
Nov 27 2017 10:52
I want rc_syntax, arc_syntax and my_custom_pointer_syntax then
Jonas Platte
@jplatte
Nov 27 2017 10:52
I'm not sure if it's supposed to be the same though
Jeroen Bollen
@Binero
Nov 27 2017 10:53
Interesting point, Arc and Rc do not have their own box_syntax.
Jonas Platte
@jplatte
Nov 27 2017 10:53
@omni-viral I'm not entirely sure, but I think the placement stuff is supposed to solve this for other allocations
Jeroen Bollen
@Binero
Nov 27 2017 10:54
Placement stuff?
Jonas Platte
@jplatte
Nov 27 2017 10:54
Maybe for box that's the future too, seen as box_syntax has been around for so long but was never stabilized
You don't get deprecation warnings currently though..
Zakarum
@omni-viral
Nov 27 2017 10:54
@Binero in PLACER { EXPR } stuff
Jonas Platte
@jplatte
Nov 27 2017 10:54
rust-lang/rust#27779
@omni-viral I think the syntax is going to be place <- expr
since this has been merged: rust-lang/rfcs#1228
Zakarum
@omni-viral
Nov 27 2017 10:57
Like vec.back() <- foo()?
Looks nice
Jonas Platte
@jplatte
Nov 27 2017 10:57
Yup, like that
Zakarum
@omni-viral
Nov 27 2017 10:58
I've caught compiler error
Unrelated to placers
Jonas Platte
@jplatte
Nov 27 2017 10:59
Also it seems like box might be generic in its "return type" (if you can call it that) in the future
Zakarum
@omni-viral
Nov 27 2017 11:00
I;ve tried to implement Drop for generic type and got
implementing Drop requires a struct

error: internal compiler error: src/librustc_typeck/check/dropck.rs:64: should have been rejected by coherence check: T
Jonas Platte
@jplatte
Nov 27 2017 11:09
Okay, just read way too much discussion on placement new ^^
Syntax is still not decided, apparently, but <- was agreed on for the initial implementation, which gives it a little bit of an advantage, bc. choosing different syntax later will mean having to convert existing code.
Judson Lester
@nyarly
Nov 27 2017 19:21
@omni-viral It's not - it belongs to the uritemplate crate (with the helpful advice that "you can implement this trait for your own types")
(sorry for delayed response)
Fra ns
@snarf95_twitter
Nov 27 2017 20:39
Anyone up for a performance review of my voxel meshing algorithm?
Fra ns
@snarf95_twitter
Nov 27 2017 20:47
Like what can I do to further improve performance?
Denis Lisov
@tanriol
Nov 27 2017 20:57
@snarf95_twitter Could you please detail what is this algorithm doing?
Fra ns
@snarf95_twitter
Nov 27 2017 21:06
Well I don't know if its complicated but it may be a bit obfuscated :p Basically it generates a mesh of cubes for a chunk of voxel data. for each voxel it checks its neighbours and appends 2 triangles (6 indices) per side. Then it figures out which vertices are used and only appends those that are used and updates the indices so that it matches..
Denis Lisov
@tanriol
Nov 27 2017 21:11
First the most correct question: have you tried profiling it and is it really a bottleneck in release mode? :-)
Personally, I'm not sure the reindex loop is optimized nicely...
...but otherwise feels mostly good.
Fra ns
@snarf95_twitter
Nov 27 2017 21:16
before I basically loop over the entire cube_indices (could be up to 36) and decreased index by one if index was "too high"
Fra ns
@snarf95_twitter
Nov 27 2017 21:22
Haven't done any profiling :p but yeah this is pretty hot code
Denis Lisov
@tanriol
Nov 27 2017 21:26
Maybe something like the following?
let indices_lookup = (0..8).scan(0, |state, idx| {
    if(points_bits.get_bit(idx)) {
        state += 1;
    }
    Some(state - 1)
});
However, this certainly needs profiling ;-)
Fra ns
@snarf95_twitter
Nov 27 2017 21:27
that looks interesting :p
Zakarum
@omni-viral
Nov 27 2017 21:27

you can implement this trait for your own types

@nyarly but not for type parameters

Instead you can create your own type wrapper. Wrap any IntoIterator in it and then implement IntoTemplateVar for it
Like this
struct MyOwnTypeWrapperCanBeNamedAsIWant<T>(T);

impl<T> IntoTemplateVar for MyOwnTypeWrapperCanBeNamedAsIWant<T>
where
    T: IntoIterator
{
    ....
}
Fra ns
@snarf95_twitter
Nov 27 2017 21:29
Haven't used scan before but that looks really neat :) thx @tanriol I'll have try an profile it later
apiraino
@apiraino
Nov 27 2017 21:47
Hello everyone! I'm getting my feet wet with WASM and Rust. I've installed the toolchain using wargo, I've followed this turorial
Steve Klabnik
@steveklabnik
Nov 27 2017 21:47
so, you may want to know that some new stuff has happened since then!
apiraino
@apiraino
Nov 27 2017 21:47
Now I'm trying to compile a Rust project that has reqwest as dependency
yeah thanks! I will wait for the next stable release of Rust ;-)
however the compilation fails on the library mio
here's the stacktrace of the error
Steve Klabnik
@steveklabnik
Nov 27 2017 21:48
yeah that makes sense
apiraino
@apiraino
Nov 27 2017 21:49
any hint? I' pretty mush a newbie here ;(
Steve Klabnik
@steveklabnik
Nov 27 2017 21:49
... though i expected a different error than that one
mio is a wrapper over OS primitives that i'm not sure exist in the browser
so i wouldn't expect it to really work
apiraino
@apiraino
Nov 27 2017 21:51
ok, say I want to write an API client in Rust, compile it to WASM and hand it over to my frontend team for playing
should I use a more barebone HTTP library?
I'd started working on this: https://github.com/apiraino/rust-meetup-client
Steve Klabnik
@steveklabnik
Nov 27 2017 21:52
i'm not sure honestly; i think the ecosystem is still so new that i'm not sure
like, i doubt that there's a crate that exposes ajax natively to rust, you'd probably have to implement that too
Judson Lester
@nyarly
Nov 27 2017 21:53
@omni-viral Right - for my purposes it was just more straightforward to write a conversion function directly, since I'd need to e.g. MyListIntoVarWrapper(&[AType]) anyway. But it seems mete for the source crate to generalize their trait, right?
apiraino
@apiraino
Nov 27 2017 21:53
hum ok.
anyway thanx @steveklabnik (btw it was nice meeting you in Zurich in Sept. I was at the rustbridge with you Ashley et al)
Fra ns
@snarf95_twitter
Nov 27 2017 21:59
let indices_lookup = (0..8)
    .scan(0u16, |state, idx| {
        if points_bits.get_bit(idx) {
            *state += 1;
            Some(*state - 1)
        } else {
            Some(*state)
        }
    })
    .collect::<smallvec::SmallVec<[u16; 8]>>();
this is what I ended up with, works great :) thx again @tanriol
Steve Klabnik
@steveklabnik
Nov 27 2017 22:02
@apiraino :D