These are chat archives for rust-lang/rust

5th
Feb 2018
blank-dan
@blank-dan
Feb 05 2018 00:41
std::env::current_exe() seems to be.
Ayose Cazorla
@ayosec
Feb 05 2018 02:42
In a derive macro, is it possible to get the path of the file where the #[derive(...)] is expanded?
Kelly Thomas Kline
@kellytk
Feb 05 2018 03:05
Thanks @blank-dan
Kelly Thomas Kline
@kellytk
Feb 05 2018 10:03
In the implementation of from_str from FromStr, can the instance of the struct it's being implemented on be accessed from within from_str?
Michal 'vorner' Vaner
@vorner
Feb 05 2018 10:10
I'm not sure I understand you correctly, but if so, the answer is… sort of. You are creating a new instance there. After you create it, you can access it as you like. But until then, there's no instance to access.
Kelly Thomas Kline
@kellytk
Feb 05 2018 10:15
Pardon me @vorner I asked the wrong question. Can an additional argument be passed to from_str? The reason I'm asking is because within that function I'd like to know something about the calling context
Michal 'vorner' Vaner
@vorner
Feb 05 2018 10:15
No. You have to create your own trait for that.
The idea about traits is that they have all the same interface.
Denis Lisov
@tanriol
Feb 05 2018 10:16
Or your own wrapper with a different impl.
Kelly Thomas Kline
@kellytk
Feb 05 2018 10:18
@tanriol What I want is for from_str to generate progress messages to a logger. Would a better solution than reimplementing FromStr be to set the logger as a static or some kind of global?
Kelly Thomas Kline
@kellytk
Feb 05 2018 10:43
A static in main.rs and used in modules is a very straightforward solution
Zakarum
@omni-viral
Feb 05 2018 10:50
Is it valid to transmute &[*const T] to &[&T] if l'm sure those Ts are alive?
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:13
Apparently with a static struct I cannot modify its field's values without declaring the static with mut and wrapping the field value setting code in unsafe, which I would rather avoid. Is there another, better solution?
Denis Lisov
@tanriol
Feb 05 2018 11:26
I'd probably not use from_str in this case at all.
Zakarum
@omni-viral
Feb 05 2018 11:34
@kellytk Do you need to modify it regulary? What about multithreading?
If you only access it from single thread - it's ok. just use unsafe
If not -> put it into Mutex
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:36
@omni-viral Yes a field of the struct will be frequently updated. Initially the program is single-threaded although I'd prefer to use good practices nevertheless
I'll read about Mutex thanks
Zakarum
@omni-viral
Feb 05 2018 11:37
Also you may look at RwLock if you mostly read that field
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:39
@omni-viral And BTW, the Mutex recommendation is for the case when I would like to make a static mut without use of unsafe, correct?
Zakarum
@omni-viral
Feb 05 2018 11:39
Correct. You even don't need mut with Mutex
Multiple thread accessing through unsafe may cause race condition. So if you plan to go multithreaded then use Mutex or RwLock unless you know what you are doing :smile:
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:41
@omni-viral That's interesting. What I find appealing about the static solution is that, despite being a global, it becomes available through my entire project through use and it doesn't have to clutter up function signatures to get passed around manually. What do you think?
Zakarum
@omni-viral
Feb 05 2018 11:42
Tipically use of statics are discouraged
unless it's static constant
And the fact it doesn't appear in function signatures is one of the reasons
Michal 'vorner' Vaner
@vorner
Feb 05 2018 11:43
Well, it depends. There are things that you consider a global resource. I often place a thread pool as a global in lazy_static ‒ simply because it is a kind of singleton-ish thing.
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:44
Static const would be fine. I intend to use this logger for the life of a run of the program
Zakarum
@omni-viral
Feb 05 2018 11:44
static const imply that you don't change it
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:45
The struct instance assigned to the static wouldn't change, but the value of one of its fields would. Does that disqualify it from being const?
Zakarum
@omni-viral
Feb 05 2018 11:45
yes
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:45
Oh
Zakarum
@omni-viral
Feb 05 2018 11:46
You can do it. But I wouldn't recommend it
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:46
The problem with passing around the logger, and not using a static, is that as this FromStr trait showed me, I expect I'll have plenty of my code called implicitly for me wherein I'll have no opportunity to pass the logger, as I would if I were explicitly calling. What do you think?
Zakarum
@omni-viral
Feb 05 2018 11:47
Especially if you write a library.
If you write an application you can afford a bit more dirty hacks :smile:
Michal 'vorner' Vaner
@vorner
Feb 05 2018 11:47
If nothing else, it's confusing. Something like my_string: u32.
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:47
Pardon?
Michal 'vorner' Vaner
@vorner
Feb 05 2018 11:47
I mean, having a const that changes is confusing ‒ similar to the above code.
Zakarum
@omni-viral
Feb 05 2018 11:47
@kellytk check for log crate BTW
Michal 'vorner' Vaner
@vorner
Feb 05 2018 11:47
You don't expect that.
Zakarum
@omni-viral
Feb 05 2018 11:48
log provides exactly that. static access point to logger
Kelly Thomas Kline
@kellytk
Feb 05 2018 11:48
@vorner A static struct with a field value that gets changed is ok though correct?
Michal 'vorner' Vaner
@vorner
Feb 05 2018 11:55
It at least doesn't outright lie about what it is.
Romeo Disca
@thinkrapido
Feb 05 2018 18:59
Hi, how can I make
#[derive(Debug, PartialEq, Eq)] pub enum PropertyEnum { Float(f32), }
work
Hunar Roop Kahlon
@kinggoesgaming
Feb 05 2018 19:06
@thinkrapido you can clarify your question?
Alexandre Bury
@gyscos
Feb 05 2018 19:14
Have derived trait work though every single-tuple enum values?
Not sure it's supported in the standard library
Erich Cordoba
@ericho
Feb 05 2018 19:59
Does anyone knows where can I find an example on how to download a file from a url? in this case a PNG file. I'm seeing reqwest, but I only see how to get the body of a web page.
Erich Cordoba
@ericho
Feb 05 2018 20:25
I solved with curl crate :smile: