These are chat archives for rust-lang/rust

23rd
May 2018
Kelly Thomas Kline
@kellytk
May 23 2018 00:00
Rather than configuring Cargo to build projects into different directories than target, is it considered better practice to build into default directories then move build products out with post-build scripts?
Farzeen
@thefzsalam
May 23 2018 02:57
What does pub trait .... for ... {..} mean?
pub unsafe trait IsA<T: StaticType + UnsafeFrom<ObjectRef> + Wrapper>: StaticType + Wrapper +
    Into<ObjectRef> + UnsafeFrom<ObjectRef> +
    for<'a> ToGlibPtr<'a, *mut <T as Wrapper>::GlibType> { }
apiraino
@apiraino
May 23 2018 11:30
Not strictly related to coding, but this weekend there is the Rustfest in Paris. I'll stay for the Impl days too toying myself with some "good first issues" (for newbies like me). If anyone wants to join me for some pair programming, I would be more fun!
Farzeen
@thefzsalam
May 23 2018 13:13
Is there any way to run some code before main(), if a crate is included, to initialize the crate?
For example, registering GObject types a crate provides
Denis Lisov
@tanriol
May 23 2018 13:15
@thefzsalam No way, AFAIK. Make an init function for your user to call.
Dylan DPC
@Dylan-DPC
May 23 2018 13:43
put your main in another function and make your wrapper as the main()
@thefzsalam
Farzeen
@thefzsalam
May 23 2018 14:11

I want to make a GObject wrapper around Box<T>.
What I have to do is the following:

#[repr(C)]
struct BoxWrapperGObject<T> {
    parent: gobject_ffi::GObject,
    value: Box<T>
}
// plus a bunch of other functions as boilerplate code.

But this generates a different version of BoxWrapperGObject for different T's I use.
Is there anyway to avoid this and generate a single version of struct BoxWrapperGObject, regardless of type parameter T?
Something like type erasure in Java's generics?

Isaac Woods
@IsaacWoods
May 23 2018 14:15
Use value: Box<Any> and get rid of the type parameter
Then you can downcast it at runtime
Farzeen
@thefzsalam
May 23 2018 14:20
So, losing compile time type checks are inevitable?
ozgurakkurt
@ozgurakkurt
May 23 2018 14:20

hi, I wrote this to make a match statement shorter.

if e.code().is_some() && e.code().unwrap().code() == "23503"

will this code ever panic?

Farzeen
@thefzsalam
May 23 2018 14:21
@ozgurakkurt, Not when e.code() returns None. Because if statements have a property called shortcircuiting.
ozgurakkurt
@ozgurakkurt
May 23 2018 14:22
@thefzsalam, Ok thanks
Farzeen
@thefzsalam
May 23 2018 14:23
No probs :)
Also have a look at https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or
@ozgurakkurt
Denis Lisov
@tanriol
May 23 2018 14:55
@thefzsalam If you need only a small number of different types, you may choose to use an enum
Farzeen
@thefzsalam
May 23 2018 14:55
That's not an option :(
Denis Lisov
@tanriol
May 23 2018 14:56
What are you trying to achieve?
Farzeen
@thefzsalam
May 23 2018 15:11
@tanriol, while trying to type out what I was trying to achieve, I realised the inherent flow in my design.
Thanks for asking :)
ozgurakkurt
@ozgurakkurt
May 23 2018 15:34

hi again, I am getting this error when trying to make a std str. using std::str::from_utf8(). This is the error:

Utf8Error { valid_up_to: 1, error_len: Some(1) }
the value I give the function is:
[76, 221, 39, 211, 61, 41, 156, 120, 185, 155, 159, 101, 131, 166, 210, 12, 213, 149, 0, 128, 165, 43, 86, 223, 168, 243, 45, 107, 161, 149, 82, 47]
this value is coming from a sha256 hash function.
I dont get why this happens. Isn't a hash valid utf8?

sorry for markdown
Krys
@Krysme
May 23 2018 15:36
Not necessarily
ozgurakkurt
@ozgurakkurt
May 23 2018 15:36
do you know a way of converting it to a string?
Krys
@Krysme
May 23 2018 15:37
Well a string has to contain valid utf8 values
For binary data it might or might not be valid utf8 values
Dealing with a string of random bytes,you might prefer u8 slices instead
ozgurakkurt
@ozgurakkurt
May 23 2018 15:46
I hashed the same value using a website. This is the hash:
"4CDD27D33D299C78B99B9F6583A6D20CD5950080A52B56DFA8F32D6BA195522F"
I dont get why this wouldn't be valid utf8
@Krysme Thanks I will use u8 slices than
Krys
@Krysme
May 23 2018 15:50
Oh,its in base16 format
But in the previous message the 2nd element is 221
James McCoy
@jamessan
May 23 2018 15:52
@ozgurakkurt Arbitrary bytes aren't valid utf-8. Formatting the bytes as hex characters is different than trying to display the raw values as a string.
Krys
@Krysme
May 23 2018 15:52
Indeed
ozgurakkurt
@ozgurakkurt
May 23 2018 15:54
I want to save it to a database. I have to convert it to a string to do it. I am using rust-postgres for connecting to the database and sha2 for hashing.
Ok I can save it as another type. Thanks for help.
tandrysyawaludin
@tandrysyawaludin
May 23 2018 16:00
@berkus That is amazing, thanks for the solution
Krys
@Krysme
May 23 2018 16:11
You might want to hex it first and then convert it into a str
ozgurakkurt
@ozgurakkurt
May 23 2018 16:30
I saved it as BYTEA (Vec<u8>) to postgres it is working now so no problem
estk
@estk
May 23 2018 21:32
Hi there, I've been struggling with a strange question about rust. In rust, is it possible to write a function that can either take a value OR a reference depending on what is passed in?
Ryan
@rnleach
May 23 2018 21:35
@estk AsRef might be a good place to start.
estk
@estk
May 23 2018 21:37
@rnleach thanks for that tip!
I was actually just looking at that, my next question is, is it possible to use that with self in a method signature?
Dmitriy
@dpogretskiy
May 23 2018 21:44
it most probably is a valid &self
estk
@estk
May 23 2018 21:48
@dpogretskiy Thanks, yeah all I'm really after is &self, but the problem is that there are cases where I want that method to take ownership of self such that I dont run into the 'x does not live long enough' error.
Denis Lisov
@tanriol
May 23 2018 21:49
@estk Could you describe your use case in more detail?
estk
@estk
May 23 2018 21:50
@tanriol yes absolutely, I'll create a play.rust-lang, I apologize that the code I have is not really a minimum example
Gist: https://gist.github.com/rust-play/dbd9bd26a4c0dd7d063777af2e2f5198
error[E0597]: borrowed value does not live long enough
   --> src/main.rs:116:5
    |
116 |     Dialer::default().dial(host, port)
    |     ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
117 | }
    | - temporary value only lives until here
    |
    = note: borrowed value must be valid for the static lifetime...

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
error: Could not compile `dial`.

To learn more, run the command again with --verbose.
I can fix this error quite easily by just consuming self, but I really would like to allow both use cases.
estk
@estk
May 23 2018 21:56
BTW, by moving the method dial into a standalone func and using AsRef it works great, just not the ergonomics I'm looking for.
Denis Lisov
@tanriol
May 23 2018 22:02
The Dialer struct contains only settings, not state, correct? Do you need the ability to change them after calling dial?
estk
@estk
May 23 2018 22:04
@tanriol you're correct, just settings
Denis Lisov
@tanriol
May 23 2018 22:04
If correct and no need, I'd suggest to take self and add #[derive(Clone, Copy)] for the Dialer.
By the way, usually it's a good idea to derive Debug too...
estk
@estk
May 23 2018 22:05
Huh, not something I had considered, is it not bad form to just clone all over the place?
Denis Lisov
@tanriol
May 23 2018 22:08
For an 12-byte struct? Well, unless the code is extremely hot that's not going to be a problem (a reference is already 8 bytes for a 64-bit system). And I'm pretty sure LLVM will optimize it without any problems :-)
If you derive Copy, you don't even need any explicit clone calls.
estk
@estk
May 23 2018 22:09
haha, I guess I'm wondering from a learning standpoint what one would do if it wasnt a super-tiny-never-changing struct :)
Denis Lisov
@tanriol
May 23 2018 22:12
The straightforward way is to have an Arc<Mutex<MyStruct>> and clone the ark in some points :-)
Speaking about your particular question -- take either self or &self -- it's possible to have the operation in a trait that's implemented for both types.
There's also the ToOwned trait for some cases...
estk
@estk
May 23 2018 22:15
interesting, I assume in practice, its just not worth it in most cases?
Denis Lisov
@tanriol
May 23 2018 22:17
Depends on your constraints and requirements... I have no experience with high-throughput systems, but there are some specific tricks they may need.
But usually I don't worry too much about performance until it proves insufficient.
estk
@estk
May 23 2018 22:43
Yeah makes sense, thanks so much for your help, i really appreciate it!