Skip to content

Instantly share code, notes, and snippets.

View gmbeard's full-sized avatar

Greg Beard gmbeard

  • Rochdale, UK
  • 23:30 (UTC +01:00)
View GitHub Profile
@gmbeard
gmbeard / factory.cpp
Created February 8, 2019 23:38
Variant return type
#include <variant>
#include <iostream>
#include <string>
struct A { };
struct B { };
struct C { };
using Object = std::variant<A, B, C>;
@gmbeard
gmbeard / pool.cpp
Last active December 6, 2018 22:05
Fixed-size pool allocator
#include <iostream>
#include <memory>
#include <atomic>
#include <cassert>
struct Node {
uint8_t* const data;
size_t const length;
Node* next;
};
@gmbeard
gmbeard / ctrl-z.cpp
Created September 20, 2018 19:50
Ctrl-Z Examples...
// Copyright 2018 G. M. Beard
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in

Keybase proof

I hereby claim:

  • I am gmbeard on github.
  • I am gmbeard (https://keybase.io/gmbeard) on keybase.
  • I have a public key ASC6PdmH8xgc7AHBnMjUmqUNDm0vHyMUVkWH3ziJNkoL7Ao

To claim this, I am signing this object:

@gmbeard
gmbeard / future_internals.rs
Created December 6, 2017 17:52
Rough implementation of Rust's Future / Stream traits, to demonstrate their combinators and ownership semantics
use std::mem;
enum Chain<T, F, U> {
First(T, F),
Second(U),
Done,
}
impl<T, F, U> Future for Chain<T, F, U>
where T: Future,
.text
.def _ZN50_$LT$asm_bug..Rot13$u20$as$u20$asm_bug..Crypto$GT$7encrypt17hbaabafb2f8efe03fE;
.scl 2;
.type 32;
.endef
.section .text,"xr",one_only,_ZN50_$LT$asm_bug..Rot13$u20$as$u20$asm_bug..Crypto$GT$7encrypt17hbaabafb2f8efe03fE
.globl _ZN50_$LT$asm_bug..Rot13$u20$as$u20$asm_bug..Crypto$GT$7encrypt17hbaabafb2f8efe03fE
.p2align 4, 0x90
_ZN50_$LT$asm_bug..Rot13$u20$as$u20$asm_bug..Crypto$GT$7encrypt17hbaabafb2f8efe03fE:
movq 16(%rcx), %r11
.text
.def _ZN50_$LT$asm_bug..Rot13$u20$as$u20$asm_bug..Crypto$GT$7encrypt17hbaabafb2f8efe03fE;
.scl 2;
.type 32;
.endef
.globl __xmm@bfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbf
.section .rdata,"dr",discard,__xmm@bfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbf
.p2align 4
__xmm@bfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbf:
.zero 16,191
use std::ops::Deref;
pub trait Crypto {
type Output;
type Error;
fn encrypt(&mut self) -> Result<Self::Output, Self::Error>;
}
impl<'a, T: Crypto> Crypto for &'a mut T {
@gmbeard
gmbeard / rotate_and_read_search.rs
Last active December 7, 2016 22:00
Non-allocating search for a word in a stream by rotate-and-read
use std::io::Read;
fn fill(buf: &mut [u8]) {
for (i, n) in buf.iter_mut().enumerate() {
*n = i as u8;
}
}
fn rotate<T: Copy + Default>(buf: &mut [T], at: usize) -> usize {
@gmbeard
gmbeard / service_manager.cpp
Created November 1, 2015 20:31
A simple "Service Manager" implementation
#include <iostream>
#include <unordered_map>
#include <memory>
#ifdef _DEBUG
#include <cassert>
#endif
namespace gmb
{
struct service_id { };