There are 2 modules:
Main - main.rs
mod db;
use db::*;
use actix_web::{web, HttpResponse, Result};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyObj {
name: String,
}
async fn index(obj: web::Path<MyObj>) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().json(MyObj {
name: obj.name.to_string(),
}))
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
hello();
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().route(r"{name}", web::get().to(index)))
.bind("127.0.0.1:8088")?
.run()
.await
}
Additional - db.rs
use postgres::{Client, NoTls, Error};
pub fn hello() -> Result<(), Error> {
println!("hello() from db.rs");
let mut client = Client::connect("postgresql://postgres:postgres@localhost/postgres", NoTls)?;
client.batch_execute("
CREATE TABLE IF NOT EXISTS author (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
country VARCHAR NOT NULL
)
")?;
client.batch_execute("
CREATE TABLE IF NOT EXISTS book (
id SERIAL PRIMARY KEY,
title VARCHAR NOT NULL,
author_id INTEGER NOT NULL REFERENCES author
)
")?;
Ok(())
}
Cargo.toml
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Mike_Kharkov <yamaradg@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "2.0"
actix-rt = "1.0"
postgres = "0.17.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
json = "0.12"
In the additional module, the task is to work with the database.
When trying to implement this task using the following guide:
I get the following error:
C:/Users/yamar/.cargo/bin/cargo.exe run --color=always --package hello_world --bin hello_world
Compiling hello_world v0.1.0 (E:\Progi3\Rust_test_project\hello_world)
warning: unused `std::result::Result` that must be used
--> src\main.rs:21:5
|
21 | hello();
| ^^^^^^^^
|
= note: `#[warn(unused_must_use)]` on by default
= note: this `Result` may be an `Err` variant, which should be handled
Finished dev [unoptimized + debuginfo] target(s) in 6.33s
Running `target\debug\hello_world.exe`
hello() from db.rs
thread 'main' panicked at 'default Tokio reactor already set for execution context', C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\io\driver\mod.rs:92:9
stack backtrace:
0: backtrace::backtrace::trace_unsynchronized
at C:\Users\VssAdministrator\.cargo\registry\src\github.com-1ecc6299db9ec823\backtrace-0.3.40\src\backtrace\mod.rs:66
1: std::sys_common::backtrace::_print_fmt
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\sys_common\backtrace.rs:77
2: std::sys_common::backtrace::_print::{{impl}}::fmt
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\sys_common\backtrace.rs:59
3: core::fmt::write
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libcore\fmt\mod.rs:1057
4: std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\io\mod.rs:1426
5: std::sys_common::backtrace::_print
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\sys_common\backtrace.rs:62
6: std::sys_common::backtrace::print
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\sys_common\backtrace.rs:49
7: std::panicking::default_hook::{{closure}}
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\panicking.rs:195
8: std::panicking::default_hook
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\panicking.rs:215
9: std::panicking::rust_panic_with_hook
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\panicking.rs:463
10: std::panicking::begin_panic<str*>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\panicking.rs:390
11: tokio::io::driver::set_default::{{closure}}
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\io\driver\mod.rs:92
12: std::thread::local::LocalKey<core::cell::RefCell<core::option::Option<tokio::io::driver::Handle>>>::try_with<core::cell::RefCell<core::option::Option<tokio::io::driver::Handle>>,closure-0,()>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:262
13: std::thread::local::LocalKey<core::cell::RefCell<core::option::Option<tokio::io::driver::Handle>>>::with<core::cell::RefCell<core::option::Option<tokio::io::driver::Handle>>,closure-0,()>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:239
14: tokio::io::driver::set_default
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\io\driver\mod.rs:89
15: tokio::runtime::io::variant::set_default::{{closure}}
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\io.rs:43
16: core::option::Option<tokio::io::driver::Handle*>::map<tokio::io::driver::Handle*,tokio::io::driver::DefaultGuard,closure-0>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libcore\option.rs:450
17: tokio::runtime::io::variant::set_default
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\io.rs:43
18: tokio::runtime::handle::{{impl}}::enter::{{closure}}<closure-0,core::result::Result<(tokio_postgres::client::Client, tokio_postgres::connection::Connection<tokio_postgres::socket::Socket, tokio_postgres::tls::NoTlsStream>), tokio_postgres::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\handle.rs:34
19: tokio::runtime::blocking::pool::{{impl}}::enter::{{closure}}<closure-0,core::result::Result<(tokio_postgres::client::Client, tokio_postgres::connection::Connection<tokio_postgres::socket::Socket, tokio_postgres::tls::NoTlsStream>), tokio_postgres::error::
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\blocking\pool.rs:194
20: std::thread::local::LocalKey<core::cell::Cell<core::option::Option<const tokio::runtime::blocking::pool::Spawner*>>>::try_with<core::cell::Cell<core::option::Option<const tokio::runtime::blocking::pool::Spawner*>>,closure-0,core::result::Result<(tokio_pos
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:262
21: std::thread::local::LocalKey<core::cell::Cell<core::option::Option<const tokio::runtime::blocking::pool::Spawner*>>>::with<core::cell::Cell<core::option::Option<const tokio::runtime::blocking::pool::Spawner*>>,closure-0,core::result::Result<(tokio_postgre
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:239
22: tokio::runtime::blocking::pool::Spawner::enter<closure-0,core::result::Result<(tokio_postgres::client::Client, tokio_postgres::connection::Connection<tokio_postgres::socket::Socket, tokio_postgres::tls::NoTlsStream>), tokio_postgres::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\blocking\pool.rs:179
23: tokio::runtime::handle::Handle::enter<closure-0,core::result::Result<(tokio_postgres::client::Client, tokio_postgres::connection::Connection<tokio_postgres::socket::Socket, tokio_postgres::tls::NoTlsStream>), tokio_postgres::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\handle.rs:33
24: tokio::runtime::Runtime::block_on<std::future::GenFuture<generator-0>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\mod.rs:416
25: postgres::config::Config::connect<tokio_postgres::tls::NoTls>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\postgres-0.17.5\src\config.rs:324
26: postgres::client::Client::connect<tokio_postgres::tls::NoTls>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\postgres-0.17.5\src\client.rs:33
27: hello_world::db::hello
at .\src\db.rs:6
28: hello_world::main::{{closure}}
at .\src\main.rs:21
29: std::future::{{impl}}::poll<generator-0>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\future.rs:43
30: tokio::task::local::{{impl}}::poll<std::future::GenFuture<generator-0>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\task\local.rs:307
31: tokio::runtime::basic_scheduler::{{impl}}::block_on::{{closure}}<tokio::park::either::Either<tokio::time::driver::Driver<tokio::park::either::Either<tokio::io::driver::Driver, tokio::park::thread::ParkThread>>, tokio::park::either::Either<tokio::io::drive
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\basic_scheduler.rs:139
32: tokio::runtime::global::with_state::{{closure}}<closure-1,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\global.rs:100
33: std::thread::local::LocalKey<core::cell::Cell<tokio::runtime::global::State>>::try_with<core::cell::Cell<tokio::runtime::global::State>,closure-0,core::result::Result<(), std::io::error::Error>>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:262
34: std::thread::local::LocalKey<core::cell::Cell<tokio::runtime::global::State>>::with<core::cell::Cell<tokio::runtime::global::State>,closure-0,core::result::Result<(), std::io::error::Error>>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:239
35: tokio::runtime::global::with_state<closure-1,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\global.rs:83
36: tokio::runtime::global::with_basic_scheduler<closure-1,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\global.rs:62
37: tokio::runtime::basic_scheduler::BasicScheduler<tokio::park::either::Either<tokio::time::driver::Driver<tokio::park::either::Either<tokio::io::driver::Driver, tokio::park::thread::ParkThread>>, tokio::park::either::Either<tokio::io::driver::Driver, tokio:
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\basic_scheduler.rs:122
38: tokio::runtime::{{impl}}::block_on::{{closure}}<tokio::task::local::LocalFuture<std::future::GenFuture<generator-0>>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\mod.rs:419
39: tokio::runtime::global::with_state::{{closure}}<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\global.rs:100
40: std::thread::local::LocalKey<core::cell::Cell<tokio::runtime::global::State>>::try_with<core::cell::Cell<tokio::runtime::global::State>,closure-0,core::result::Result<(), std::io::error::Error>>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:262
41: std::thread::local::LocalKey<core::cell::Cell<tokio::runtime::global::State>>::with<core::cell::Cell<tokio::runtime::global::State>,closure-0,core::result::Result<(), std::io::error::Error>>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:239
42: tokio::runtime::global::with_state<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\global.rs:83
43: tokio::runtime::global::with_basic_scheduler<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\global.rs:62
44: tokio::runtime::basic_scheduler::Spawner::enter<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\basic_scheduler.rs:174
45: tokio::runtime::spawner::Spawner::enter<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\spawner.rs:30
46: tokio::runtime::handle::{{impl}}::enter::{{closure}}::{{closure}}<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\handle.rs:36
47: tokio::time::clock::Clock::enter<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\time\clock.rs:30
48: tokio::runtime::time::variant::with_default<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\time.rs:43
49: tokio::runtime::handle::{{impl}}::enter::{{closure}}<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\handle.rs:36
50: tokio::runtime::blocking::pool::{{impl}}::enter::{{closure}}<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\blocking\pool.rs:194
51: std::thread::local::LocalKey<core::cell::Cell<core::option::Option<const tokio::runtime::blocking::pool::Spawner*>>>::try_with<core::cell::Cell<core::option::Option<const tokio::runtime::blocking::pool::Spawner*>>,closure-0,core::result::Result<(), std::i
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:262
52: std::thread::local::LocalKey<core::cell::Cell<core::option::Option<const tokio::runtime::blocking::pool::Spawner*>>>::with<core::cell::Cell<core::option::Option<const tokio::runtime::blocking::pool::Spawner*>>,closure-0,core::result::Result<(), std::io::e
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:239
53: tokio::runtime::blocking::pool::Spawner::enter<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\blocking\pool.rs:179
54: tokio::runtime::handle::Handle::enter<closure-0,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\handle.rs:33
55: tokio::runtime::Runtime::block_on<tokio::task::local::LocalFuture<std::future::GenFuture<generator-0>>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\runtime\mod.rs:416
56: tokio::task::local::{{impl}}::block_on::{{closure}}<std::future::GenFuture<generator-0>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\task\local.rs:288
57: tokio::task::local::{{impl}}::with::{{closure}}<core::result::Result<(), std::io::error::Error>,closure-0>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\task\local.rs:381
58: std::thread::local::LocalKey<core::cell::Cell<core::option::Option<core::ptr::non_null::NonNull<tokio::task::local::Scheduler>>>>::try_with<core::cell::Cell<core::option::Option<core::ptr::non_null::NonNull<tokio::task::local::Scheduler>>>,closure-0,core:
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:262
59: std::thread::local::LocalKey<core::cell::Cell<core::option::Option<core::ptr::non_null::NonNull<tokio::task::local::Scheduler>>>>::with<core::cell::Cell<core::option::Option<core::ptr::non_null::NonNull<tokio::task::local::Scheduler>>>,closure-0,core::res
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\thread\local.rs:239
60: tokio::task::local::Scheduler::with<core::result::Result<(), std::io::error::Error>,closure-0>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\task\local.rs:377
61: tokio::task::local::LocalSet::block_on<std::future::GenFuture<generator-0>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.6\src\task\local.rs:287
62: actix_rt::runtime::Runtime::block_on<std::future::GenFuture<generator-0>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\actix-rt-1.0.0\src\runtime.rs:89
63: actix_rt::builder::SystemRunner::block_on<std::future::GenFuture<generator-0>,core::result::Result<(), std::io::error::Error>>
at C:\Users\yamar\.cargo\registry\src\github.com-1ecc6299db9ec823\actix-rt-1.0.0\src\builder.rs:187
64: hello_world::main
at .\src\main.rs:19
65: std::rt::lang_start::{{closure}}<core::result::Result<(), std::io::error::Error>>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\rt.rs:67
66: std::rt::lang_start_internal::{{closure}}
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\rt.rs:52
67: std::panicking::try::do_call<closure-0,i32>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\panicking.rs:296
68: panic_unwind::__rust_maybe_catch_panic
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libpanic_unwind\lib.rs:79
69: std::panicking::try
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\panicking.rs:272
70: std::panic::catch_unwind
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\panic.rs:394
71: std::rt::lang_start_internal
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\/src\libstd\rt.rs:51
72: std::rt::lang_start<core::result::Result<(), std::io::error::Error>>
at /rustc/760ce94c69ca510d44087291c311296f6d9ccdf5\src\libstd\rt.rs:67
73: main
74: invoke_main
at f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:64
75: __scrt_common_main_seh
at f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:253
76: BaseThreadInitThunk
77: RtlUserThreadStart
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Panic in Arbiter thread.
error: process didn't exit successfully: `target\debug\hello_world.exe` (exit code: 101)
Process finished with exit code 101
I understand that the guide used the main()
function, unlike my additional module.
If I include the same code in my main module but with the main()
function, then there are no problems.
Questions:
Why are these errors being thrown?
How in this case is it necessary to implement this task in an additional module - without causing errors?
If there is a task to connect to the same database (in the same additional module) not using the native Rast, but using the actix-web framework - can you give an example of how this could be implemented? (no unnecessary bells and whistles - just the most primitive case.)