Skip to content

Commit

Permalink
tweak counter example for better handler visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcountryman committed Jan 22, 2024
1 parent 1313c26 commit 25a08c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ const COUNTER_KEY: &str = "counter";
#[derive(Default, Deserialize, Serialize)]
struct Counter(usize);

async fn handler(session: Session) -> impl IntoResponse {
let counter: Counter = session.get(COUNTER_KEY).await.unwrap().unwrap_or_default();
session.insert(COUNTER_KEY, counter.0 + 1).await.unwrap();
format!("Current count: {}", counter.0)
}

#[tokio::main]
async fn main() {
let session_store = MemoryStore::default();
Expand All @@ -95,12 +101,6 @@ async fn main() {
.await
.unwrap();
}

async fn handler(session: Session) -> impl IntoResponse {
let counter: Counter = session.get(COUNTER_KEY).await.unwrap().unwrap_or_default();
session.insert(COUNTER_KEY, counter.0 + 1).await.unwrap();
format!("Current count: {}", counter.0)
}
```

You can find this [example][counter-example] as well as other example projects in the [example directory][examples].
Expand Down
12 changes: 6 additions & 6 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const COUNTER_KEY: &str = "counter";
#[derive(Default, Deserialize, Serialize)]
struct Counter(usize);

async fn handler(session: Session) -> impl IntoResponse {
let counter: Counter = session.get(COUNTER_KEY).await.unwrap().unwrap_or_default();
session.insert(COUNTER_KEY, counter.0 + 1).await.unwrap();
format!("Current count: {}", counter.0)
}

#[tokio::main]
async fn main() {
let session_store = MemoryStore::default();
Expand All @@ -25,9 +31,3 @@ async fn main() {
.await
.unwrap();
}

async fn handler(session: Session) -> impl IntoResponse {
let counter: Counter = session.get(COUNTER_KEY).await.unwrap().unwrap_or_default();
session.insert(COUNTER_KEY, counter.0 + 1).await.unwrap();
format!("Current count: {}", counter.0)
}
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
//! #[derive(Default, Deserialize, Serialize)]
//! struct Counter(usize);
//!
//! async fn handler(session: Session) -> impl IntoResponse {
//! let counter: Counter = session.get(COUNTER_KEY).await.unwrap().unwrap_or_default();
//! session.insert(COUNTER_KEY, counter.0 + 1).await.unwrap();
//! format!("Current count: {}", counter.0)
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! let session_store = MemoryStore::default();
Expand All @@ -64,12 +70,6 @@
//! .await
//! .unwrap();
//! }
//!
//! async fn handler(session: Session) -> impl IntoResponse {
//! let counter: Counter = session.get(COUNTER_KEY).await.unwrap().unwrap_or_default();
//! session.insert(COUNTER_KEY, counter.0 + 1).await.unwrap();
//! format!("Current count: {}", counter.0)
//! }
//! ```
//!
//! ## Session expiry management
Expand Down

0 comments on commit 25a08c5

Please sign in to comment.