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]
    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.
    MCF
    @mcf-rocks
    error[E0432]: unresolved import std::panic::catch_unwind
    --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.31/src/stream/catch_unwind.rs:3:18
    |
    3 | use std::panic::{catch_unwind, UnwindSafe, AssertUnwindSafe};
    | ^^^^^^^^^^^^ no catch_unwind in panic
    wat?
    tanriol
    @tanriol:matrix.org
    [m]
    I hope that's not a Rust version from 2015 :-)
    MCF
    @mcf-rocks
    code is old, but used to compile
    rustc -V rustc 1.51.0 (2fd73fabe 2021-03-23)
    yes, probably something is out of date, but i don't see "futures" in the cargo.toml
    how to know what to change?
    tanriol
    @tanriol:matrix.org
    [m]
    Are you building just for normal Windows/Linux/Mac or for some unusual target?
    MCF
    @mcf-rocks
    unusual target
    real freak show
    tanriol
    @tanriol:matrix.org
    [m]
    If an unusual target, it'd be interesting to hear which :-) there can be something weird with its std