These are chat archives for rust-lang/rust

2nd
Oct 2018
Ash
@ashthespy
Oct 02 2018 13:10
Is there a way to do something like this without creating an abstract wrapper struct for fruit?
let fruit = if some_test {
            apple::get(args).unwrap()
        } else {
            banana::get(arg).unwrap()
        };
Denis Lisov
@tanriol
Oct 02 2018 13:12
Either creating an enum or using a trait object if appropriate. What do these have in common?
Ash
@ashthespy
Oct 02 2018 13:15
banana has a subset of the fields of apple
enum is a simple solution!
H M
@HM64259860_twitter
Oct 02 2018 13:45
Hi, I want to specify that a generic parameter to a function implements as_ref. I have tried but it's not working, any ideas?
https://play.rust-lang.org/?gist=a3ef9f47fe6a4af72b642e0f09ed7195&version=stable&mode=debug&edition=2015
Lyle Mantooth
@IslandUsurper
Oct 02 2018 13:50
AsRef takes a type agument, like AsRef<str>. For this code, maybe AsRef<[u8]>?
I am NOT sure of the specific syntax
Lyle Mantooth
@IslandUsurper
Oct 02 2018 13:59
Then again, iter() usually provides Option<&T>, so do you really need AsRef, @HM64259860_twitter ?
H M
@HM64259860_twitter
Oct 02 2018 14:02
dunno, the code worked exactly like it is when it was in the main body... i was repeating the same code over and over, so i thought i could put it into a function... but it seems that is not so simple.
You may be able to replace all this with .clone().as_ptr().
red75prime
@red75prime
Oct 02 2018 14:05
This function must be marked as unsafe.
H M
@HM64259860_twitter
Oct 02 2018 14:07
thanks
i am going to try that
red75prime
@red75prime
Oct 02 2018 14:11
Using just a pointer (c_ptr) without the size of the buffer it points to is a sure way to get buffer overruns though.
H M
@HM64259860_twitter
Oct 02 2018 14:13
I will allocate the pointer appropriately in C
H M
@HM64259860_twitter
Oct 02 2018 14:19
for some reason it insists on i8, not u8
well, it needs to copy u8 bytes... i'm gonna go back to pasting the code everywhere.
Lyle Mantooth
@IslandUsurper
Oct 02 2018 14:21
@HM64259860_twitter , there is a c_char, and a c_uchar in Rust.
H M
@HM64259860_twitter
Oct 02 2018 14:23
really, wow, so c_char is a signed character - that's special
Lyle Mantooth
@IslandUsurper
Oct 02 2018 14:23
Just found it by searching c_char on the docs, and saw c_uchar in the results.
IIRC, C integers always behave the same whether you mean to interpret them as signed or not.
H M
@HM64259860_twitter
Oct 02 2018 14:26
ok, got it to compile :)
Lyle Mantooth
@IslandUsurper
Oct 02 2018 14:27
And compiling is half the battle!
JurassicRobot
@JurassicRobot
Oct 02 2018 14:54
Q about custom error types - is there any reason to impl Error now that description() is soft deprecated, if there is no underlying cause()?
Michal 'vorner' Vaner
@vorner
Oct 02 2018 15:08
@JurassicRobot Yes, there is. You mark the type that it is an error and things like Failure (or other errors, as their cause) can use it. If you didn't, they would have hard time referring to you as their cause.
JurassicRobot
@JurassicRobot
Oct 02 2018 15:43
so just an 'empty' impl will do? impl Error for MyError {}
cubetastic
@cubetastic33
Oct 02 2018 16:33
Is it possible to read the contents of a webpage at localhost:5050/foo using only tokio?
Kelly Thomas Kline
@kellytk
Oct 02 2018 20:52
Am I reading https://github.com/SimonSapin/rust-std-candidates/blob/df2edec638128c35a4cf55aac73314ef2be03bf4/matches/lib.rs correctly that debug_assert_matches does not support multiple items for its pattern? For example, there's no way to declare the equivalent of debug_assert_matches!(oddenum, Myenum::V1 || Myenum::V3)
Diggory Blake
@Diggsey
Oct 02 2018 21:12
how can a type ever be Sync but not RefUnwindSafe?
Vitaly
@Virtuos86
Oct 02 2018 22:37
Because these are different traits?
Diggory Blake
@Diggsey
Oct 02 2018 22:37
but if you are Sync you must inherently be RefUnwindSafe
because you could be shared between two threads and one thread could panic whilst the other accesses it
James McCoy
@jamessan
Oct 02 2018 23:26
@HM64259860_twitter char has no specific sign in C. It's up to the platform to decide whether it's signed or unsigned. Rust's c_char follows that same behavior. If you need a specific sign, then use c_schar or c_uchar.