by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Sylwester Rąpała
    @xoac
    buf is type of bytes::Buf
    Sylwester Rąpała
    @xoac

    The Frame should be valid as long as buf is valid. and it's defined to be valid for ('a) - longer or equal to Frame ('p).

    Is this error limitation of rust or I am wrong?

    John W.
    @TrustTheRust

    Hey would some rust veteran please help with this “temporary value dropped while borrowed” ? I’ve tried cloning all the values too. Value drops when converting a UUID to String and then a &str, below:

    Code:

    if let Some(vec) = &campaign.sequences {
    
       let mut sw: SmoothWeight<&str> = SmoothWeight::new();
    
       for seq in vec {
           sw.add(seq.sequence.sid.to_string().as_str(), seq.weight as isize);
       }

    Error:

     sw.add(seq.sequence.sid.to_string().as_str(), seq.weight as isize);
        |                 --     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                               - temporary value is freed at the end of this statement
        |                 |      |
        |                 |      creates a temporary which is freed while still in use
        |                 borrow later used here
    matrixbot
    @matrixbot
    aladdy John W. (Gitter): Does replacing seq.sequence.sid.to_string().as_str() with &seq.sequence.sid.to_string() work?
    aladdy That should keep the String in scope to be freed after the function call completes.
    John W.
    @TrustTheRust
    Thanks a lot @matrixbot , worked
    Frank McSherry
    @frankmcsherry

    I'm trying out serde for the first time, and was hoping to write a custom deserializer for JSON. In particular, I started with the goal of defining a type

    enum Command<'de> {
        Null,
        Bool(bool),
        Number(f64),
        String(&'de str),
        ArrayStart,
        ArrayEnd,
        MapStart,
        MapEnd,
    }

    and then trying to deserialize a Vec<Command<'de>>.

    Now, I've been hitting my head against Deserialize<'de> and Visitor<'de> for a while, and I thought I would ask if it is even possible. At least, using deserialize_any. It looks like deserialize creates an owned value from nothing, meaning it would be difficult to populate an already existing non-static allocation, and that the Visitor pattern consumes the visitor with each visit (the self argument, vs say &mut self).
    Any thoughts, or better places to ask about serde?
    Sylwester Rąpała
    @xoac

    Have you read https://serde.rs/impl-deserialize.html? You can also look at existing serde_json implementation.

    Better place for ask are listed in github readme: https://github.com/serde-rs/serde#getting-help

    Frank McSherry
    @frankmcsherry
    Thanks!
    Jeffrey Smith
    @jeffsmith82
    i'm trying to find my postion in an interator so i can just slice from there to the end https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=886747ec5f5bcb768314eac09ab8b34c is this possible to do safely ?
    Jasper
    @jbg
    @jeffsmith82 since the HTTP spec says those are single space characters, not runs of spaces, i'd do it like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=183b14b843f51a5c2c9fefed550131f4 (I changed the Result return type to Option just because I'm lazy and it makes the example clearer to be able to use ? this way)
    note there is an http crate that is a much better idea than rolling your own
    if you really want to use split_whitespace(), just next() for the first two and then collect::<Vec<_>>().join(' ') for the last one, but you would collapse runs of whitespace in the description field into single spaces
    Jasper
    @jbg
    itertools also has intersperse() so you could do intersperse(" ").collect::<String>() if you don't mind the dependency
    Sylwester Rąpała
    @xoac
    If someone is interested with my Buf problem I created an issue in bytes repo: tokio-rs/bytes#395
    Jeffrey Smith
    @jeffsmith82
    @jbg cheers for that. Was just trying to write something equivalent to something someone did in our c code base at work. would use the the http crate for this if i rewrote it.
    prographo
    @prographo
    I gave up trying to get reqwest to work, I am now doing a system call to do curl... how to convert osStr to regular String?
    prographo
    @prographo
    what is the difference between .output() and .spawn() https://doc.rust-lang.org/std/process/struct.Command.html
    I suspect .output is blocking and .spawn is non-blocking, but it doesn't say that.
    prographo
    @prographo
    myOsStr.into_string() seems to work for conversion
    Jasper
    @jbg

    I suspect .output is blocking and .spawn is non-blocking, but it doesn't say that.

    as the docs say, output waits for the command to complete and returns its output (stdout and stderr). spawn returns immediately, and you can read from the file handles while the command is running

    doing HTTP this way seems crazy to me... reqwest is so easy to use...
    Arnaud Esteve
    @aesteve
    Hello everyone, it's been 5 or 6 times in the last year I tried to learn Rust through a real-life example, and every time I'm blocked facing borrowing issues.
    Here's an example: I have an ApiConfiguration struct containing immutable data read from a file, then, everywhere, I'd like to define utility stuff like this:
    fn get_status_gauge(config: ApiConfiguration) -> Option<GaugeVec> {
        config
            .checks
            .into_iter()
            .flat_map(|check| check.harvests.into_iter())
            .find(|h| match h.kind {
                HarvestKind::StatusCode => true,
                _ => false,
            })
            .map(|h| {
                register_gauge_vec!(
                    format!("http_status_{}", config.name.clone()),
                    "The HTTP status returned by the API.".to_string(),
                    &[h.name.as_str()]
                )
                .unwrap()
            })
    }
    but I can't, since "borrow of moved value config"
    I'm trying to read code from some libraries, to understand how such an issue gets tackled, to be able to define some tiny utility functions, but get myself lost, almost every time.
    I'm missing obvious stuff for sure, would anyone have good blogs/books/etc. on "how to structure your codebase in Rust". The book details the concepts, but not really "how should I think"
    matrixbot
    @matrixbot
    aladdy I guess to get a start on that, the into_iter takes ownership of the value it is used on, consuming it. Meaning that any later uses of it are invalid.
    Denis Lisov
    @tanriol
    @frankmcsherry I'd also like to note that String(&'de str) likely will not work because the string can be escaped and require unescaping (unless you want to unescape it in the JSON text itself)
    Denis Lisov
    @tanriol
    @jeffsmith82 I'm pretty sure there are external crates supporting this kind of things. Also, if you're into contributing to Rust, I'd say a method called remaining or so could be an interesting addition to different split iterators.
    The collect-based versions are not equivalent as they allocate, but for the HTTP status line that probably should not be a problem.
    @prographo What are the problems with reqwest? Are its examples not good enough? Is there any example that should be added in your opinion?
    Denis Lisov
    @tanriol
    @aesteve Why are you trying to pass the configuration by value instead of by reference?
    Also you likely to want iter instead of into_iter as noticed above.
    Arnaud Esteve
    @aesteve
    Both the errors you mention are true @tanriol , I changed both after pasting the code. Unfortunately, since config is used elsewhere in the code, I can't pass the reference without facing borrowing issues
    So I'm getting a bit closer : I should never ever put the whole config as a field of a struct, if I do so, I lose the ability to use &config everywhere in my code
    Denis Lisov
    @tanriol
    Sometimes I also use Arc<Config> when I need shared ownership of the config.
    laurent bernabé
    @loloof64

    Hi ! In a quite medium size project, I am facing an issue relative to closure and static lifetime. So I've tried my best to create a simple playground that has the same issue that in my project :

    https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a99521c9bc690b0103bd82a528c21e2b

    I need a way to make the outer_test() work, without changing its argument (I am given a reference to a RefCell of a custom struct, which don't implement Copy, nor Clone. If it is impossible, I think I can implement Clone for the custom struct).

    But I have issues with the fact that instance_a is not static, and probably will never be. Meanwhile, the closure really needs to be static (a widget callback).

    I forgot that instance_a needs to be borrow_mut(), and not just borrow().
    Arnaud Esteve
    @aesteve
    Arc<> is definitely something I think I totally need to understand a bit better so that I can identify patterns where it's useful when I'm facing them. Thank you
    laurent bernabé
    @loloof64

    Sorry, I rather meant a use case like this one : https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=90cdd7f492837b1346db18aa484f6289

    I can't figure out how to make the outer_test work

    Denis Lisov
    @tanriol
    Something like this?
    laurent bernabé
    @loloof64
    Thank you very much :smile:
    Denis Lisov
    @tanriol
    Sorry, I missed the point that the closure needs to be static.
    laurent bernabé
    @loloof64
    Oups, np
    In fact I have to do with it, because I am creating closure for GTK widget callback
    laurent bernabé
    @loloof64
    It may be impossible. I should design my code otherwise, I think
    Denis Lisov
    @tanriol
    Sounds unsafe :-(
    laurent bernabé
    @loloof64
    Ok thanks.
    So I'll have to think the design again