Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / tokio_channels_demo.rs
Created February 27, 2024 13:26
Rust tokio channels demo
View tokio_channels_demo.rs
use tokio::sync::broadcast;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
#[tokio::main]
async fn main() {
let (tx, rx) = oneshot::channel();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
@fancellu
fancellu / rw_lock_demo.rs
Created February 27, 2024 11:36
Rust tokio demo of RwLock
View rw_lock_demo.rs
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::sleep;
use tokio::time::Duration;
// Demonstrates RwLock with a shared resourced
async fn use_read_lock(id: i32, lock: Arc<RwLock<String>>) {
let lock = lock.read().await;
println!("reader: {} lock string: {}", id, lock);
@fancellu
fancellu / tokio_barrier_demo.rs
Last active February 26, 2024 20:14
Rust tokio barrier demo
View tokio_barrier_demo.rs
use std::sync::Arc;
use tokio::sync::Barrier;
use tokio::sync::BarrierWaitResult;
use tokio::sync::Notify;
use tokio::time::sleep;
use tokio::time::Duration;
// Simulating the filling of boxes with 5 cans, from many tasks
async fn barrier_wait(barrier: Arc<Barrier>, notify: Arc<Notify>, id: usize) -> BarrierWaitResult {
@fancellu
fancellu / tokio_notify_demo.rs
Created February 26, 2024 17:22
Rust tokio demo of Notify usage
View tokio_notify_demo.rs
use std::sync::Arc;
use tokio::sync::Notify;
use tokio::time::sleep;
use tokio::time::Duration;
// Notify can be thought of a Semaphore with 0 permits
async fn order_packages(package_delivered: Arc<Notify>) {
sleep(Duration::from_secs(2)).await;
println!("Company: Find package");
@fancellu
fancellu / tokio_sem.rs
Last active February 25, 2024 11:34
Rust tokio Semaphore example
View tokio_sem.rs
use std::sync::Arc;
use tokio::sync::{Semaphore, SemaphorePermit};
use tokio::time::sleep;
use tokio::time::Duration;
async fn person(sem: Arc<Semaphore>, name: String) {
println!("Person {}: waiting for a permit", name);
teller(sem, &name).await;
println!("\tPerson {} finished", name);
}
@fancellu
fancellu / tokio_mutex.rs
Created February 23, 2024 19:25
Rust example of Tokio Mutex usage between threads
View tokio_mutex.rs
use std::sync::Arc;
use tokio::sync::Mutex;
// async tokio function to increment i32 behind arc mutex
async fn increment(remote: Arc<Mutex<i32>>) {
println!("trying to lock");
let mut tvc = remote.lock().await;
println!("incremented");
*tvc += 1;
}
@fancellu
fancellu / tokio_spawn.rs
Last active February 24, 2024 02:56
Rust example of tokio::spawn and JoinHandle processing
View tokio_spawn.rs
async fn hello(name: &str) -> String {
// pretend to be doing some work
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
format!("Hello {}", name)
}
fn blocking() -> String {
println!("Blocking");
@fancellu
fancellu / csv_demo.rs
Last active February 24, 2024 02:56
Rust simple CSV processing from a CV file, using csv crate
View csv_demo.rs
use std::{error::Error, process};
fn cities() -> Result<(), Box<dyn Error>> {
let mut csv_rdr = csv::Reader::from_path("data/cities.csv")?;
for result in csv_rdr.records() {
let record = result?;
println!("{:?}", record);
println!("{}",record.get(0).unwrap());
}
Ok(())
@fancellu
fancellu / reqwest_demo.rs
Created February 19, 2024 20:02
Rust reqwest http client async json demo with serde
View reqwest_demo.rs
use serde::Deserialize;
use std::time::Duration;
#[derive(Deserialize, Debug)]
struct Product {
id: i32,
title: String,
description: String,
price: f64,
#[serde(rename = "discountPercentage")]
@fancellu
fancellu / DiningPhilosophersIOApp.scala
Created February 16, 2024 14:23
Dining Philosophers with Cats Effect
View DiningPhilosophersIOApp.scala
import cats.effect.std.Semaphore
import cats.effect._
import cats.implicits._
import scala.concurrent.duration._
object DiningPhilosophersIOApp extends IOApp {
case class Fork(id: Int, lock: Semaphore[IO])