Skip to content

Instantly share code, notes, and snippets.

@jberkenbilt
Created December 1, 2024 15:46
Show Gist options
  • Save jberkenbilt/48ca46e82d1958701b54da71cac9bb48 to your computer and use it in GitHub Desktop.
Save jberkenbilt/48ca46e82d1958701b54da71cac9bb48 to your computer and use it in GitHub Desktop.
Go to Rust: controller with tokio
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