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 1 commit
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
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
6 changes: 3 additions & 3 deletions examples/application/src/mutable_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// <setup_mutable>
use ntex::web;
use std::sync::Mutex;
use std::sync::{Arc, Mutex};

struct AppStateWithCounter {
counter: Mutex<i32>, // <- Mutex is necessary to mutate safely across threads
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to use Arc<Mutex<_>> here, or Arc for index handler

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad i fixed it, and let's see if i fixed the ci build aswell

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

web::HttpServer::new(move || {
// move counter into the closure
Expand Down
Loading