Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Snoyman snoyberg

View GitHub Profile
@snoyberg
snoyberg / conduit.rs
Created Mar 15, 2021
Basic and silly implementation of conduit in Rust
View conduit.rs
#![feature(never_type)]
pub enum Conduit<I, O, R> {
Send(O, Box<dyn FnOnce() -> Conduit<I, O, R>>),
Recv(Box<dyn FnOnce(Option<I>) -> Conduit<I, O, R>>),
Done(R),
Leftover(I, Box<dyn FnOnce() -> Conduit<I, O, R>>),
}
use Conduit::*;
@snoyberg
snoyberg / main.rs
Created Dec 29, 2020
HTTP reverse proxy in Rust, from December 29, 2020 livestream
View main.rs
use hyper::{Client, Server, Request, Response, Body};
use anyhow::*;
use std::net::SocketAddr;
use hyper::service::{make_service_fn, service_fn};
use std::sync::{Arc, RwLock};
fn mutate_request(req: &mut Request<Body>) -> Result<()> {
for key in &["content-length", "transfer-encoding", "accept-encoding", "content-encoding"] {
req.headers_mut().remove(*key);
}
@snoyberg
snoyberg / abonimable-snoyman.rs
Created Dec 2, 2020
Playing with GATs, transformers, and more
View abonimable-snoyman.rs
#![feature(generic_associated_types)]
#[allow(dead_code)]
trait Functor {
type Unwrapped;
type Wrapped<B>: Functor;
fn map<F, B>(self, f: F) -> Self::Wrapped<B>
where
F: FnMut(Self::Unwrapped) -> B;
@snoyberg
snoyberg / transformers.rs
Created Dec 2, 2020
Transformers: Rust in disguise
View transformers.rs
#![feature(generic_associated_types)]
trait Functor {
type Unwrapped;
type Wrapped<A>: Functor;
fn map<F, B>(self, f: F) -> Self::Wrapped<B>
where
F: FnOnce(Self::Unwrapped) -> B;
}
@snoyberg
snoyberg / check-status.hs
Created May 27, 2020
Check HTTP status codes
View check-status.hs
#!/usr/bin/env stack
-- stack --resolver lts-15.10 script
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
import RIO
import Network.HTTP.Simple
import qualified RIO.Text as T
main :: IO ()
main = runSimpleApp $ do
@snoyberg
snoyberg / Main.hs
Created Apr 1, 2020
Complete snapshot layer
View Main.hs
#!/usr/bin/env stack
-- stack --resolver foo.yaml script
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
import Pantry
import RIO
import Path
import Path.IO
import Data.Yaml (encodeFile)
@snoyberg
snoyberg / Main.hs
Created Apr 8, 2019
Help debugging the pantry lock bug
View Main.hs
import RIO
import Pantry
import Path
data App = App SimpleApp PantryConfig
instance HasPantryConfig App where
pantryConfigL = lens (\(App _ pc) -> pc) undefined
instance HasLogFunc App where
logFuncL = (lens (\(App sa _) -> sa) undefined).logFuncL
View uses-rio.hs
#!/usr/bin/env stack
-- stack --resolver lts-12.21 script
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
import Control.Monad (replicateM)
import System.Random
import RIO
@snoyberg
snoyberg / no-wrap.rs
Created Jan 21, 2019
NoWrap data type in Rust
View no-wrap.rs
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub struct NoWrap<T>(T);
impl std::ops::Add for NoWrap<u8> {
type Output = Option<Self>;
fn add(self, rhs: Self) -> Self::Output {
self.0.checked_add(rhs.0).map(|x| NoWrap(x))
}
}
@snoyberg
snoyberg / init-arrays.rs
Created Nov 27, 2018
Safely initialize fix length arrays in Rust (WIP!)
View init-arrays.rs
#[derive(Debug)]
struct Foo(usize);
impl Drop for Foo {
fn drop(&mut self) {
println!("Dropping: {}", self.0);
}
}
enum MakeArray<T> {