Categories
Blog

Faster visit of std::variant

Whilst experimenting with std::visit on Godbolt, I was surprised how noisy the generated assembly was. For all the main compilers there seemed to be a lot of calls and vtables being generated, and it all seemed unecessary given that so much could be determined at compile time. I tried using std::variant.index(), std::get() and std::get_if() – initially in a big switch statement – and found that the compiler could be coerced into generating far more minimal code:

Categories
Blog

Household 3D Print – Bath Plug

My bath has – well, had – a push-action bath which would seal when pushed down and unseal when pushed again. Over-engineered crap like this is bugbear of mine. An old-fashioned plug or tap is intuitive to use, and very grabbable in a Gaudi-like way. My experience of modern ‘bathroom innovation’ is just bad UX. The prime example of this was a tap in a restaurant bathroom which had a plain cube on top. Should I turn it or push it? Nope, I tilt it. Naturally.

These designs just add entropy to the universe; they fuel consumer insanity and replace old wisdoms with trends. Case in point, the push-plug in my bathroom was a nightmare to clean and eventually broke off. My bachelor solution for the past week was to simply tape over the hole with gaffa, which works surprisingly well!

Today I thought it would be a good bit micro-DIY to make a new one and test my 3D printing skills. I cracked open a Jupyter notebook and wrote the following.

from solid import *
import math
import viewscad

def make_plug(
    top_diameter: float,
    base_diameter: float,
    height: float,
    ball_radius: float,
    handle_height: float,
    handle_diameter: float
):
    handle_top_radius = handle_diameter / 2
    handle_taper_radius = handle_top_radius / 2

    handle = translate((0, 0, 1))(
        cylinder(
            r1=handle_taper_radius,
            r2=handle_top_radius,
            h=handle_height,
            segments=30
        )
    )

    top_radius = top_diameter / 2
    base_radius = base_diameter / 2
    ball_height = ball_radius + height / 2

    return union()(
        handle,
        difference()(
            cylinder(
                r1=base_radius,
                r2=top_radius,
                h=height,
                segments=60
            ),
            translate((0, 0, ball_height))(
                sphere(
                    r=ball_radius,
                    segments=40
                )
            )
        )
    )

# Bespoke just for my bath!
plug = make_plug(41, 38, 10, 40, 16, 20)

The radii were roughly measured and I did a very low-res print but I was surprised that the test came out really well. Here’s the initial draft.

And amazingly the test print is a perfect fit. A little PVC tape around it will make a good seal. I’ll iterate this a bit, the final print will be in white, higher res, and with bevelled edges.

Categories
Blog Code

Hypothetical Rust-ish Borrowing in C++

Last night I got into a insomniac code-spiral about replicating some of Rust’s borrowing warnings in C++. In place of Rust’s initial variable declararion of let I use wrapper Owner<T> in C++ that internally tracks ownership. Passing to another Owner<T> will take the ownership, or a Borrower<T> class can accept a properly owned value and reference it as a constant.

// Function borrowing a value can only read const ref
static void use(Borrower<int> v) {
  (void)*v;
}

// Function can mutate value via ownership which generates 'warnings' if used again incorrectly
static void use_mut(Owner<int> v) {
  *v = 5;
}

int main() {
  // Rather than just 'int', Owner<int> tracks the lifetime of the value
  Owner<int> x{3};

  // Borrowing value before mutating causes no problems
  use(x);

  // Mutating value passes ownership, has_been_moved set on original x
  use_mut(std::move(x));

  // Uncomment for owner_already_borrowed = 1
  //use(x);

  // Uncomment for owner_already_moved = 1
  //use_mut(std::move(x));

  // Uncomment for another owner_already_borrowed++
  //Borrower<int> y = x;

  //Uncomment for owner_use_after_move = 1;
  //return *x;
}
Categories
Blog

Miyazaki Film Title Generator

This code combines random components of words to produce silly Miyazaki-inspired film titles. Such classics as:

  • Tolo Pippi’s Marvellous Leaf
  • Being Onto
  • Chibot Pom’s Jolly Bramble
  • Poko Trollpi and Tombo Go’s Coat Pushing Whistle
  • Tolo Pom and Asbo Go
  • Chi Pop’s Flambuoyant Sponge Popping Service
  • Kabee Go’s Wandering Jelly
  • Tolopa Popyo’s Functional Castle Sniffing Service
Categories
Blog

Doodles for Lossless Compression of CV Data