Created
December 1, 2024 15:46
-
-
Save jberkenbilt/48ca46e82d1958701b54da71cac9bb48 to your computer and use it in GitHub Desktop.
Go to Rust: controller with tokio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::error::Error; | |
use tokio::sync::RwLock; | |
#[derive(Default)] | |
struct ReqData { | |
seq: i32, | |
last_path: String, | |
} | |
#[derive(Default)] | |
pub struct Controller { | |
req_data: RwLock<ReqData>, | |
} | |
impl Controller { | |
pub fn new() -> Self { | |
Default::default() | |
} | |
async fn request(&self, path: &str) -> Result<(), Box<dyn Error + Sync + Send>> { | |
let mut ref_data = self.req_data.write().await; | |
ref_data.seq += 1; | |
// A real implementation would make a network call here. Call await to make this | |
// non-trivially async. | |
async { | |
ref_data.last_path = format!("{path}&seq={}", ref_data.seq); | |
} | |
.await; | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment