Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    tanriol
    @tanriol:matrix.org
    [m]
    On Nightly only, -Z polonius I think
    Félix Lescaudey de Maneville
    @ManevilleF
    Thank you for your help !
    RuRu92
    @RuRu92

    Got stuck in learning how to use mutexes..

    56 |         domain_settings.into_inner().expect("ERROR").get_password_reset_token_duration()
       |         ^^^^^^^^^^^^^ move occurs because `**domain_settings` has type `Mutex<InternalDomainSettings>`, which does not implement the `Copy` trait

    I got this struct

    pub struct DomainSettingProvider {
        settings: Arc<HashMap<DomainName, Mutex<InternalDomainSettings>>>,
        db: Arc<DB>,
    }

    The aim is to use this as a single point of entry for getting the settings. So acting like a cache, and having 1 thread that will update the map every so often.

    So far, it seems that getting values of the mutex pose to be a pain

    RuRu92
    @RuRu92

    This is the func that throws the error

     pub fn get_password_reset_token_duration(&self, domain: DomainName) -> Duration {
            let domain_settings = self.settings.get(domain).expect("Failed to get domain settings");
            domain_settings.into_inner().expect("ERROR").get_password_reset_token_duration()
        }

    Is this the right way to get values out of mutex in a hashmap?

    yorodm
    @yorodm:efym.net
    [m]
    Hi: I would go with Arc<Mutex<HashMap<DomainName,InternalDomainSettings>>> instead
    Or use RwLock if you're worried about reader threads starving
    yorodm
    @yorodm:efym.net
    [m]
    Also I think you want to avoid using into_inner unless you're discarding the Mutex, calling lock should be enough in your case
    RuRu92
    @RuRu92

    Hi @yorodm:efym.net
    Thanks for the answer.

    To understand better, its better to wrap the whole map into mutex ? Even though, the only value updated would the the settings?

    What does RwLock gives me? The data is going to be initialized on startup, I would assume that readers would be fine in this case.

    I will keep in mind regarding the into_inner.
    Does a lock on the mutex prevents readers from accessing the object?

    tanriol
    @tanriol:matrix.org
    [m]
    @RuRu92: If you don't need to add or remove domains, a per-entry lock would be fine.
    yorodm
    @yorodm:efym.net
    [m]
    Hi, I suggested to wrap the whole map in a Mutex or RwLock because of insert and remove operations, also it makes more sense to me not to keep a bunch of Mutexes (with HashMap<T, Mutex<K>> you're getting one for each entry in the map) but if you're not inserting or removing it would be fine
    tanriol
    @tanriol:matrix.org
    [m]
    And yes, you need lock, not into_inner
    RuRu92
    @RuRu92
    I think I follow now, that makes sense. And perhaps it is better to wrap the whole map.
    Besides that, I think I need RwLock as I need readers to not be blocked while having high volume of requests
    Mutex doesnt see the difference between readers and writers as I understand?
    yorodm
    @yorodm:efym.net
    [m]
    Exactly, a Mutex just locks, with RwLock you can have many readers or a single writer
    tanriol
    @tanriol:matrix.org
    [m]
    If the updates are really infrequent and the request volume is really high, I'd consider using something RCU-like instead of locks :-)
    Michal 'vorner' Vaner
    @vorner
    If you are interested in that, I might as well advertise something mine O:-) : https://docs.rs/arc-swap/1.2.0/arc_swap/
    1 reply
    Majid
    @MajidHeydari
    how have to install rust and opencv in ubuntu?
    tanriol
    @tanriol:matrix.org
    [m]
    @ManevilleF: That's exactly the idea :-) the T in it (for example, some configuration) is immutable, and every worker clones it, for example, at start of a new request (and drops when the request is done). When you need to update the configuration, you clone that T, update it and replace the Arc<T> inside RwLock with the new version. When every worker starts the next request, it comes and clones the new version of the configuration.
    Instead of "the configuration can change under your feet at any moment" it becomes "just don't forget to refresh your copy from time to time". On the other hand, it's your copy and you don't need any locking to read it - only to refresh.
    Félix Lescaudey de Maneville
    @ManevilleF
    Isn't there any way to have one struct who owns T and can use it as mutable, and other structs having a readonly reference on it through an Arc<T>? that would be much simpler
    I mean it can be done through borrows (&T) but the lifetimes get crazy over self referencing
    tanriol
    @tanriol:matrix.org
    [m]
    Really depends on what you need from your T. For example, if it contains only atomics, you don't need a mutable reference at all to change them. On the other hand, suppose it contains a Vec<u32> and you need to replace this vector with a different one. Who's supposed to free the old vector and when?
    tanriol
    @tanriol:matrix.org
    [m]
    @vorner: I wonder whether you've ever tried benchmarking against something like URCU
    Félix Lescaudey de Maneville
    @ManevilleF

    My use case is this:

    struct Tree<T> {
         nodes: Vec<Node>,
         objects: Vec<T>,  // Only tree edits the objects
    }
    
    struct Node<T> {
         object_references: Vec<Arc<T>>, // Readonly reference to one of the Tree objects
    }

    Since this can't work I have a Arc<Rwlock<T>> on both Tree and Node:

    struct Tree<T> {
         nodes: Vec<Node>,
         objects: Vec<Arc<Rwlock<T>>>, 
    }
    
    struct Node<T> {
         object_references: Vec<Arc<Rwlock<T>>>, 
    }
    tanriol
    @tanriol:matrix.org
    [m]
    @ManevilleF: That's already an implementation, not a description of a use case. Yes, this is a good solution for some problems. If you're fine with the lock overhead, I'd just leave it as is with Arc<RwLock<T>> in both places.
    Larry Dewey
    @TheSilentDrifter_gitlab
    Is there a native rust way to chown a file?
    tanriol
    @tanriol:matrix.org
    [m]
    Using third-party crates (or manually with FFI). Looks like it was dropped from std during the pre-1.0 purge and never came back.
    Larry Dewey
    @TheSilentDrifter_gitlab
    @tanriol:matrix.org Thanks :)
    f8thl3ss
    @f8thl3ss:synapse.f8thl3ss.engineer
    [m]
    Hey everyone! So i'm build a small project with actix, async_graphql and rusqlite and i'm having a hard time with accessing context (database pool) in my resolvers
    Has anyone ever done something similar before ?

    It looks like this:
    18 async fn get_request<'ctx>(
    19 &self,

    20 ctx: &Context<'ctx>,
    21 request_id: String,
    22 ) -> FieldResult<Option<Request>> {
    23 let pool = ctx.data::<SqlitePool>()?; ‣pool: &Pool<SqliteConnectionManager>
    24 let maybe_request = web::block(move || get_request(request_id, pool)) ‣maybe_request: Optio
    25 .await ‣Result [rustc E0759] [H] ...and is required to live as long as 'static here
    26 .map_err(|e| e.extendwith(|, e| e.set("code", 500)))?;

    The compiler is asking for a static lifetime for my ctx since it moves everything in another blocking thread (the web::block) but I feel like i'm missing something
    I need the blocking part because my librarie sqlite librarie isn't async
    dzil123
    @dzil123:fairydust.space
    [m]
    Have you considered using an async sqlite library?
    I think sqlx is popular https://lib.rs/crates/sqlx
    f8thl3ss
    @f8thl3ss:synapse.f8thl3ss.engineer
    [m]
    I guess I could do that, but i'm still wondering how lifetimes works in this case
    Alexander Van Hecke
    @alexandervanhecke
    Hi, I must be missing something obvious, I'm trying to combine Result values from both the rusqlite crate and the url crate. How do I combine those with an and_then to get something like a Result<Url, Box<dyn std::error::Error>> or something equivalent using anyhow?
    ie I want to do statement.get(1).and_then(Url::parse)
    tanriol
    @tanriol:matrix.org
    [m]
    @alexandervanhecke: Do you need that to be and_then, or would the ? operator work instead?
    Alexander Van Hecke
    @alexandervanhecke
    no, I'd like to be able to get a Result<B, std::error::Error> given 2 functions _ -> Result<A, ErrorA> and A -> Result<B, ErrorB> and given I have a From instance for both ErrorA and ErrorB. Basically if the first function fails I want that error mapped to std::error::Error, if the second fails I want the second error mapped to std::error::Error. Ie something like first_method.map_err(??).and_then(second_method).map_err(??)
    I have a Scala background and am just starting to learn Rust, so if the above is not the right way to do things, please let me know
    Ichoran
    @Ichoran
    @alexandervanhecke - Yeah, this is a case where the typical Scala and the typical Rust approaches differ greatly. Although I mostly use Scala, I think Rust is generally clearer here.
    In Rust, the standard thing to do is use ? to get out the value on the success branch, not use monadic chaining. It's mostly a lot cleaner.

    So whereas in Scala you would

    f(x).flatMap(y => g(y))

    in Rust you'd typically

    let y = f(x)?;
    g(y)

    And if you need to map the error branch, you can do that with map_err in Rust (c.f. swap.map(h).swap in Scala if you're using Either, since Scala doesn't have a method to map the error branch).

    Ichoran
    @Ichoran
    Granted, the two aren't identical; you can do either approach in either language. But if you're in a function that has the right return value, the f(x)? approach is the standard in Rust. And to me, anyway, it keeps things considerably cleaner since at that point you really don't need to remember anything about the error branch--it's just gone. With Scala, you have to continually chain the two types along in your head. More work, at least IMO.
    Alexander Van Hecke
    @alexandervanhecke
    thanks, makes sense :+1:
    Ichoran
    @Ichoran
    (I added a .? macro for myself in Scala so I could do it this way there.)
    Aphimuk Yimsukpaitoon
    @chanhuhu
    Are there any example how to use wkhtmltopdf on actix-web? I cannot generate pdf in the second time because wkpdftohtml limitation.