Skip to content

Commit

Permalink
use AtomicU32
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrul committed Dec 31, 2023
1 parent d4511e7 commit 3631cf6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

### Concept
I've choose 3 different ownership concepts with **Atomic**, **Mutex** and **RwLock** to show how to operate handling the todos
- Atomic for the counter, specifically `AtomicU16` unassigned 16 bit integer. the counter will goes up when new todo is inserted to the todos vector.
- `Atomic` for the counter, specifically `AtomicU32` unassigned 32-bit integer. the counter will goes up when new todo is inserted to the todos vector.
- `Mutex` is use to store the todos, with the locking mechanism in place ensure the changes to the todos will be handled correctly on multi thread ops.
- `RwLock` is used to handle the filter (tab link #/all #/active #/completed), since the length is never changed with only selected parameter changed when pages is click, it save to do read/write operations.

Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use astra::{Body, ConnectionInfo, Request, Response, ResponseBuilder, Server};
use fragments::{edit_todo, filter_bar, page, todo_item};
use serde_json::json;
use std::sync::{
atomic::{AtomicU16, Ordering},
atomic::{AtomicU32, Ordering},
Arc, Mutex, MutexGuard, RwLock,
};
use url::form_urlencoded::parse;
Expand All @@ -24,7 +24,7 @@ impl Todo {
// helper method to create a new instance with a calculated ID
// the counter can only go up, which good enough for in-memory indexing
// if we storing the data elsewhere this might not be the case anymore
fn new_id(task: String, done: bool, editing: bool, counter: &Arc<AtomicU16>) -> Todo {
fn new_id(task: String, done: bool, editing: bool, counter: &Arc<AtomicU32>) -> Todo {
let id = counter.fetch_add(1, Ordering::Relaxed); // increment the counter for the next ID
Todo {
id: id.into(),
Expand Down Expand Up @@ -135,7 +135,7 @@ fn update_counts(todos: &MutexGuard<'_, Vec<Todo>>) -> String {
fn handle_request(
_req: Request,
_info: ConnectionInfo,
id_counter: Arc<AtomicU16>,
id_counter: Arc<AtomicU32>,
todos: Arc<Mutex<Vec<Todo>>>,
filters: Arc<RwLock<Vec<Filter>>>,
) -> Response {
Expand Down Expand Up @@ -290,7 +290,7 @@ fn main() {
// choose 3 different ownership concepts with Atomic, Mutex and RwLock
// wrap all in Arc
// use Atomic for the id_counter
let id_counter = Arc::new(AtomicU16::new(0));
let id_counter = Arc::new(AtomicU32::new(0));
// initialize the todos vector, use Mutex, lock for any operations ensure
// the atomic counter always sync when the length of the vector goes up
let todos = Arc::new(Mutex::new(Vec::new()));
Expand Down

0 comments on commit 3631cf6

Please sign in to comment.