Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

So the beta is out. I've been keeping my eye on Rust for a while and downloaded it. It seems pretty cool so far!

Adbot
ADBOT LOVES YOU

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

space kobold posted:

I've got one dependency that only compiles on the 1.1 nightly, and another dependency that only compiles on the 1.0 beta. Welp.

:negative:

So much for my earlier prediction of things being much more stable and uniform as we approach that big 1.0 release.

Obvious solution is to compile half your program as a cli executable and call it from the other half

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

gonadic io posted:

Quick pick a top library from another language and then write it in Rust! Parsec, lens, an xml parser or something.

This maybe isn't a bad idea if you want 15 minutes of programmer fame.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

drainpipe posted:

I'm starting to learn Rust, and I have a question about using async with threads. I'm writing a program that has multiple threads, each making different http requests. I'm using the reqwest crate for this, which uses async functions. If I try to spawn a thread, I'd need something like an async closure, which is apparently still unstable. Should I just turn the threads to async tasks? There's no reason why I couldn't, but I just wanted to touch on a greater variety of stuff in Rust.

what do you mean? I'm spawning threads inside async functions right now in stable rust.

is what you want to do fundamentally different than this? https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=92cb9e316200bbafa59fd23f1b09a876

fart simpson fucked around with this message at 02:29 on Feb 24, 2020

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Look at the example I just linked. Does that not work?

e: ok, I think I get it

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

drainpipe posted:

It could very well be that spawning threads to do async things is not a correct thing to do since the whole point of async is to do concurrency in one thread. But it's just weird then that I can't make http requests (at least with the reqwest crate) on different threads without using an unstable feature.

I don't understand why you want to do this in the first place, but you could probably use this if you really want to: https://docs.rs/reqwest/0.10.2/reqwest/blocking/index.html

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

drainpipe posted:

Ah thanks! There's really no reason I want to do threads over async tasks, and I'm happy to do it either way. I'm just getting a feel for the land right now, and was curious about how various things fit together.

Well, if you have some async code and you want to fit it into threads the way you're doing, I think you could wrap your async functions in
code:
block_on
and then use them in spawned threads, if you get what I mean. It would basically be converting from async/await to threaded code.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

street doc posted:

I just want to use actix and Postgres for a simple ‘hello world’ example. But it feels like a rabbit hole of complexity. Arctic using tokio? And futures? What asynchronous crate should I be using, in addition to actix?

i made a simple website using actix web and postgres last year. it wasn’t that complicated imho

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

street doc posted:

What dependency did you use for Postgres? Diesel?

i used something called r2d2_postgres. dont remember much about it tbh

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

looking at it again and it looks like mostly what i did was stored procs in postgres and wrote a macro in rust to help build queries with the postgres tokio wrapper. r2d2 is just a connection pool thing

code:
macro_rules! build_query {
    (Vec<$type:ty>, $db:ident, $sql:literal, $args:expr, $res:tt) => {
        web::block(move || {
            let x: DBResult<Vec<$type>> = $db.get()
                .map_err(|e| DBError::PoolError(e))
                .and_then(|c| get(c, $sql, $args))
                .and_then($res);
            x
        })
        .await
    };

    ($type:ty, $db:ident, $sql:literal, $args:expr, $res:tt) => {
        web::block(move || {
            let x: DBResult<$type> = $db.get()
                .map_err(|e| DBError::PoolError(e))
                .and_then(|c| get_row(c, $sql, $args))
                .and_then($res);
            x
        })
        .await
    };
}

fn get(mut c: DBPool, query: &str, params: &[&(dyn tokio_postgres::types::ToSql + Sync)]) -> DBResult<Vec<tokio_postgres::row::Row>> {
    c.query(query, params).map_err(|e| DBError::TokioPostgresError(e))
}

fn get_row(mut c: DBPool, query: &str, params: &[&(dyn tokio_postgres::types::ToSql + Sync)]) -> DBResult<tokio_postgres::row::Row> {
    c.query_one(query, params).map_err(|e| DBError::TokioPostgresError(e))
}

fn get_from_row(row: tokio_postgres::row::Row) -> DBResult<String> {
    row.try_get(0).map_err(|e| DBError::TokioPostgresError(e))
}

pub async fn select_hello(db: web::Data<DB>) -> WebResult<String> {
    build_query!(
        String,
        db,
        "SELECT 'hello';",
        &[],
        { |row| get_from_row(row) }
    )
}
im sure theres a better way but that seemed to work for me???

fart simpson fucked around with this message at 16:29 on Mar 13, 2021

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Ralith posted:

You could make the error handling a lot more concise using the ? operator and maybe a From impl or two, and that macro probably doesn't need to be a macro at all, and if you're using tokio postgres stuff you can probably just await it directly.

yeah probably. although if i ever decide to touch this again ill probably switch to that sqlx thing people were just talking about.

i remember there being a reason i made it a macro, like i tried it another way and it didn’t work. don’t remember why now

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Mata posted:

Thanks for the tips, I think into boxed slice is what feels best for me:
Rust code:
    let vec = Vec::with_capacity(LARGE_NUM);
    // todo fill
    let arr: Box<[f32; LARGE_NUM]> = vec.into_boxed_slice().try_into().unwrap();
While messing around with this, I keep wishing there was a way to index into [T; 256] arrays using u8 indices. Not for perf reasons, I can imagine it's slower than just using the machine's word size. Not really for safety either, just for that feeling of satisfaction when all the pieces fit together just right.

solution: use bigger arrays and just pad the end with zeroes

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Subjunctive posted:

Does anyone know of a Rust training consultancy they’d recommend? I’m thinking of adopting more Rust where I work and as part of that I’d like to look into some structured training options.

nope sorry i dont know anything about this

Adbot
ADBOT LOVES YOU

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

i was being sincere

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply