These are chat archives for rust-lang/rust

22nd
Sep 2017
Ashley Mannix
@KodrAus
Sep 22 2017 01:51
@diegor8 I think @vorner is also saying the Rust compiler itself is the thing stackoverflowing, rather than their own code overflowing
CrLF0710
@crlf0710
Sep 22 2017 05:03
@TatriX maybe you're running rustc in a virtual box shared folder? I heard that rustc.exe does have some stability issues when it is using network drives and memory disks and stuff like those.
Michal 'vorner' Vaner
@vorner
Sep 22 2017 05:41
@KodrAus Yes, indeed. And I deserved the crash, because I was abusing experimental features.
TatriX
@TatriX
Sep 22 2017 05:54
@crlf0710 yes I am. Thanks for the advice, I'll try to copy the files.
mindcrime-ilab
@mindcrime-ilab
Sep 22 2017 06:17
Und
Fra ns
@snarf95_twitter
Sep 22 2017 07:49
Hello rust community, I'm kinda confused about how to store Fn in structs. I got a Box<Fn(&[T]) -> T> in my struct and I don't know how to call it.. any idea?
this doesn't work compute_cell.compute_func(&compute_input_values) where compute_func is the boxed func in play
Jonas Platte
@jplatte
Sep 22 2017 07:52
I haven't done this before, but I'd expect it to work like this: (*compute_cell.compute_func)(&compute_input_values)
Fra ns
@snarf95_twitter
Sep 22 2017 07:54
woah thanks @jplatte , works purrfectly now :)
lemonxah
@lemonxah
Sep 22 2017 07:55
good day, i have some lifetime issues and i know why it happens but is there a way to fix this? https://play.rust-lang.org/?gist=6b842e1386b68b5318cc0e0434206099&version=nightly
TatriX
@TatriX
Sep 22 2017 07:59
@crlf0710 copying files to the local virtualbox drive did the trick. Thank you very much!
Jonas Platte
@jplatte
Sep 22 2017 08:02
@lemonxah from_str gives you a result that (potentially) borrows from the string slice you put into it. You can use DeserializeOwned to exclude that case: https://play.rust-lang.org/?gist=795a2653dfb60d5fd863d085f0d3cc58&version=nightly
If you want to only deserialize into owning types that is. Otherwise what you are trying to do is impossible, but it sounds like you already know that :)
lemonxah
@lemonxah
Sep 22 2017 08:04
than you for the input but it turns out there is a way :) from IRC i got https://doc.rust-lang.org/nightly/nomicon/hrtb.html
that solves it by adding for<'a>, where for<'a> A: serde::de::Deserialize<'a>
that solved my issue
Jonas Platte
@jplatte
Sep 22 2017 08:05
Yeah, that's exactly what DeserializeOwned does too ;)
lemonxah
@lemonxah
Sep 22 2017 08:06
cool
there si soooo much of rust i still dont understand or know
its going to be an exciting journey
Jonas Platte
@jplatte
Sep 22 2017 08:07
Yeah Rust really has quite a large amount of language features. But luckily you don't need to learn them all at once :)
lemonxah
@lemonxah
Sep 22 2017 08:07
yeah but i need to know a lot of them at once :P not all
Fra ns
@snarf95_twitter
Sep 22 2017 08:11
cloning a func to avoid borrowing, possible?
right now I've put a 'static constraint on my input func
but that doesn't work in every case
Jonas Platte
@jplatte
Sep 22 2017 08:13
functions don't implement Clone in the current stable.
But they do implement Copy
(which really shouldn't be possible, but you know.. compiler magic)
So put a Copy bound on it and I think it should work
Fra ns
@snarf95_twitter
Sep 22 2017 08:14
now I'm getting the parameter typeFmay not live long enough
callback_func: Box::new(callback)
Jonas Platte
@jplatte
Sep 22 2017 08:15
Ohh yeah, you have those boxes.
That might complicate things
Fra ns
@snarf95_twitter
Sep 22 2017 08:15
hmm
Jonas Platte
@jplatte
Sep 22 2017 08:15
Sounds like you're very close to replicating rust-lang/rust#28229 :D
Fra ns
@snarf95_twitter
Sep 22 2017 08:15
I don't know why I'm using boxes tbh
Jonas Platte
@jplatte
Sep 22 2017 08:15
Unless you're on a current nightly
Fra ns
@snarf95_twitter
Sep 22 2017 08:15
I'm on nightly
Jonas Platte
@jplatte
Sep 22 2017 08:16
Oh, then Fn(T) -> U + Clone might actually work
But maybe you could explain in a bit more detail what it is that you're trying to do, since you're not sure whether you should have boxes at all?
Fra ns
@snarf95_twitter
Sep 22 2017 08:17
Clone doesn't seem to work
I can show my entire code 2 sec
it's the add_callback fn that's in question
Fra ns
@snarf95_twitter
Sep 22 2017 08:33
this test is causing issues with my 'static approach:
fn compute_cells_fire_callbacks() {
    // This is a bit awkward, but the closure mutably borrows `values`.
    // So we have to end its borrow by taking reactor out of scope.
    let mut values = Vec::new();
    {
        let mut reactor = Reactor::new();
        let input = reactor.create_input(1);
        let output = reactor.create_compute(&vec![input], |v| v[0] + 1).unwrap();
        assert!(reactor.add_callback(output, |v| values.push(v)).is_ok());
        assert!(reactor.set_value(input, 3).is_ok());
    }
    assert_eq!(values, vec![4]);
}
Jonas Platte
@jplatte
Sep 22 2017 08:38
I fixed it! :D
tbh I don't fully understand why this works, but your code doesn't..
Fra ns
@snarf95_twitter
Sep 22 2017 08:38
o wow that's amazing tell me :)
Jonas Platte
@jplatte
Sep 22 2017 08:38
I'll put it on the playground, wait a sec
Fra ns
@snarf95_twitter
Sep 22 2017 08:39
passing funcs around in rust is hard lol
Your test fails, but I made it compile at least :D
Also I removed those -> ()s as they aren't necessary
Fra ns
@snarf95_twitter
Sep 22 2017 08:40
haha that's a lot of 'a introduced
thanks a lot @jplatte
Jonas Platte
@jplatte
Sep 22 2017 08:41
you're welcome :)
Fra ns
@snarf95_twitter
Sep 22 2017 08:42
now on to making it pass the test :p
Jonas Platte
@jplatte
Sep 22 2017 08:42
Oh, one more thing: In create_compute you're taking dependencies as a slice and then cloning it via to_vec. IMO it makes more sense to pass in a Vec right away.
(if you need ownership anyway, that is)
Fra ns
@snarf95_twitter
Sep 22 2017 08:43
hmm yea you're right, but I gotta make my function fit with the test suite requirements
Jonas Platte
@jplatte
Sep 22 2017 08:44
Ah, I've seen that site before
Fra ns
@snarf95_twitter
Sep 22 2017 08:45
it's pretty nice but also kinda awkward sometimes with the tests
Jonas Platte
@jplatte
Sep 22 2017 08:45
Hm, have you tried https://www.codewars.com/ ?
Same concept, also has Rust tasks.
Haven't used it much myself though, so it might have the same sort of issues.
Fra ns
@snarf95_twitter
Sep 22 2017 08:46
ok I'll maybe try it later
thx
Btw is my usage of box justified in this scenario? I haven’t really used Box before
Jonas Platte
@jplatte
Sep 22 2017 08:53
Yeah it is
Without box, all your callbacks could only ever be the same closure.
Although.. If your closures wouldn't borrow anything, they could be coerced to plain fns, and you could store those without a box.

The difference there is memory layout. Anonymous functions that don't close over their environment are exactly the same as regular functions, e.g.

fn a(x: i32) -> i32 { x } is the same as |x: i32| x.

They both are simply a function pointer
But once your anonymous functions use stuff from their environment (or "close over" the environment, which is where the name closure comes from) you need to store that environment as well.
Fra ns
@snarf95_twitter
Sep 22 2017 09:00
Ok that actually makes sense but is sorta confusing in terms of syntax etc
Jonas Platte
@jplatte
Sep 22 2017 09:00

e.g. the return value of

fn wat(x: i32) -> ??? {
    || x + 1
}

can't be just a function pointer because || x + 1 isn't really a function..

Bc. it does different things depending on how you obtained it, e.g. wat(1) or wat(2)
Makes sense?
Yeah fn(i32) -> i32 and Fn(i32) -> i32 are really too similar syntactically for how different they actually are.
I think I've seen the idea of Call(i32) -> i32 float around, but it's very unlikely for such a fundamental thing to ever be renamed
Jonas Platte
@jplatte
Sep 22 2017 10:03
Hm, now that I've tried a few times, I can't recommend Rust on Codewars.
lemonxah
@lemonxah
Sep 22 2017 10:03
why not?
Jonas Platte
@jplatte
Sep 22 2017 10:03
The exercises are decent (albeit much smaller than what exercism seems to offer), but there is one huge issue and that is timeouts.
I guess they must include build times in there 12s timeout or something like that
Because trivial pieces of code time out
Sometimes they work, very often they time out when they run in <1s locally.
Okay maybe the scope of the exercises is actually not that different. The one @snarf95_twitter was doing was the very last one in the Rust course.
Either way the timeouts on CodeWars suck
Maxwell Leisner
@CoffeeFrame
Sep 22 2017 12:09
Good morning folks
lemonxah
@lemonxah
Sep 22 2017 12:10
good afternoon
Maxwell Leisner
@CoffeeFrame
Sep 22 2017 12:10
I'm new to these here parts
lemonxah
@lemonxah
Sep 22 2017 12:10
welcome :P
Maxwell Leisner
@CoffeeFrame
Sep 22 2017 12:10
Thanks :3
I want to help with the rust impl initiave but am not the best coder
tbh I'm best at linux admin stuffs
er, strongest at
lemonxah
@lemonxah
Sep 22 2017 12:12
i am not sure what you are refering to
Maxwell Leisner
@CoffeeFrame
Sep 22 2017 12:13
wait, this is the lang room, not the rust-impl -period room
):
Aravindh Sridharan
@hardvain
Sep 22 2017 16:37
I am trying to write a linked list and one of the functions is pub fn add_all(&mut self, listToBeAdded: &List){} My question is what is the idiomatic way to get the listToBeAdded. Should it be an immutable reference with & or should I take ownership of the value? Because when I assign the list to the next pointer of the current list, I am essentially moving out borrowed content if I use a &. What is the idiomatic way to write such a function in rust? One approach is i can use clonebut what if the the elements of the list are not clone able? I can provide the structure of the list if my question is not clear
CrLF0710
@crlf0710
Sep 22 2017 16:47
The idiomatic way is to separate the concepts of "a list" and "a node". Regard "node" as a lower level of abstraction that is included(owned) within the "list" abstraction. So you just define the behavior(effect) of the whole function, leaving the changes of pointers and nodes as implementation details. Effectively you're reimplementing LinkedList<T>::append which is fn append(&mut self, other: &mut LinkedList<T>)
Aravindh Sridharan
@hardvain
Sep 22 2017 16:52
@crlf0710 The following is my structure. I am learning this from http://cglab.ca/~abeinges/blah/too-many-lists/book/first-layout.html.
pub struct List {
    head: Link,
}

enum Link {
    Empty,
    More(Box<Node>),
}

struct Node {
    elem: i32,
    next: Link,
}
So I do have a lower level abstraction as Node.
But taking a mutable borrow is rather limiting for the caller of the function. isnt it?
CrLF0710
@crlf0710
Sep 22 2017 16:56
Actually you're transferring the ownership of the nodes, so you either accept an &mut List or a wholeList... The second one can be seen as the first one plus dropping the now empty list.
Is the first one more limiting?
Aravindh Sridharan
@hardvain
Sep 22 2017 17:01
I actually dont understand your question. The options I had was either accepting &Listor List. Since this is a linked list, the last node of the first list can point to the the head of listToBeAddedand with this reasoning, Its enough if i set self.last.next = listToBeAdded.head. self.lastis an imaginary value here. So for this reason I dont need a mutable borrow. Taking ownership is limiting for the caller as they cannot use listToBeAdded again.
CrLF0710
@crlf0710
Sep 22 2017 17:06
In Rust everything has exactly one owner except where explicit mechanism like refcounting is involved... If it was implemented in your way, the node is now within the first list, and when the first list is dropped, then the node will be dropped too. And the second list is now broken. This breaks the safety rules...
Aravindh Sridharan
@hardvain
Sep 22 2017 17:11
Ok.Oh ya. I didnt think from this perspective that just doing self.last.next = listToBeAdded.headwill result in two owners for listToBeAdded. This makes sense . So an immutable borrow is out of question. Then I can only think of taking ownership. But why does the rust linkedlist takes a mutable borrow as in https://doc.rust-lang.org/src/alloc/linked_list.rs.html#285
CrLF0710
@crlf0710
Sep 22 2017 17:13
From the user's perspective, the function move all contents of the list2 into list1. So it modifies both lists.
Aravindh Sridharan
@hardvain
Sep 22 2017 17:16
ok. This makes sense. thanks a lot. But then I have one more question. In the docs, it says
/// Moves all elements from `other` to the end of the list.
    ///
    /// This reuses all the nodes from `other` and moves them into `self`. After
    /// this operation, `other` becomes empty.
CrLF0710
@crlf0710
Sep 22 2017 17:18
yeah
Aravindh Sridharan
@hardvain
Sep 22 2017 17:18
So the caller essentially will not be able to use the passed list because it will be mutated. If in case the caller needs to use this, they need to clone the list before passing. My question is, why not simply take ownership and assign self.tail.next = listToBeAdded.head instead of getting a mutable reference and making the listToBeAdded empty
Having an empty listToBeAdded in the caller site is waste of memory isnt it?
if we take ownership, the caller will no longer have access to listToBeAdded and incase if they need to reuse it, they will have to clone it anyway
kindly correct if i am wrong
also what will the caller do if there is a requirement for the list to be used after calling append but the list is not cloneable?
CrLF0710
@crlf0710
Sep 22 2017 17:21
Actually you're right. in real world rust you'll actually use extend in this case... a.extend(b.iter().cloned()); like this.
Iterators are another story though
Denis Lisov
@tanriol
Sep 22 2017 17:22
@hardvain The caller may want to reuse the passed list later instead of creating a new empty list when he needs one.
Aravindh Sridharan
@hardvain
Sep 22 2017 17:23
@crlf0710 But what if the contents of the list is not cloneable?
CrLF0710
@crlf0710
Sep 22 2017 17:24
Then there's only one copy of all those values... how can you expect them to occur in both lists?
Aravindh Sridharan
@hardvain
Sep 22 2017 17:25
@crlf0710 uh. Ya. Sometimes I miss to see the obvious things. I want a copy of non cleaneable elements. How awesome I am. But thanks a lot for the explanation.
@tanriol Do you tell this as the reasoning behind using a mutable borrow and not taking ownership?
Denis Lisov
@tanriol
Sep 22 2017 17:26
A possible reasoning. There are some use cases that become less ergonomic if you take the list by value.
CrLF0710
@crlf0710
Sep 22 2017 17:28
I imagine if AddAssign is implemented for these collection types they'll take the operands by value. :)
But generally people use iterators more, and iterators easily satisfy most needs~
Aravindh Sridharan
@hardvain
Sep 22 2017 18:24
Understood. Thank you
Dmitriy
@dpogretskiy
Sep 22 2017 20:02
Hi there, my question is, is abstracting over trait function arguments a thing?
Say for example i have a trait State which is amethyst state pushdown automaton thingy, so it has a bunch of methods like update/handle_events and such, and i want it have a type parameter, that represents data i want to have for decisions, and although it desn't store anything, i need to introduce a type parameter. Which goes wild if i have references in it, for example.
As far as i got, higher kinded types/higher kinded types that abstract over lifetimes is not really a thing atm.
Steve Klabnik
@steveklabnik
Sep 22 2017 20:43
yeah, HKT is not a thing. ATC is coming, which gives you some of the power
i am not 100% sure what to recommend to you in this situation
unreadable
@unreadable
Sep 22 2017 20:46
Hey, are there any future plans to rewrite the stdlib without libc dependency?
At least on unix
Dmitriy
@dpogretskiy
Sep 22 2017 20:47
@steveklabnik for now i just used full power of copy-pasting (poor man specialization), but could use macro's, now that i think of it...
Jonas Platte
@jplatte
Sep 22 2017 20:48
Dmitriy
@dpogretskiy
Sep 22 2017 20:48
time to learn rust macro programming
unreadable
@unreadable
Sep 22 2017 20:50
Is steed ready to use?
Steve Klabnik
@steveklabnik
Sep 22 2017 20:51
@dpogretskiy :)
good luck
Dmitriy
@dpogretskiy
Sep 22 2017 20:53
@krypton97 somewhat, always worth trying :wink:
unreadable
@unreadable
Sep 22 2017 20:54
ofc! Thanks for that, knew about it and I'm pretty impressed :)
Dmitriy
@dpogretskiy
Sep 22 2017 20:58
@krypton97 also look for https://github.com/redox-os/redox
they probably use something like pure-rust std
unreadable
@unreadable
Sep 22 2017 21:02
yep they do