These are chat archives for rust-lang/rust

5th
Mar 2019
Ingvar Stepanyan
@RReverser
Mar 05 00:05
@thymbahutymba I still don't understand why do you want to call a function by name. Your sample doesn't seem to do that.
Why can't you use name of the function as a regular identifier rather than a string?
David O'Connor
@David-OConnor
Mar 05 00:56
Hey guys. What's the best way to deal with the error "unknown character escape:" ? Not trying to escape anything, just have some backslashes. Is prepending the quotes with r the correct approach?
Ichoran
@Ichoran
Mar 05 01:41
That's an easy way, yeah.
You can also escape the slash with \\.
David O'Connor
@David-OConnor
Mar 05 01:48
thank you!
mmynsted
@mmynsted
Mar 05 04:37
Hello
I am trying to derive Eq on a struct that contains an Option<f32>. The compile complains that Eq is not implement for Option<f32> but that I also may not implement it because they (Option and f32 ) are defined outside my crate.
I understand why, just not how to get around this.
mmynsted
@mmynsted
Mar 05 04:43
Hmm maybe rug is the answer.
mmynsted
@mmynsted
Mar 05 05:06
An empty Eq implementation for the whole struct worked fine.
Zakarum
@omni-viral
Mar 05 06:26
@mmynsted the Eq is marker trait that ensures some properties of comparison method defined for PartialEq
Namely that a == a is always true
f32 doesn't satisfy it since f32::nan() != f32::nan()
mmynsted
@mmynsted
Mar 05 06:27
yes
For my needs I do not need to compare the f32 at all. It was simply part of the struct.
Zakarum
@omni-viral
Mar 05 06:28
So you don't derive PartialEq?
mmynsted
@mmynsted
Mar 05 06:28
I did
It requires Eq
Zakarum
@omni-viral
Mar 05 06:28
Then you do compare f32
mmynsted
@mmynsted
Mar 05 06:28
so I just implement Eq as emapy
empty
no I compare for partial equality and partial ordering and that field is not significant
Zakarum
@omni-viral
Mar 05 06:29
Then you should implement PartialEq manually
mmynsted
@mmynsted
Mar 05 06:29
I do
Zakarum
@omni-viral
Mar 05 06:29
@omni-viral
So you don't derive PartialEq?
@mmynsted
I did
mmynsted
@mmynsted
Mar 05 06:30
Ooops. Yes. I do not derive PartialEq I manually implement it.
Zakarum
@omni-viral
Mar 05 06:30
Ah, Ok then )
mmynsted
@mmynsted
Mar 05 06:32
Heh now I have another issue. I have a std::time::Duration that I need to compare with an f32. Duration can be captured as seconds but the seconds are u64. (None of the Durations) are very large at all.
But I can not think of a nice way to convert u64 to an f32 (so long as it fits)
mmynsted @mmynsted facepalm
mmynsted
@mmynsted
Mar 05 06:40
as f32 works fine even for u64::max_value(); for what I need.
mmynsted
@mmynsted
Mar 05 06:48
guess it is time for sleep
thank you for help
Zakarum
@omni-viral
Mar 05 07:38
You can pick a precision and convert duration to f32
For example use duration.as_millis() as f32 * 1e-3
Just don't compare floating point numbers for equality )
uncotion
@uncotion
Mar 05 08:56

I’m working on a program that must have an async tcpstream, I don’t know how I must handle streams, because handshake negotiations are uncertain.

for example for handling a tcpstream, I’m using many stream combinators.

.and_then(|tcpstream|{…})
.and_then(|tcpstream|{…})
.and_then(|tcpstream|{…})
….
.and_then(|tcpstream|{…})
.and_then(|tcpstream|{…})
.and_then(|tcpstream|{…})

I know it’s not the correct answer, Could please someone give me a hint?

Richard Wiedenhöft
@Richard-W
Mar 05 09:20
What is the canonical way to define a conversion between two types that are not part of the crate
Implementing Into/From does not work of course. Right now I am using a dedicated function, but it seems not very "rusty"
Zakarum
@omni-viral
Mar 05 10:18

canonical way to define a conversion between two types that are not part of the crate

Is to define in the crate they belong

Otherwise, if types are from different unrelated crates - a dedicated function
Functions are very rusty
If they are made of iron
And were exposed to moisture for long time
Richard Wiedenhöft
@Richard-W
Mar 05 10:21
true :)
Ichoran
@Ichoran
Mar 05 15:21
@mmynsted - You do not generally want to convert durations to f32 because you will lose a ton of precision.
If that is what you want, you should have a duration-normalizer that rounds all durations to the nearest f32 representation, so as to avoid calling some things equal in one place, and then calculating that they are not equal somewhere else, causing confusion.
Ichoran
@Ichoran
Mar 05 15:29
For instance, at the limit of u64, the closest times you can represent are about 35 years apart (instead of one millisecond apart).
Right now, the closest epoch times you can represent are about two minutes apart. So that's pretty bad.
So even if you just mean to use it with small durations, like a couple hours, you'll have to be careful with the API to avoid "misusing" it to calculate longer times. Better just to use something where that's less likely to be an issue (e.g. f64 is still pretty okay).
mmynsted
@mmynsted
Mar 05 17:39
In my case the precision necessary is 30 min. The max duration would be less than 200 hours.
matrixbot
@matrixbot
Mar 05 17:40
bogglez so would you be ok with 29 minutes being rounded down to 0?
mmynsted
@mmynsted
Mar 05 17:40
Really now that I am awake, I question why I used Duration at all. It is not providing value, only encouraging misuse of numeric conversions.
yes
mmynsted
@mmynsted
Mar 05 17:49
Originally I was going to use rug but my precision is so low its seems silly. I think I need to re-factor later. Use something like rug and avoid Duration.
Part of the point is to lean Rust
:-)
At least with rug the precision would be intentional, clear, and flexible (in case it needed to change in the future).
Sylwester Rąpała
@xoac
Mar 05 17:56
Do you know any template engine for rust? I mean I would like to generate some common files for test etc without doubling information. For example this looks like a good start but for example no tests for Windows
Ingvar Stepanyan
@RReverser
Mar 05 17:58
If this is meant for bootstrapping projects (judging by your link), you might want to look into https://github.com/ashleygwilliams/cargo-generate
Sylwester Rąpała
@xoac
Mar 05 18:05
it's look interesting :)
Ingvar Stepanyan
@RReverser
Mar 05 18:06
that's what official wasm project templates are using
mmynsted
@mmynsted
Mar 05 18:11
Is there a way to get the field names of a struct programmatically?
I guess a macro
Ingvar Stepanyan
@RReverser
Mar 05 18:16
Yeah, a macro or a derive
I'm pretty sure there is already a derive for this somewhere
mmynsted
@mmynsted
Mar 05 18:17
Hmm not sure how to google for a derive, but that would be ideal
Ingvar Stepanyan
@RReverser
Mar 05 18:17
search on crates.io for "derive ..." I guess
mmynsted
@mmynsted
Mar 05 18:18
Wow. Perfect. Thank you
Ingvar Stepanyan
@RReverser
Mar 05 18:18
Sure, np.