Skip to content

Instantly share code, notes, and snippets.

View davidlopezre's full-sized avatar

David Lopez Reyes davidlopezre

View GitHub Profile
@davidlopezre
davidlopezre / main.rs
Created April 25, 2021 06:20
Rust program to read a file specified by user input
use std::fs::File;
use std::io;
use std::io::Read;
fn main() {
let mut user_input = String::new();
io::stdin().read_line(&mut user_input).expect("Failed to read line");
let result = read_file(String::from(user_input.trim()));
match result {
@davidlopezre
davidlopezre / docker-compose.yml
Created May 1, 2021 12:03
Sample Docker Compose file for ghost/mysql
version: '3'
services:
ghost:
image: ghost:1-alpine
container_name: ghost-blog
restart: always
ports:
- 80:2368
environment:
database__client: mysql
@davidlopezre
davidlopezre / unreachable.rs
Created November 24, 2021 11:48
Attempt to get git unreachable objects
use std::collections::HashSet;
use git2::{Commit, Repository, Tree, TreeWalkMode, TreeWalkResult};
use thiserror::Error;
fn main() -> Result<(), Error> {
let repository = Repository::open_from_env()?;
let odb = repository.odb()?;
let mut objects: HashSet<String> = HashSet::new();
@davidlopezre
davidlopezre / mutex.rs
Last active January 30, 2022 11:10
Spin Lock Mutex implementation in Rust from "Crust of Rust: Atomics and Memory Ordering"
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::{cell::UnsafeCell, sync::atomic::AtomicBool};
use std::{thread, usize};
fn main() {
const N: usize = 100;
let data = Arc::new(Mutex::new(0));
let mut handles = Vec::with_capacity(N);
@davidlopezre
davidlopezre / rc.rs
Last active February 9, 2022 10:20
Demonstration of Rc and Weak references
use std::cell::RefCell;
use std::rc::Weak;
#[derive(Debug)]
pub struct Node<T> {
data: T,
// must be a RefCell because we want to store non Copy type `Vec`
// while allowing interior mutability (necessary for this cyclic type)
// this doesn't necessarily need to be weak, it is just for learning
// purposes