These are chat archives for rust-lang/rust

24th
Apr 2019
Paul Delafosse
@oknozor
Apr 24 11:52
hello rust, I was wondering if there is a good way to align/format str for test
        let input = r#"= The message
this is a story that must be told
== Another title
"#;
i am doing it like so right now
but it is a bit annoying
would there be a way to indent the two last lines without adding whitepaces or tabulations?
Denis Lisov
@tanriol
Apr 24 12:02
One option is to use a non-raw string with explicit \n\ in the end
Another is to use the indoc! macro from the crate with the same name.
Ingvar Stepanyan
@RReverser
Apr 24 12:03
I've been using and found https://github.com/dtolnay/indoc useful for this
Ah too late :)
Paul Delafosse
@oknozor
Apr 24 12:03
:)
indoc seems to be what i was looking for. Thank you !
Asit Kumar Singh
@asitacko
Apr 24 12:31
Hello, I have created a static BtreeMap using lazy_static macro and inserted around 4k entries in that map. After adding that my rust compilation time has increased by 30 minutes. Any idea Why this might be happening? I was also wondering how does rust compilation works? Any help will be really appreciated.
Thanks
pub static ref PINCODES: BTreeMap<&'static str, Pincode> = { let mut pincode_map = BTreeMap::new(); pincode_map.insert( "700102", Pincode::new( Some("Kolkata (fe_unserviceable)".to_string()), None, None, None, Some(false), None, ), );
something like this
Ingvar Stepanyan
@RReverser
Apr 24 12:32
Well... if you're inserting all of them statically then it's not really surprising, it's generating quite a lot of code.
Asit Kumar Singh
@asitacko
Apr 24 12:32
So what actually happens there?
Denis Lisov
@tanriol
Apr 24 12:32
Are you saying that you've got some 4k lines like that pincode_map.insert(...)?
Ingvar Stepanyan
@RReverser
Apr 24 12:32
Nothing much, you just have lots of code?
Asit Kumar Singh
@asitacko
Apr 24 12:33
@tanriol yes
Denis Lisov
@tanriol
Apr 24 12:37
Well, 30 min is still too much IMO :-)
Denis Lisov
@tanriol
Apr 24 12:42
Don't know the specific structure of Pincode and it does not reproduce for () in my test.
Anyway, do you really want this to be a lazy_static BTreeMap?
Asit Kumar Singh
@asitacko
Apr 24 12:50
@tanriol I want it to be static BtreeMap.
Ingvar Stepanyan
@RReverser
Apr 24 12:51
it won't be really static though (unless you mean the lifetime)
as in, it's still created and populated in runtime
Paul Delafosse
@oknozor
Apr 24 12:53
re hello, I have another question regarding indoc! and compilation in general. Let's say that I use a specific crate (indoc) only in #[cfg(test)]does rustc ignore that "test" crate in release mode or do i have to specify explicitly I want that dependency only on debug target?
Ingvar Stepanyan
@RReverser
Apr 24 12:54
yeah #[cfg(test)] explicitly includes that code for compilation only in test mode
just like custom #[cfg(feature = ...)]
Paul Delafosse
@oknozor
Apr 24 12:54
so nothing special in Cargo.toml ?
Ingvar Stepanyan
@RReverser
Apr 24 12:54
yeah
Paul Delafosse
@oknozor
Apr 24 12:55
that's great !
Ingvar Stepanyan
@RReverser
Apr 24 12:55
as in, it won't even be checked for any errors aside from basic syntax - easy to check by putting some garbage inside and trying to compile
(this can bite sometimes :) )
Paul Delafosse
@oknozor
Apr 24 13:26
Also i am looking for some advice, I am writing a parser crate for asciidoctor targeting html. I am trying hard to not optimize early but I know I will soon try to make it "zero copy". Are you aware of good reading materials on that topic?
Denis Lisov
@tanriol
Apr 24 13:39
@asitacko My question was because I'd probably use a (sorted) array and binary search the needed value in it. Or, if that does not work, initialize the BTreeMap from an array.
Greg Wiley
@aztecrex
Apr 24 17:31

I am trying to decouple high-level logic from collaborator implementations for TDD in Rust. In this example, I want to defer the actual type of high-level errors to application wiring but the compiler errors with "overflow evaluating the requirement..." is it possible to help the compiler figure out how to terminate in this case? My workaround is to use an enum instead and just sum the collaborator error types but it would be great to figure out how to express the real intent.

pub trait Complicated {
    type ComplicatedError;
    fn perform_complicated(input: &str) -> Result<(), Self::ComplicatedError>;
}

pub struct ServiceAError {
    foo: String,
    bar: i32,
}

pub trait ServiceA {
    fn perform_service_a() -> Result<String, ServiceAError>;
}


pub struct ServiceBError(u128);

pub trait ServiceB {
    fn perform_service_b() -> Result<(), ServiceBError>;
}


impl<T> Complicated for T
where T: ServiceA + ServiceB,
      <T as Complicated>::ComplicatedError: From<ServiceAError> + From<ServiceBError> {
    fn perform_complicated(input: &str) -> Result<(), Self::ComplicatedError> {
        let a = ServiceA::perform_service_a()?;
        let b = ServiceB::perform_service_b(&a)?;
        Ok(())
    }
}

The compilation error:

error[E0275]: overflow evaluating the requirement `<Self as submit::Complicated>::ComplicatedError`
 --> src/submit.rs:5:1
  |
5 | / pub trait Complicated {
6 | |     type ComplicatedError;
7 | |     fn perform_complicated(input: &str) -> Result<(), Self::ComplicatedError>;
8 | | }
  | |_^
  |
  = note: required because of the requirements on the impl of `submit::Complicated` for `Self`
mandreyel
@mandreyel
Apr 24 17:32
hi all, how do I map cloned elements of a HashMap with an iterator? doing the usual iter.cloned().map(|(k, v)| { ... }).collect() gives me the following error: expected tuple, found reference
Peter Huene
@peterhuene
Apr 24 18:22
@mandreyel is the closure to map returning a key-value tuple?
Peter Huene
@peterhuene
Apr 24 18:37
oh, your error is likely from the cloned call. The iterator produced by cloned is expecting a reference rather than the tuple produced by HashMap's iterator. did you mean to clone the values, perhaps?
Peter Huene
@peterhuene
Apr 24 18:49
or just clone the key/value as-needed in the closure to map (i.e. without the cloned call)
Greg Wiley
@aztecrex
Apr 24 19:33
found that if I factor out a parallel trait, ComplicatedError that includes just the error type declaration, then slightly modify the type functions in the implementation to use the new trait, it works.
Greg Wiley
@aztecrex
Apr 24 19:40
well not quite, had to declare an error type parameter on Complicated. oh well, not the prettiest but no error and it is really easy to test
tsoernes
@tsoernes
Apr 24 19:50
Inside a map closure fn, what's the difference between |&mut mut x| and |&mut x|
and why do I need the former to do x *= 5?
Kelly Thomas Kline
@kellytk
Apr 24 23:25
I'm encountering the cargo build error "collision in the lockfile." In my scenario my Cargo.toml depends on two packages located locally via path, foo and bar. foo has its own dependency named bar, also located locally, which is different from the other bar. Using the package key feature doesn't appear to help here. How should this be solved?