Skip to content

Commit 7a0d627

Browse files
authored
fix/example: shared mutable state (#13)
1 parent 488912c commit 7a0d627

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

.github/workflows/rust.yml

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v3
1919
- uses: actions-rust-lang/setup-rust-toolchain@v1.6
20+
- name: install deps
21+
run: |
22+
sudo apt-get install -y libsqlite3-dev
2023
- name: test examples
2124
run: |
2225
cd examples

examples/application/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod config;
66
pub mod scope;
77
pub mod state;
88
pub mod vh;
9+
pub mod mutable_state;
910

1011
// <multi>
1112
#[ntex::main]

examples/application/src/mutable_state.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// <setup_mutable>
22
use ntex::web;
3-
use std::sync::Mutex;
3+
use std::sync::{Arc, Mutex};
44

5+
#[derive(Clone)]
56
struct AppStateWithCounter {
6-
counter: Mutex<i32>, // <- Mutex is necessary to mutate safely across threads
7+
counter: Arc<Mutex<i32>>, // <- Mutex is necessary to mutate safely across threads
78
}
89

910
async fn index(data: web::types::State<AppStateWithCounter>) -> String {
@@ -19,7 +20,7 @@ async fn index(data: web::types::State<AppStateWithCounter>) -> String {
1920
async fn main() -> std::io::Result<()> {
2021
// Note: app state created _outside_ HttpServer::new closure
2122
let counter = AppStateWithCounter {
22-
counter: Mutex::new(0),
23+
counter: Arc::new(Mutex::new(0)),
2324
};
2425

2526
web::HttpServer::new(move || {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ntex-website",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"private": true,
55
"scripts": {
66
"docusaurus": "docusaurus",

0 commit comments

Comments
 (0)