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]
    [0; 122]?
    1 reply
    tanriol
    @tanriol:matrix.org
    [m]
    When it goes out of scope or is destroyed explicitly.
    tanriol
    @tanriol:matrix.org
    [m]
    Well, you've specifically asked about an u32 array :-)
    Félix Lescaudey de Maneville
    @ManevilleF

    Hello,

    Is there any way to have a struct owning a type T and other structs referencing it in readonly with something like Arc<> or Rc<T>, it seems it's either shared ownership or no reference counting

    Félix Lescaudey de Maneville
    @ManevilleF

    Hello, I'm fighting with the borrow checker and I can't seem to win:

        pub fn find_child_mut(&mut self, index: &Index) -> &mut Self {
            let direction: u8 = index.direction();
            if let Some(child) = self.child_mut(&direction) {
                return child.find_child_mut(index)
            }
            self
        }

    The borrow checker thinks self might still be borrowed after my if let and return statement:

    171 |     pub fn find_child_mut(&mut self, index: &Index) -> &mut Self {
        |                           - let's call the lifetime of this reference `'1`
    ...
    176 |         if let Some(child) = self.child_mut(&direction) {
        |                              ---- first mutable borrow occurs here
    177 |             return child.find_child_mut(index)
        |                    --------------------------- returning this value requires that `*self` is borrowed for `'1`
    178 |         }
    179 |         self
        |         ^^^^ second mutable borrow occurs here

    I know the borrow checker is not smart but is there any way to fix this? I can't find a solution

    tanriol
    @tanriol:matrix.org
    [m]
    I'm afraid you need to check-and-unwrap instead of if let
    1 reply
    I think you need two child_mut calls - one for check, one for return :-(
    1 reply
    This is supposed to be fixed when the Polonius borrow checker becomes the default, but I'm not sure of its current status.
    Félix Lescaudey de Maneville
    @ManevilleF
    Yeah it's sad to have this kind of issues, I never heard of Polonius borrow checker, is it posible to enable it?
    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?