Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[E0596] cannot borrow data in an Arc as mutable. #6

Closed
ArunGust opened this issue Feb 10, 2021 · 4 comments
Closed

[E0596] cannot borrow data in an Arc as mutable. #6

ArunGust opened this issue Feb 10, 2021 · 4 comments

Comments

@ArunGust
Copy link

Thank you @pjtatlow

I tried to clone and pass-through Arc, but it shows [E0596] cannot borrow data in an Arc as mutable. Any idea ?

let jdb = jammdb::DB::open("../jammdb").unwrap();
HttpServer::new(move || {
App::new()
.data(jdb.clone())
async fn fn_inituserdb(req: HttpRequest, _jdb: Datajammdb::DB) ->Result<String, Error> {
let _tx = _jdb.tx(true).borrow_mut().as_ref().map_err(|e| actix_web::http::StatusCode::BAD_REQUEST);
| ^^^^ cannot borrow as mutable

[E0596] cannot borrow data in an Arc as mutable.
[Note] cannot borrow as mutable

@pjtatlow
Copy link
Owner

Hey @ArunGust! Yeah this is an annoying design limitation that I'm going to resolve in version 0.4.0. Should have that out sometime today!

@ArunGust
Copy link
Author

thanks

@pjtatlow
Copy link
Owner

Alright 0.4.0 has been published. Here's an example of using jammdb with actix-web

use actix_web::{get, web, App, HttpServer, Responder};
use jammdb::DB;

#[get("/{id}/{name}")]
async fn index(
    web::Path((id, name)): web::Path<(u32, String)>,
    db: web::Data<DB>,
) -> impl Responder {
    let tx = db.tx(true).unwrap();
    let b = tx.get_or_create_bucket("users").unwrap();
    if let Some(kv) = b.get_kv(id.to_be_bytes()) {
        let db_name = std::str::from_utf8(kv.value()).unwrap();
        if db_name != name {
            format!("No. Your name is {}! id:{}", db_name, id)
        } else {
            format!("Welcome back {}! id:{}", name, id)
        }
    } else {
        b.put(id.to_be_bytes(), name.as_str()).unwrap();
        tx.commit().unwrap();
        format!("Welcome {}! id:{}", name, id)
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let jdb = jammdb::DB::open("./data.jammdb").unwrap();

    HttpServer::new(move || App::new().service(index).data(jdb.clone()))
        .bind("127.0.0.1:8080")?
        .run()
        .await
}

@ArunGust
Copy link
Author

Thanks a lot, let me try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants