People
Repo info
Activity
    Ichoran
    @Ichoran
    @David-OConnor - Anyway, either crate is cool.
    Zakarum
    @omni-viral
    Traits from multiple crates implemented for Either is objectively good thing
    Which isn't the case, because who would add new dependency just to implement his traits for Either?
    Ichoran
    @Ichoran
    Yeah, that's a good point. Okay, you're probably right: would be better if Either was in std.
    Riley Cat
    @WreckedAvent
    I don't really see a point in having both Either and Result since they are effectively the same type, just one has variants that you have to memorize as to what it means and the other has obvious variants
    anyway @David-OConnor I'm not sure what your use case is exactly for returning either a single element or a vec of them, but you can just wrap the single element in a vec and check if there's just one element or create an enum that better describes your domain
    I would not suggest using an Either here since by convention one "side" is used for errors
    ambiso
    @ambiso

    Hello!

    I have a tiny question about the tokio library.

    I'm trying to find a more idiomatic way to do the following:

                    let msg: Result<WsMsg, WebSocketError> = serde_json::from_str(&msg)
                        .map_err(|e| ResponseError("The server has replied with an unknown message"));
                    if msg.is_err() {
                        return err(msg.unwrap_err());
                    }
                    let msg = msg.unwrap();

    I've tried using the ? operator on the FuturesResult returned by err, but for some reason FuturesResult doesn't implement the Try trait.

    Zakarum
    @omni-viral

    @WreckedAvent

    one has variants that you have to memorize as to what it means and the other has obvious variants

    Exactly, and sometimes you need other variants

    i.e. sometimes it is not success and error
    Victor Lopes
    @vlopes11

    Hello Rustaceans! To implement Clone for a Box of a trait instance with a specific lifetime, what's the best practice? Example:

    trait SomeTrait: Clone + Sized {
        fn some_method(&self) -> &bool;
    }
    
    #[derive(Clone)]
    struct SomeStruct {
        pub val: bool,
    }
    impl SomeTrait for SomeStruct {
        fn some_method(&self) -> &bool {
            &self.val
        }
    }
    
    struct OtherStruct<'a> {
        pub some_box: Box<'a + SomeTrait>,
    }
    
    fn main() {}

    This code will produce this:

    error[E0038]: the trait `SomeTrait` cannot be made into an object
      --> src/main.rs:16:5
       |
    16 |     pub some_box: Box<'a + SomeTrait>,
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SomeTrait` cannot be made into an object
       |
       = note: the trait cannot require that `Self : Sized`
    Ingvar Stepanyan
    @RReverser
    the error message says that your trait cannot be used with dynamic dispatch, it's not strictly related to cloning
    Riley Cat
    @WreckedAvent
    @ambiso the more idiomatic approach would be to use a combinator like and_then
    Ingvar Stepanyan
    @RReverser
    what exactly are you trying to do? do you actually want dynamic dispatch inside of OtherStruct or can you make it generic?
    Victor Lopes
    @vlopes11
    I need a dynamic dispatch without 'static lifetime
    Maybe an usage example is a Vec with different implementations of SomeTrait?
    Ingvar Stepanyan
    @RReverser
    I see. And by cloning you mean you want to actually be able to clone data behind the trait?
    If you remove : Clone + Sized bound, your code will compile as expected, but I'm not sure if you need it too...
    Victor Lopes
    @vlopes11
    Understood. And impl Clone for Box<SomeTrait>?
    Ingvar Stepanyan
    @RReverser
    no, I don't think that will work because both trait Clone and the type Box are not in your crate
    one thing you can do is add custom clone method to your trait
    and use that
    Victor Lopes
    @vlopes11
    Let me test
    Ingvar Stepanyan
    @RReverser
    but you can't combine the real Clone with dynamically dispatched trait
    why do you want to clone dynamic object though?
    Victor Lopes
    @vlopes11
    For example:
    trait SomeTrait {}
    
    struct Foo {}
    impl SomeTrait for Foo {}
    struct Bar {}
    impl SomeTrait for Bar {}
    
    struct SomeStruct<'a> {
        some_attr: Vec<Box<'a + SomeTrait>>,
    }
    
    impl<'a> SomeStruct<'a> {
        pub fn some_method(&self) -> Vec<Box<SomeTrait>> {
            self.some_attr.clone();
        }
    }
    Ingvar Stepanyan
    @RReverser
    Hmm... maybe better not to clone that data? It's likely to be pretty expensive
    I'd instead return &[Box<SomeTrait>] and use it where possible
    Victor Lopes
    @vlopes11
    Hmm but in case we really need to implement Clone for SomeStruct, we don't have options?
    Ingvar Stepanyan
    @RReverser
    There are but they're tricky
    As I said, one option is to have custom method that would clone anything into such box, and then you can make cloning to work...
    And you could probably even separate it out into its own base trait and provide blanket implementation to avoid reimplementing this manually...
    Let me try something
    So something like this is not pretty, but it works, even with derive(Clone): https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4e96609149bce63ae5ad428db14413c3
    Ingvar Stepanyan
    @RReverser
    (you could keep them as a single trait, but then you would have to implement clone_into_dyn_box manually for each implementor of SomeTrait, and I tried to avoid that in favour of derive)
    Victor Lopes
    @vlopes11
    hmm got it
    Thanks for the help!
    David O'Connor
    @David-OConnor
    @WreckedAvent Thanks. Aborting, and going with only Vec<T> as the API. I'm leaning towards this being intractable. My goal was for a clean, flexible API
    Ogulcan Erduran
    @erduranogulcan
    I have 1k requests. Each request has 2 queries that check user mail and password ( not in 1 query for example ) how can I handle them in same time to finish in 500ms? Any suggestions?
    Riley Cat
    @WreckedAvent
    you can run them in parallel but if it's performance critical you probably want to work some sql black magic to make it just one request
    ozgurakkurt
    @ozgurakkurt
    hi, I am writing blas functions in assembly to use in rust I have this project https://github.com/ozgurakkurt/aquila. When I run cargo bench sscal is as fast as openblas but when I run RUSTFLAGS="-C target-cpu=native" cargo bench It is 2x slower than openblas. Does anyone know why target-cpu=native is slowing my code?
    Geordon Worley
    @vadixidav
    @ozgurakkurt thats a really general question, but some computers have been shown to perform worse when using AVX512 (https://blog.cloudflare.com/on-the-dangers-of-intels-frequency-scaling/ and https://lemire.me/blog/2018/04/19/by-how-much-does-avx-512-slow-down-your-cpu-a-first-experiment/)
    it seems counter-intuitive, but sometimes this is the case
    it may be preferable to target just AVX-2 and not AVX-512
    this will depend on your workload
    also, it might be that the benchmark isnt representative of real-world scenarios
    so it might be that it IS faster in certian situations, but just not the benchmark you have
    Geordon Worley
    @vadixidav
    also, if you want to inspect the output assembly: https://godbolt.org/
    just use that
    ebinshabu
    @ebinshabu
    hai