Skip to content

Instantly share code, notes, and snippets.

View whatisaphone's full-sized avatar
🙁
Sometimes I worry the compiler ignores all my comments

John Simon whatisaphone

🙁
Sometimes I worry the compiler ignores all my comments
View GitHub Profile
@whatisaphone
whatisaphone / circle_point_tangents.rs
Created December 27, 2018 13:44
circle_point_tangents
/// Returns the two points on a circle that form a tangent with the given point.
///
/// If the point is inside the circle, returns `None`.
pub fn circle_point_tangents(
center: Point2<f32>,
radius: f32,
point: Point2<f32>,
) -> Option<[Point2<f32>; 2]> {
// I'm so glad the internet exists
// http://www.ambrsoft.com/TrigoCalc/Circles2/CirclePoint/CirclePointDistance.htm
@whatisaphone
whatisaphone / builder_pattern.rs
Last active November 6, 2018 04:21
Rust builder pattern performance
pub struct X {
a: i32,
b: i32,
c: i32,
d: i32,
}
impl X {
pub fn set_a_mut(&mut self, a: i32) {
self.a = a;
@whatisaphone
whatisaphone / main.rs
Created May 16, 2014 16:48
Rust example of overloading using enums
trait Node { }
struct LI {
nodes: Vec<Box<Node>>
}
impl Node for LI { }
struct A<'a> {
nodes: Vec<Box<Node>>,
#!/bin/bash
# Usage: rust-backtrace ./my-rust-prog args...
exec gdb -batch -n -x /dev/fd/3 --args "$@" 3<<ENDGDB
set height 0
set breakpoint pending on
break rust_fail
@whatisaphone
whatisaphone / segfault.rs
Created January 17, 2014 22:31
Segfault in rust 0.9-ish (rust commit 74258eae)
fn main() {
let mut c = Container { tests: ~[] };
c.add_str_test(~"/");
c.tests[0].test();
}
trait Test {
fn test(&self) -> bool;
}
@whatisaphone
whatisaphone / feistel.py
Created June 12, 2012 20:32
Unlimited length feistel cipher
import hashlib
class FeistelSHA1:
rounds = 4 # 4 rounds is sufficient as long as the round function is cryptographically secure
split = 1 / 2
def __init__(self, key, rounds=rounds):
self.subkeys = [hashlib.sha1(bytes((i,)) + key).digest() for i in range(rounds)]