https://discord.cloudflare.com logo
Join Discord
Powered by
# 🦀workers-rs
  • z

    zeb

    05/26/2023, 6:53 PM
    Using
    lazy_static
    or
    once_cell
    should work, what behavior were you running into?
  • s

    sirupsen

    05/26/2023, 6:54 PM
    Yeah, I was using
    lazy_static
    but the key is absent between requests
  • s

    sirupsen

    05/26/2023, 6:54 PM
    Copy code
    lazy_static! {
        static ref METADATA_CACHE: Mutex<HashMap<String, CacheValue>> = Mutex::new(HashMap::new());
    }
  • s

    sirupsen

    05/26/2023, 6:59 PM
    Hmm, seems to work now, must've hit some evicted isolates or something
  • s

    sirupsen

    05/26/2023, 6:59 PM
    Thx for confirming zeb
  • z

    zeb

    05/26/2023, 6:59 PM
    Great to hear, was worried there for a second 😅
  • s

    sirupsen

    05/26/2023, 7:02 PM
    👍 Thanks for confirming! Using a mutex feels a bit weird in this environment, but seems like you need to satisfy all the traits and borrow rules
  • k

    kian

    05/26/2023, 7:02 PM
    Isolates are per metal rather than per colo so whilst isolates tend to hang around for a long time (I stopped counting the highest I’ve seen at 22 hours), actually hitting an existing one unless you’ve got a connection already alive is a bit of a pain
  • s

    sirupsen

    05/26/2023, 7:03 PM
    Word. I should probably just rely on a cache instead
  • k

    kian

    05/26/2023, 7:11 PM
    I guess it depends on what you're storing / how intensive/slow it is when you have a miss
  • k

    kian

    05/26/2023, 7:12 PM
    Global scope is super fast but per isolate, cache is per colo, KV has two central stores but is just as fast as cache when hot and then R2/DO are stored in .
  • l

    Liberator

    05/27/2023, 5:10 PM
    Hey guys, how do I get started with Rust on Workers?
  • k

    kian

    05/27/2023, 5:12 PM
    Try
    npx wrangler generate <NEW_PROJECT_NAME> worker-rust
  • s

    sameerali

    05/27/2023, 5:38 PM
    getting error, when i run
    npx wrangler@3.0.0 generate cf  worker-rust

    https://cdn.discordapp.com/attachments/882757739019911219/1112072286049144912/Screenshot_2023-05-27_at_10.37.29_AM.png▾

  • k

    kian

    05/27/2023, 5:40 PM
    Try experimental/worker-rust
  • s

    sameerali

    05/27/2023, 5:41 PM
    remote: Repository not found.

    https://cdn.discordapp.com/attachments/882757739019911219/1112073154274263151/Screenshot_2023-05-27_at_10.41.24_AM.png▾

  • k

    kian

    05/27/2023, 5:43 PM
    Lovely
  • k

    kian

    05/27/2023, 5:44 PM
    Guess all of the experimental templates in the Wrangler repo don’t work with generate
  • j

    James

    05/27/2023, 5:44 PM
    You could try
    npm init cloudflare@1 my-project worker-rust
  • k

    kian

    05/27/2023, 5:44 PM
  • j

    James

    05/27/2023, 5:45 PM
    I've got an issue in around this at https://github.com/cloudflare/workers-sdk/issues/3348. C3 has broken all the old
    init
    commands unfortunately
  • a

    Adam!!!

    05/27/2023, 10:21 PM
    hey, i've just been working on this & just wondering whether this looks like the right approach? i'm basing it on this file: *set_reject should be the same return type as forward

    https://cdn.discordapp.com/attachments/882757739019911219/1112143530559803433/image.png▾

  • a

    afro_ke

    05/28/2023, 8:57 PM
    Hmm, is there a way for the
    worker
    to use a
    0.2
    version for
    wasm-bindgen
    so we can use
    wasm-bindgen-futures
    ?

    https://cdn.discordapp.com/attachments/882757739019911219/1112484752679768134/image.png▾

  • a

    afro_ke

    05/28/2023, 11:15 PM
    Also any idea why I am getting this error?
  • a

    afro_ke

    05/28/2023, 11:15 PM

    https://cdn.discordapp.com/attachments/882757739019911219/1112519446267953203/image.png▾

  • a

    afro_ke

    05/28/2023, 11:15 PM
    Is it on my side? I am not using any smart pointers, no compile errors so this must be from a smart pointer
  • z

    zeb

    05/28/2023, 11:45 PM
    Requests in JS can be immutable, meaning you can't get a mutable reference to the inner path. So you're calling https://docs.rs/worker/latest/worker/struct.Request.html#method.path_mut on an immutable (according to js, not necessarily in terms of borrow checker) request
  • z

    zeb

    05/28/2023, 11:45 PM
    As for wasm-bindgen-futures, we probably need to bump our version of wasm-bindgen
  • z

    zeb

    05/28/2023, 11:46 PM
    you can add wasm-bindgen-futures pinned to an earlier version for now,
    0.4.30
    is what we use currently
  • How to send `FormData` via `Fetch`? I
    a

    alex35mil

    12/02/2023, 5:34 PM
    Apparently, there's no way to use exposed
    FormData
    for a request body, so I just build a string manually (I don't send any files yet, so it supports only text fields):
    Copy code
    rust
    pub trait Body: fmt::Debug {
        fn content_type(&self) -> String;
        fn try_into(&self) -> Result<JsValue, worker::Error>;
    }
    
    #[derive(Debug)]
    pub struct FormDataBody(HashMap<String, String>);
    
    impl FormDataBody {
        const BOUNDARY: &'static str = "X-INSERT-YOUR-BOUNDARY";
    
        pub fn new() -> Self {
            Self(HashMap::new())
        }
    
        pub fn append(&mut self, key: &str, value: &str) {
            self.0.insert(key.to_string(), value.to_string());
        }
    }
    
    impl Body for FormDataBody {
        fn content_type(&self) -> String {
            format!("multipart/form-data; boundary={}", Self::BOUNDARY)
        }
    
        fn try_into(&self) -> Result<JsValue, worker::Error> {
            let mut body = String::new();
    
            for (key, value) in &self.0 {
                body.push_str(&format!("--{}\r\n", Self::BOUNDARY));
                body.push_str(&format!("Content-Disposition: form-data; name=\"{key}\""));
                body.push_str(&format!("\r\n\r\n{value}\r\n"));
            }
    
            body.push_str(&format!("--{}--\r\n", Self::BOUNDARY));
    
            Ok(body.into())
        }
    }
    r
    n
    • 3
    • 5