These are chat archives for rust-lang/rust

1st
Oct 2018
Iurii Malchenko
@yurique
Oct 01 2018 12:57
hi everyone!
I have a small question :)
if I have a Vec, and I want to take the first element, like v.first(), and then I want to move the value out of that option (I don’t care about the vector itself or any of the other elements in it anymore), how can I do that? (first() gives an Option with a reference and I can’t cannot move out of borrowed content)
red75prime
@red75prime
Oct 01 2018 13:09
v.into_iter().next()
v will be dropped
Iurii Malchenko
@yurique
Oct 01 2018 13:15
@red75prime thanks!
Iurii Malchenko
@yurique
Oct 01 2018 19:31
is there a way share an Arc<MyTrait> between threads? :)
Denis Lisov
@tanriol
Oct 01 2018 19:41
Arc<MyTrait + Send> or possibly Arc<MyTrait + Send + Sync>
Iurii Malchenko
@yurique
Oct 01 2018 19:52
I’m trying to do this, and it seems I’d have to add +Send+Sync literally everywhere in the code (all function return types, param types, in the structs)
does that sound right or am I doing it wrong?
Denis Lisov
@tanriol
Oct 01 2018 19:53
Well, I'm not sure whether you really need the ability to share everything...
...if you do, it's probably easier to add the requirements to the trait itself
trait MyTrait: Send + Sync { ... }
Ichoran
@Ichoran
Oct 01 2018 19:55
It has to be everywhere because that is the indication that it's safe to send. If any bit is unsafe, it's all unsafe.
And if you only sometimes want the trait to be Send + Sync, you can make a type alias for it: type MyShare = MyTrait + Send + Sync;
Iurii Malchenko
@yurique
Oct 01 2018 19:57
I just made it trait MyTrait: Send + Sync (and removed all the +Send+Sync from all my Arcs) - and it works!
Ichoran
@Ichoran
Oct 01 2018 19:57
:+1:
Iurii Malchenko
@yurique
Oct 01 2018 19:57
so it’s just a matter of telling the compiler that it’s safe to share, right?
(I mean, I have to be sure it is, and if not - the compiler can’t help; and in this case I’m pretty sure)
Ichoran
@Ichoran
Oct 01 2018 19:58
Yeah. The compiler can check some things but not everything.
Denis Lisov
@tanriol
Oct 01 2018 19:58
Of telling the compiler that everything implementing this trait must be safe to share.
Iurii Malchenko
@yurique
Oct 01 2018 19:59

Of telling the compiler that everything implementing this trait must be safe to share.

right! this starts to make sense in my head, I think :)
Thanks for helping!

Denis Lisov
@tanriol
Oct 01 2018 20:00
Which results in the compiler both using this requirement (now you can use them from different threads) and enforcing it (you won't be allowed to implement it for something thread-bound).
Iurii Malchenko
@yurique
Oct 01 2018 20:08
this is neat!
(at first I was afraid I’d have to (re)implement a lot for it to work; now it makes sense and seems almost too easy :) )