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

fix/example: shared mutable state #13

Merged
merged 3 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: actions-rust-lang/setup-rust-toolchain@v1.6
- name: install deps
run: |
sudo apt-get install -y libsqlite3-dev
- name: test examples
run: |
cd examples
Expand Down
1 change: 1 addition & 0 deletions examples/application/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod config;
pub mod scope;
pub mod state;
pub mod vh;
pub mod mutable_state;

// <multi>
#[ntex::main]
Expand Down
7 changes: 4 additions & 3 deletions examples/application/src/mutable_state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// <setup_mutable>
use ntex::web;
use std::sync::Mutex;
use std::sync::{Arc, Mutex};

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

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

web::HttpServer::new(move || {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ntex-website",
"version": "2.0.4",
"version": "2.0.5",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
Expand Down