These are chat archives for rust-lang/rust

22nd
Mar 2016
fstuess
@fstuess
Mar 22 2016 10:57
Path in struct: String is ok, Path yields E0277 (sized not implemented for type [u8]. Why is that?
Erik Hedvall
@Ogeon
Mar 22 2016 10:59
Path is a wrapper around [u8], which has no known size. You will have to put it behind a reference, just like with [u8]: &Path
fstuess
@fstuess
Mar 22 2016 11:01
thx. So why String is working? isn't it [u8], too? i don't get it.
Erik Hedvall
@Ogeon
Mar 22 2016 11:02
No, String is Vec<u8>. [u8] is just a slice of bytes and doesn't own its content.
The String version of Path is PathBuf.
(owned version)
fstuess
@fstuess
Mar 22 2016 11:05
Aarg, yes, hmm. Out of brain error. I think i got it. There are many more lessons i have to learn about rust to do it smoothly.
fstuess
@fstuess
Mar 22 2016 11:10
Next Q: had it before in a way: are there best practice examples of having a global configuration/settings file eg. a .toml and read it into a struct, array, map, something to have them as global variables? Heard about lazy static but i did not end up nicely with it.
Erik Hedvall
@Ogeon
Mar 22 2016 11:12
The other safe alternative is to pass a reference to it as a function parameter. It depends on what you want to achieve.
Lazy static is the recommended method if you want a runtime specified global variable of some sort. You can use a build script to generate compile time specified configurations, but I guess that's not the case here.
fstuess
@fstuess
Mar 22 2016 11:19
yep. so with lazy static i have to build a settings-file parser and a function that yields the file contents for some specific variable. Are there any examples lying around maybe? configuration in compile time would be possible but not desired because the target is a slow arm architecture and the customer cannot cross compile. thank you nevertheless!!
Erik Hedvall
@Ogeon
Mar 22 2016 11:27

I don't know of any examples off the top of my head, but you should look into Serde or rustc-serialize for parsing.

Doing configuration at compile time is only a good idea if it won't change later, which is a very slim use case.

Pete Hayes
@petehayes102
Mar 22 2016 11:53
I have a macro that accepts a variable number of args. I want to be able to invoke it from a slice - i.e. split each slice member into a separate arg. Is this possible with/without macros?
Erik Hedvall
@Ogeon
Mar 22 2016 12:16
Macros can't make use of runtime values. They can only see the tokens you are putting into them, so this macro can only see the identifier slice:
let slice = &[1u8, 2, 3];
do_something!(slice);
Pete Hayes
@petehayes102
Mar 22 2016 12:30
Yeah I was worried that’d be the case. I’m trying to wrap a C variadic fn in something more Rusty, but it’s proving very difficult. It’s not only an issue of variable numbers of args, but also their types. :/
Erik Hedvall
@Ogeon
Mar 22 2016 12:48
Sounds tricky. I don't know how variadic functions interacts with the ffi...
Pete Hayes
@petehayes102
Mar 22 2016 13:17
Rust allows you to use C’s variadic notation, fn(…), for extern fns only, but there’s virtually no language support in Rust, e.g. argument unpacking etc.
Erik Hedvall
@Ogeon
Mar 22 2016 13:31
I see. Is it really possible to call a variadic C function with a dynamic number of elements?
Pete Hayes
@petehayes102
Mar 22 2016 14:06
Yep, you can pass extra args to the Rust extern fn, provided they are C-compatible
I am as surprised as you that it actually works though!
Erik Hedvall
@Ogeon
Mar 22 2016 14:08
I mean dynamic as in run time determined number.
Pete Hayes
@petehayes102
Mar 22 2016 14:15
Oh right, yes you can do that too with the help of a macro
Erik Hedvall
@Ogeon
Mar 22 2016 14:29
Would it be possible to replicate the same behavior or is it too platform dependent?
Pete Hayes
@petehayes102
Mar 22 2016 15:04
Argh…Gitter notifications are not working for me. Sorry...
Replicate the variadic fn behaviour? Not sure what you mean.
Erik Hedvall
@Ogeon
Mar 22 2016 16:16

Same for me about the notifications.

The same behavior as that macro you mentioned. I mean, what does it do? Does it pass a pointer or does it plop the values on the stack?

Pete Hayes
@petehayes102
Mar 22 2016 16:25
Um...I don’t know the internals of how it works, but the C lib I’m binding to uses va_list. I used bindgen to generate the Rust FFI code and discovered the variadic notation in the function signature.
There’s a mention at the end of this Reddit thread: https://www.reddit.com/r/rust/comments/2qje69/ffi_dealing_with_va_list/
Most things you pass to C have to be raw pointers, excepting structs, ints and probably some other stuff I can’t remember
So I could pass pointers to my variadic extern fn via a direct call, or wrap it in a macro to accept a variable number of args in Rust:
macro_rules! zsock_send {
    ( $s:expr, $p:expr, $( $v:expr ),* ) => {{
        let rc = unsafe { $crate::czmq_sys::zsock_send($s as *mut ::std::os::raw::c_void, $p, $( $v ),*) };
        if rc == -1i32 { Err(()) } else { Ok(()) }
    }};
}
Erik Hedvall
@Ogeon
Mar 22 2016 16:31
That should work, I guess. I don't know of any other magic that would help
Pete Hayes
@petehayes102
Mar 22 2016 16:38
All good. I’ve blown a whole day on this so I’m sidelining it for a time when I have more patience :)
Erik Hedvall
@Ogeon
Mar 22 2016 16:40
Oh, you may have already looked for it, but some libraries provides non-variadic alternatives to variadic functions. Otherwise I can only wish you good luck on your next attempt :smile:
Pete Hayes
@petehayes102
Mar 22 2016 16:42
Yeah, sadly CZMQ doesn’t provide alternatives. Thanks for your help though mate. Until next time...
Kirill
@cyrexcyborg
Mar 22 2016 17:48
hello world! )
i'm just started learning Rust
fstuess
@fstuess
Mar 22 2016 17:59
Get it goin'. Me too. These brainy people here are very active and helpful :smile: