These are chat archives for rust-lang/rust

26th
Apr 2018
Dylan DPC
@Dylan-DPC
Apr 26 2018 05:00
@tanriol yes I am. Was hoping for a direct way 😂
Berardino
@berab78_twitter
Apr 26 2018 05:21
Hi, I am trying to invoke a C library (pcap) from rust and I am having some problem when passing a rust string to C. I managed to make it to work but I don't understand why it actually works . Here the working solution and the not working one .
// This works
let cname = CString::new(self.name.clone()).unwrap();
let handle = pcap_create(cname.as_ptr(), err.as_mut_ptr());

// This does not work. Most likely the string gets deallocated from rust
// before it is used by the C pcap lib
let cname_ptr = CString::new(self.name.clone()).unwrap().as_ptr();
let handle = pcap_create(cname_ptr, err.as_mut_ptr());
In addition, I don't understand why I also need to clone the String
Michal 'vorner' Vaner
@vorner
Apr 26 2018 05:40
@berab78_twitter Any reason why you go this way? There's a pcap crate and I found it good enough in practice and quite pleasant to use. Anything missing there?
Berardino
@berab78_twitter
Apr 26 2018 05:45
learning I guess :)
Dylan DPC
@Dylan-DPC
Apr 26 2018 05:48
@tanriol though ya looking again at the match-with-guards, it looks direct enough :smile:
Michal 'vorner' Vaner
@vorner
Apr 26 2018 05:49
OK. Then, what does it do if you don't clone? I think it shouldn't be necessary. You should be able to do either CString::new(&self.name) or CString::new(self.name) (but the later one consumes it, and you have to own self to do that.
As for the other problem, I think it is described here: https://doc.rust-lang.org/std/ffi/struct.CString.html#deref-methods
Berardino
@berab78_twitter
Apr 26 2018 05:53
@vorner --> src/lib.rs:163:25 | 163 | let cname = CString::new(&self.name).unwrap(); | ^^^^^^^^^^^^ the trait `std::convert::From<&std::string::String>` is not implemented for `std::vec::Vec<u8>
where self.name is a String,
Michal 'vorner' Vaner
@vorner
Apr 26 2018 05:57
self.name.as_bytes() maybe? Or consuming the string (so it's not copied)
Berardino
@berab78_twitter
Apr 26 2018 05:59
great! it worked. I completely missed that part of the documentation. And it makes perfectly sense now. Thanks @vorner
Dylan DPC
@Dylan-DPC
Apr 26 2018 07:22
is there a way i can get which "key" or iteration i'm on while iterating a vector?
Dylan DPC
@Dylan-DPC
Apr 26 2018 07:25
oh thanks
An example function to check for palindrome.
Level of abstraction is quite amazing. Is there any hidden cost associated with this abstraction ?
Dylan DPC
@Dylan-DPC
Apr 26 2018 12:09
hey @prataprc :wave:
Tom Cumming
@tomcumming
Apr 26 2018 12:12
@prataprc maybe the .rev() is reading into mem
nope ignore that
should be very fast and cheap
prataprc
@prataprc
Apr 26 2018 12:41
Hey @Dylan-DPC
@tomcumming very impressive. Once I learn benchmarking rust code will try and measure the cost. Thanks.
Dylan DPC
@Dylan-DPC
Apr 26 2018 14:00
This message was deleted
Dylan DPC
@Dylan-DPC
Apr 26 2018 15:42
Another weird question for the day -> so i'm reading from a file to a string and then deserializing it. What's the best way to do this without knowing the "structure" of the end result?
right now, i'm doing :serde_json::from_str(..) and then using as_array() or as_object etc depending on the structure of the file , but is there a way i can generalise that?
Tom Cumming
@tomcumming
Apr 26 2018 16:19
perhaps you can from_str into one of these https://docs.serde.rs/serde_json/enum.Value.html
Dylan DPC
@Dylan-DPC
Apr 26 2018 16:41
@tomcumming thanks I'm doing that but I need to choose as_array() or as_object() depending on the structure
Ingvar Stepanyan
@RReverser
Apr 26 2018 16:42
@Dylan-DPC What you're doing should work, but if you are worried about performance, you might try using untagged enum
It will choose the variant based on shape of data which sounds like what you want
Dylan DPC
@Dylan-DPC
Apr 26 2018 18:58
ok thanks :)

last issue of the day :P
I'm having a Vec<String> (object named foo)and i'm doing foo.into_iter().find(some_string); but i get:

the trait for<'r> std::ops::FnMut<(&'r std::string::String,)> is not implemented for std::string::String

how do i solve this?

oh damn i think it takes a closure facepalm
Eugene Tolmachev
@et1975
Apr 26 2018 21:15
scottmcm
@scottmcm
Apr 26 2018 21:42
1) flat_map it on iterators, not slices
2) you have a slice of DSTs, which is not what you want. You probably want something like &[&Iterator<Item=u8>] or you want generics (and thus not a closure)
Eugene Tolmachev
@et1975
Apr 26 2018 21:50

@scottmcm hey, thanks and bear with me please, I'm new :)
so, I tried |bytes:&Iterator<Item=Iterator<Item=u8>>| and that does't work:

the flat_map method cannot be invoked on a trait object

I'm reluctant to use generic form, cause 1) I'm learning and trying to use familiar patterns 2) I'm running the code on an MCU, so even if not in this mickey-mouse case, but in general the size explosion the generics could cause is a potential concern
Dmitriy
@dpogretskiy
Apr 26 2018 21:54
familiar patterns won't work almost for sure :)
but basis is such, Iterator is a trait
which means it's size is unknown
so you can generalize collection, slice etc over it
you can do it for functions however which is not useful for this case
also iterator does not store any data
so if it is bytes, they can't be inside of iterator, so you have something underneath, which shares lifetime with iterator
and iterators can be nested, but you won't get one all that easily
what you are trying to do is flatten from scala or something like that
Eugene Tolmachev
@et1975
Apr 26 2018 22:00
F# (GC luxury :))
fair enough, so I can't use traits, but why does it not let me use flat_map on a slice? It's not implement for it?
Dmitriy
@dpogretskiy
Apr 26 2018 22:01
&[].iter()
is an iterator over slice, if there is one
but problem is you can't store iterators inside of any slice
since iterator is a trait
and therefore there can be any struct implementing it
Eugene Tolmachev
@et1975
Apr 26 2018 22:03
which was the whole hope in this twisted plot :)
Dmitriy
@dpogretskiy
Apr 26 2018 22:03
you can box them however
Eugene Tolmachev
@et1975
Apr 26 2018 22:03
nope, no heap :(
Dmitriy
@dpogretskiy
Apr 26 2018 22:04
haha, straight from gc luxury to no heap
Eugene Tolmachev
@et1975
Apr 26 2018 22:04
I know, right?
alright, thanks folks, it's back to the drawing board for me then
Dmitriy
@dpogretskiy
Apr 26 2018 22:05
well it's totally unclear what you are trying to do, but there probably will be heap somewhere
good luck with that, tho