These are chat archives for rust-lang/rust

15th
Jan 2019
thojest
@thojest
Jan 15 00:21
when returning a &'static str
does it mean that it lives forever?
Jonathan Reem
@reem
Jan 15 00:21
yes
thojest
@thojest
Jan 15 00:22
so when doing this frequently could i run out of RAM theoretically?
Ingvar Stepanyan
@RReverser
Jan 15 00:23
No, you're just returning a reference to existing data
Jonathan Reem
@reem
Jan 15 00:23
typically the only way to get a &'static str is to have a reference to a const/static
Ingvar Stepanyan
@RReverser
Jan 15 00:23
(commonly, but not necessarily, to just a constant that is already part of your executable)
thojest
@thojest
Jan 15 00:23
ahh ok i understand
Jonathan Reem
@reem
Jan 15 00:23
you can't allocate new ones at runtime without doing something like intentionally leaking a Box with unsafe
and then yes you would run out of memory over time
thojest
@thojest
Jan 15 00:24
Rust is really awesome
Jonathan Reem
@reem
Jan 15 00:24
yes!
Decentralize
@simwilso
Jan 15 06:24
Hi all. Does anyone know if there is a create that allows my app to launch a new (second) console window and print specific text in there?
Denis Lisov
@tanriol
Jan 15 07:59
@reem Note that intentionally leaking a Box does not require any unsafe
thojest
@thojest
Jan 15 10:42
hmm im having some problems with rustfmt
when i run it, it freezes and does nothing
is there some way to reset it, because it worked a few days ago
it's in a project where i cross compile to linux-musl
although running it on single files works
Farhan Ahmed
@IMacronaut_twitter
Jan 15 12:28
@toxicafunk What does "trait or the type is local to our crate” mean? Does bringing the type/trait in scope make it “local”? I am asking because when the trait (and the impl of it) are defined in the same file where the chrono::{DateTime, TimeZone, Utc} have been brought into scope, the trait methods are available on the DateTime<Utc> type.
toxicafunk
@toxicafunk
Jan 15 12:50
as I understand it, it means you created the type or trait
Zakarum
@omni-viral
Jan 15 12:50
@IMacronaut_twitter Usually bringing something is scope makes it "in scope" (or, generally speaking, same things are the same, different things are different)
Local is something that belongs "here"
Aren't you trying to create impl block for foreign (not local, defined in another crate) type?
Or implement foreign trait for foreign type
toxicafunk
@toxicafunk
Jan 15 12:57
so, iḿ trying to create a cassandra client using this library: https://github.com/AlexPikalov/cdrs/tree/master
the issue I have is iḿ using config-rs to obtain the node address from a config file
which means I need a &[&'static str]
I know how to return a Vec<String>or even Vec<str> from config-rs
but I don know how to create a slice of static str from it
anyone can suggest something?
Zakarum
@omni-viral
Jan 15 12:59
accepting &'static str usually indicates issue in API
Your only resort is unsafe
Or, better, fix API
toxicafunk
@toxicafunk
Jan 15 12:59
fn new<'b>(addr: &'b str, authenticator: A)
Zakarum
@omni-viral
Jan 15 13:00
fn new(addr: &str, authenticator: A)
Or fn new(addr: String, authenticator: A)
toxicafunk
@toxicafunk
Jan 15 13:00
mmm but its not my crate actually
Zakarum
@omni-viral
Jan 15 13:00
You can create PR
toxicafunk
@toxicafunk
Jan 15 13:01
yah, was thinking that
Zakarum
@omni-viral
Jan 15 13:01
That's how open source works ;)
toxicafunk
@toxicafunk
Jan 15 13:01
so Iḿ right to think thereś no way around this
Zakarum
@omni-viral
Jan 15 13:01
No way
toxicafunk
@toxicafunk
Jan 15 13:01
/// `NodeTcpConfigBuilder` constructor function. It receivesthread::spawn(move || { /// * node socket address as a string /// * authenticator pub fn new<'b>(addr: &'b str, authenticator: A) -> NodeTcpConfigBuilder<'b, A>
Zakarum
@omni-viral
Jan 15 13:01
&'static str can be created only from string literals
Or unsafe code
toxicafunk
@toxicafunk
Jan 15 13:01
i thought maybe I was missing something obvious
it doesn ask for static (I think) but thatś what it boils down to in prctice
or so I think
Zakarum
@omni-viral
Jan 15 13:02
Maybe you should stick with 'b
It looks like arbitrary lifetime to me, rather than 'static
Ofc NodeTcpConfigBuilder will live no longer than string
toxicafunk
@toxicafunk
Jan 15 13:02
in my program he compiler asked for static
but will try again before opening the PR
Zakarum
@omni-viral
Jan 15 13:03
Cause you try to create NodeTcpConfigBuilder<'static, A>?
toxicafunk
@toxicafunk
Jan 15 13:03
was trying something like this
let mut nodes: Vec<NodeTcpConfig<NoneAuthenticator>> = conf_nodes.iter().map(|node| { NodeTcpConfigBuilder::new(node, NoneAuthenticator {}).build() }).collect();
brb
Zakarum
@omni-viral
Jan 15 13:04
Are there docs available?
Found it
I see the flaw in API that can cause the issue
This method pub fn new<'b>(addr: &'b str, authenticator: A) -> NodeTcpConfigBuilder<'b, A>
should introduce new lifetime 'b, but use 'a instead
But I'm not sure if this is one that causes issue
At the end NodeTcpConfig will receive the addr bound to lifetime of string slice
The another potential cause is that compiler infers 'static for NodeTcpConfig you create
toxicafunk
@toxicafunk
Jan 15 13:11
Could I define a lifetime of my own then?
Zakarum
@omni-viral
Jan 15 13:11
On the function bounds
How do you use nodes?
Denis Lisov
@tanriol
Jan 15 13:12
@omni-viral That's usually not a great idea, but you should know that Box::leak is safe and can create 'static references by, well, leaking memory till the process death.
Zakarum
@omni-viral
Jan 15 13:13
@tanriol Oh, yeah, that's totally safe )
No jokes
toxicafunk
@toxicafunk
Jan 15 13:14
cluster_config = ClusterTcpConfig(nodes);
Zakarum
@omni-viral
Jan 15 13:14
But @toxicafunk just has issues with lifetimes, he certainly doesn't need 'static, I'm sure. Sort of.
Can you provide me whole function? :)
But using config-rs and, eventually, tokio threads
Zakarum
@omni-viral
Jan 15 13:15
No, your function exactly )
As is
toxicafunk
@toxicafunk
Jan 15 13:21
On mobile but will share it asap
toxicafunk
@toxicafunk
Jan 15 13:57
type CurrentSession = Session<RoundRobinSync<TcpConnectionPool<NoneAuthenticator>>>;

pub fn init(conf_nodes: &[&'static str]) -> Arc<CurrentSession> {
    let mut nodes: Vec<NodeTcpConfig<NoneAuthenticator>> = conf_nodes.iter().map(|node| {
        NodeTcpConfigBuilder::new(node, NoneAuthenticator {}).build()
    }).collect();
    let cluster_config = ClusterTcpConfig(nodes);
    Arc::new(new_session(&cluster_config, RoundRobinSync::new()).expect("session should be created"))
}
@omni-viral this is the function I want to call
from a main that lookes more or less like
let scylla_session = scylla::init(addrs.as_slice());
Zakarum
@omni-viral
Jan 15 13:59
And new_session?
toxicafunk
@toxicafunk
Jan 15 13:59
right after Arc::new
Zakarum
@omni-viral
Jan 15 13:59
No, the function itself
toxicafunk
@toxicafunk
Jan 15 13:59
ah
thatś from the library
1 sec
Zakarum
@omni-viral
Jan 15 14:00
link to docs then )
toxicafunk
@toxicafunk
Jan 15 14:00
/// Creates new session that will perform queries without any compression. `Compression` type
/// can be changed at any time.
/// As a parameter it takes:
/// * cluster config
/// * load balancing strategy (cannot be changed during `Session` life time).
pub fn new<A, LB>(
  node_configs: &ClusterTcpConfig<'static, A>,
  mut load_balancing: LB,
) -> error::Result<Session<LB>>
where
  A: Authenticator + 'static + Sized,
  LB: LoadBalancingStrategy<TcpConnectionPool<A>> + Sized,
{
Zakarum
@omni-viral
Jan 15 14:00
ClusterTcpConfig<'static, A> here is your static
It comes from this type
toxicafunk
@toxicafunk
Jan 15 14:00
yup, I knew it wasn't me :)
so, as I imagine, in practice 'b is static
Swarnim Arun
@swarnimarun
Jan 15 14:03
Guys I was having some problems with the nightly install with rustup on my linux on 11th January morning. So with the the 10th Jan build, I had to specifically install 09th Jan version. But now it seems to be working fine.
Any ideas why that could have happened.
Tim Robinson
@1tgr
Jan 15 14:04
I think this is new_session
toxicafunk
@toxicafunk
Jan 15 14:04
@1tgr exactly
Tim Robinson
@1tgr
Jan 15 14:04
I don't see how it's possible to use this code
Without hardcoding the config strings in your program
toxicafunk
@toxicafunk
Jan 15 14:04
exactly: "127.0.0.1:9042"
Tim Robinson
@1tgr
Jan 15 14:06

Indeed it does work, but that actually means that one should recompile the whole project after editing configuration file :)

If you might be interested I'd suggest to change the addr type in config_tcp.rs from &'a str to String or maybe something more generic, for example, T: AsRef<str>. That shouldn't break your existing code and examples but should solve the problem with static-only parameters.

toxicafunk
@toxicafunk
Jan 15 14:10
I didn't even know include_str! was a thing
however I did know (and used) lazy_static!
Tim Robinson
@1tgr
Jan 15 14:16
Doesn't exactly help if you want to pass the config on the command line
toxicafunk
@toxicafunk
Jan 15 14:17
well, i had set it up so that I pass the env argument and I can choose which config file to load
but that means u load even files that u won't use :S
toxicafunk
@toxicafunk
Jan 15 15:05
is Arc::clone(&session) equivalent to session.clone()?
is one to be preferred over the other?
Ingvar Stepanyan
@RReverser
Jan 15 15:06

The Arc::clone(&from) syntax is the most idiomatic because it conveys more explicitly the meaning of the code. In the example above, this syntax makes it easier to see that this code is creating a new reference rather than copying the whole content of foo.

(c) https://doc.rust-lang.org/std/sync/struct.Arc.html

toxicafunk
@toxicafunk
Jan 15 15:07
thx! Should have checked the docs before asking, my bad
Michal 'vorner' Vaner
@vorner
Jan 15 15:08
It is not always equivalent. The session.clone() may call the Session::clone instead of Arc::clone, depending on the type you want. Eg let x: Arc<_> = session.clone() is cloning the arc, while let x: Session = session.clone() will clone the Session inside.
Ingvar Stepanyan
@RReverser
Jan 15 15:08
well he asked specifically about Arc::clone(&session) which already assumes that session is an Arc
Michal 'vorner' Vaner
@vorner
Jan 15 15:13
Yes, it's Arc<Session>. My claim still stands. That's auto-deref in play. Because there's no clone(&self) -> Session on Arc, it'll try to deref the arc and try again.
Ingvar Stepanyan
@RReverser
Jan 15 15:15
You mean in let x: Session = session.clone() where session is an Arc?
No it won't, it will just fail the typecheck
Michal 'vorner' Vaner
@vorner
Jan 15 15:15
I think I've seen it work.
Ingvar Stepanyan
@RReverser
Jan 15 15:15
Deref is deterministic and doesn't depend on target type when calling methods.
thojest
@thojest
Jan 15 20:46
hmmm no constantinople :(
oh wrong gitter channel sry