These are chat archives for rust-lang/rust

23rd
Mar 2016
vgocoder
@vgocoder
Mar 23 2016 04:59
how about redox os ?Anybody known that?http://www.redox-os.org/
Yuri Setiantoko
@zerosign
Mar 23 2016 09:04
hi, for Rust ffi, how could I pass **char (array of string) in C as a param in rust function ? I try to use **mut c_char but it didn't work. Anybody willing to help me ?
Peter Atashian
@retep998
Mar 23 2016 09:05
*mut *mut c_char
Yuri Setiantoko
@zerosign
Mar 23 2016 09:05
I see
@retep998 thanks
Jeroen Bollen
@Binero
Mar 23 2016 17:05
I am writing a macro, and I am basically wrapping around a structure
How would I accept both pub and non-pub structures?
Is there a fragment specifier for it?
like $vis:visibility struct $name:ident {
Erik Hedvall
@Ogeon
Mar 23 2016 17:06
No, but you could try with $vis:tt and cross your fingers.
Jeroen Bollen
@Binero
Mar 23 2016 17:07
aight
Erik Hedvall
@Ogeon
Mar 23 2016 17:08
Macros does unfortunately not cover the whole syntax. Lifetime parameters are an example of something that is almost impossible to deal with.
Jeroen Bollen
@Binero
Mar 23 2016 17:21
Is there a way in a macro to optionally allow for something?
It seems to only be possible if you want to either have it any number of times, but not "0 or 1"
Pete Hayes
@petehayes102
Mar 23 2016 17:22
As in an optional argument? You can use $( $arg:expr ),* for zero or more args
Jeroen Bollen
@Binero
Mar 23 2016 17:22
Yeah I know about that one
Pete Hayes
@petehayes102
Mar 23 2016 17:23
Yeah sorry you beat me to it
Jeroen Bollen
@Binero
Mar 23 2016 17:23
what I'm doing is struct name opt(<..>)
Erik Hedvall
@Ogeon
Mar 23 2016 17:24
No, there is no on/off optional. Just 0 or more and one or more. You can sometimes use an additional pattern to achieve the same result
Pete Hayes
@petehayes102
Mar 23 2016 17:26
If you do that, you could have one match call another one (prefilling a default value) using the normal macro!() syntax. That makes it a bit more reusable.
Erik Hedvall
@Ogeon
Mar 23 2016 17:26
Exactly
Jeroen Bollen
@Binero
Mar 23 2016 20:43
Is there a short way to allocate a slice of uninitialised memory?
Erik Hedvall
@Ogeon
Mar 23 2016 21:08
There is an uninitialized function, but it's unsafe. Do you really need it to be uninitialized? Also, by "slice", do you mean array?
Peter Atashian
@retep998
Mar 23 2016 21:09
let x: [u8; 100] = unsafe { mem::uninitialized() };
although prefer mem::zeroed() if you can to avoid undefined behavior if something accidentally reads from it before you initialize it
Jeroen Bollen
@Binero
Mar 23 2016 21:10
I meant slice
Box<[T]>
thanks though
Peter Atashian
@retep998
Mar 23 2016 21:10
Oh, that, that's really tricky to get
Jeroen Bollen
@Binero
Mar 23 2016 21:11
I could use a Box of Options, but that'd create some redundancy as my program is already checking if the values exist somewhere else
Is there a way to convert a blob of memory to a slice?
Peter Atashian
@retep998
Mar 23 2016 21:12
Hmmm, you could probably use Vec::resize() (or Vec::set_len() if you really want unsafe uninitialized memory) and then do Vec::into_boxed_slice() to get a Box<T>
Jeroen Bollen
@Binero
Mar 23 2016 21:12
aha
that'd work
I was thinking of using pub unsafe fn allocate(size: usize, align: usize) -> *mut u8
but not sure how to get that into a slice, a vec will probably do
Erik Hedvall
@Ogeon
Mar 23 2016 21:13
The Vec is a safer bet.
Jeroen Bollen
@Binero
Mar 23 2016 21:19
Thanks
Oh, that stack allocated thoush
*though
Erik Hedvall
@Ogeon
Mar 23 2016 21:20
No, it's just from any pointer.
Jeroen Bollen
@Binero
Mar 23 2016 21:20
ah
so it's for when I already have data and I want to slice it
not the case here though
Erik Hedvall
@Ogeon
Mar 23 2016 21:21
Yep. Useful when dealing with ffi
...or when using allocate
Jeroen Bollen
@Binero
Mar 23 2016 21:22
ah right
can't use allocate though I noticed
I'm on stable rust
Erik Hedvall
@Ogeon
Mar 23 2016 21:23
Vec should be just fine.
Jeroen Bollen
@Binero
Mar 23 2016 21:24
Yeah using that
What is preferred as return value for a function that either returns a result or had a specific error
Peter Atashian
@retep998
Mar 23 2016 21:25
Result?
Jeroen Bollen
@Binero
Mar 23 2016 21:25
Result<T, ()> or Option<T> or Result<T, ErrorType>] where ErrorType is struct ErrorType;?
Erik Hedvall
@Ogeon
Mar 23 2016 21:26
Result<T, ErrorType> will let you combine it with other errors later in the pipe line.
Peter Atashian
@retep998
Mar 23 2016 21:26
If not returning the result is considered a failure, then definitely use Result, and having an actual error type in there, even if it is just a unit struct, is more descriptive than a tuple
Jeroen Bollen
@Binero
Mar 23 2016 21:26
I see
Even if there is only one possible error that can occur?
Peter Atashian
@retep998
Mar 23 2016 21:26
even then
Jeroen Bollen
@Binero
Mar 23 2016 21:26
Alright
Mohamed
@sangimed
Mar 23 2016 21:42
Hi
Jeroen Bollen
@Binero
Mar 23 2016 22:01
Heyo