Rust: A Productive Language for Writing Database Applications

ScyllaDB 758 views 28 slides Oct 14, 2024
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

Think Rust is just about performance and safety? Let's talk productivity. Last year, Rust's library ecosystem needed work. What's changed? I'll dive into the current state of Rust libraries for database access, focusing on ergonomics and ease of use. #RustLang #DatabaseDev


Slide Content

A ScyllaDB Community
Rust: A Productive Language for
Writing Database Applications
Carl Lerche
Principal Engineer at AWS

RUST IS (MOSTLY) USED AT THE
INFRASTRUCTURE LEVEL

RUST IS PRODUCTIVE

SAME BENEFITS APPLY
HIGHER UP THE STACK

ARE WE WEB YET?
HTTPS://AREWEWEBYET.ORG

TOASTY
A NEW ORM FOR RUST

EASE OF USE
OVER
MAXIMIZING PERFORMANCE

SQL
BUT ALSO NOSQL

PROCEDURAL MACROS

async fn hello_world() -> impl IntoResponse {
let hello_world = json!({
"hello": "world"
});

Json(hello_world)
}

let app = Router::new()
.route("/", get(hello_world));

#[derive(Serialize)]
struct HelloWorld {
hello: &'static str,
}

async fn hello_world() -> impl IntoResponse {
Json(HelloWorld {
hello: "world",
})
}

#[toasty::model]
struct User {
#[key]
#[auto]
id: Id,

#[unique]
email: String,

todos: [Todo],
}

SCHEMA FILE
AND
CODE GENERATION

let user = User::find_by_email(email)
.get(&db)
.await?;

async fn hello_world() -> impl IntoResponse {
let message = "world";
let hello_world = json!({
"hello": message
});

Json(hello_world)
}

HOW IMPORTANT IS READING
THE GENERATED CODE?

#[derive(Serialize)]
struct HelloWorld {
hello: &'static str,
}

AVOID SECOND-ORDER
TRAIT BOUNDS

fn set_name<T>(&mut self, name: T)
where
T: Into<String>,
{ ... }

fn serve<T, R>(T)
where
T: Service<Response = R>
R: Into<http::Response>,
{ ... }

pub fn find_by_email<'a>(
email: impl stmt::IntoExpr<'a, String>
) -> FindByEmail<'a> {

let expr = User::EMAIL.eq(email);
let query = Query::from_expr(expr);
FindByEmail { query }
}

RESULT FOR RUNTIME ERRORS
PANIC FOR BUGS

uri::Builder::new()
.authority("tokio.rs")
.build()
.unwrap();

let Ok(uri) = uri::Builder::new()
.authority(user_provided_authority)
.try_build()
else {
eprintln!("invalid authority");
abort();
};

let user = User::find_by_email(email)
.get(&db)
.await?;

for todo in user.todos() {
println!(
"Category: {}",
todo.category().name);
}

let user = User::find_by_email(email)
.include(toasty::path![User.todos.category])
.get(&db)
.await?;

WHAT IS NEXT?

THANK YOU
HTTPS://DISCORD.GG/TOKIO
Tags