tokio-rusqlite-new

Crates.iotokio-rusqlite-new
lib.rstokio-rusqlite-new
version0.11.0
created_at2025-02-07 05:38:45.708252+00
updated_at2025-07-09 08:14:05.019398+00
descriptionAsynchronous handle for rusqlite library.
homepage
repositoryhttps://github.com/xuxiaocheng0201/tokio-rusqlite
max_upload_size
id1546529
size41,708
XuXiaoCheng (xuxiaocheng0201)

documentation

https://docs.rs/tokio-rusqlite-new

README

Tokio-Rusqlite-New

Crate GitHub last commit GitHub issues GitHub pull requests GitHub

Description

Asynchronous handle for rusqlite library.

This is a fork of tokio-rusqlite.

Guide

This library provides Connection struct. The Connection struct is a handle to call functions in background thread and can be cloned cheaply. Connection::call method calls provided function in the background thread and returns its result asynchronously.

Design

A thread is spawned for each opened connection handle. When call method is called: provided function is boxed, sent to the thread through mpsc channel and executed. Return value is then sent by oneshot channel from the thread and then returned from function.

Usage

use rusqlite::params;
use tokio_rusqlite_new::{Connection, Result};

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>,
}

#[tokio::main]
async fn main() -> Result<()> {
    let conn = Connection::open_in_memory().await?;

    let people = conn.call(|conn| {
        conn.execute(
            "CREATE TABLE person (
                id    INTEGER PRIMARY KEY,
                name  TEXT NOT NULL,
                data  BLOB
            )",
            [],
        )?;

        let steven = Person {
            id: 1,
            name: "Steven".to_string(),
            data: None,
        };

        conn.execute(
            "INSERT INTO person (id, name, data) VALUES (?1, ?2, ?3)",
            params![steven.id, steven.name, steven.data],
        )?;

        let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
        let people = stmt
            .query_map([], |row| {
                Ok(Person {
                    id: row.get(0)?,
                    name: row.get(1)?,
                    data: row.get(2)?,
                })
            })?
            .collect::<Result<Vec<Person>, rusqlite::Error>>()?;

        Ok(people)
    })
    .await?;

    for person in people {
        println!("Found person {person:?}");
    }

    conn.close().await?;
    Ok(())
}

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

License

This project is licensed under the MIT license.

Commit count: 65

cargo fmt