Awesome FOSS Logo
Discover awesome open source software
Launched 🚀🧑‍🚀

A Year Hiatus with Rust and Webassembly

Categories
Rust logo + WebAssembly logo

tl;dr - I did the thing where authors disappear for year(s) due to $DAYJOB – I’ve been working with Rust and WebAssembly. I’m going to try to write a bit more!

I’m not back but it has occurred to me that this blog hasn’t seen any content in a whole year (and a day).

Sorry about that – I’ve been busy working at $DAYJOB, and have been doing a lot of Rust and a bunch of WebAssembly.

What have I been up to?

While almost all the code I write at $DAYJOB is F/OSS, I’ve been doing some personal F/OSS work and making some small contributions in the Rust space (and doing more tinkering!) that I’m going to try to get back to writing about.

In the meantime I’ve:

  • Started and stopped running an startup ideas newsletter
  • Continued working on Awesome F/OSS (a listing site for F/OSS projects)
  • Built some content sites
  • Purchased some tiny businesses (SaaS, content sites)
  • … more that is tedious to list here

Anyway here’s some code stuff I’ve been working on.

situwaition (12mo ago)

🔗 crates.io

As you might imagine I’m very proud of the name of this crate, took me a little bit to come up with it.

This is a simple utility crate for waiting for a given event to happen, which can be used in both sync and async contexts. Here’s an example of an async context:

use situwaition::runtime::tokio::wait_for;

// ...

    // Do some waiting
    let result = wait_for(|| async {
        // Get the current value from the mutex
        if some_condition { Ok(value) } else { Err(SomeError) ]
    });

    // Act on the result
    match result {
        Ok(v) => { ... }
        Err(SituwaitionError::TimeoutError(e)) => { ... }
    }

// ...

Full support for both tokio and async-std runtimes!

🗑 async-dropper (9mo ago)

🔗 crates.io

I wrote this a while ago, but it seems people are findng it useful – it’s an implementation of AsyncDrop which currently doesn’t exist in the Rust standard library but you might want to use.

It was fun to write this – there are a couple implementations in there – one ripped off from someone else, and one which I did with a little macro magic.

Here’s a somewhat long example of the interesting implementation:

use std::{
    result::Result,
    time::Duration,
};

use async_dropper::derive::AsyncDrop;
use async_trait::async_trait;

/// This object will be async-dropped
///
/// Objects that are dropped *must* implement [Default] and [PartialEq]
/// (so make members optional, hide them behind Rc/Arc as necessary)
#[derive(Debug, Default, PartialEq, Eq, AsyncDrop)]
struct AsyncThing(String);

/// Implementation of [AsyncDrop] that specifies the actual behavior
#[async_trait]
impl AsyncDrop for AsyncThing {
    // simulated work during async_drop
    async fn async_drop(&mut self) -> Result<(), AsyncDropError> {
        eprintln!("async dropping [{}]!", self.0);
        tokio::time::sleep(Duration::from_secs(2)).await;
        eprintln!("dropped [{}]!", self.0);
        Ok(())
    }

    fn drop_timeout(&self) -> Duration {
        Duration::from_secs(5) // extended from default 3 seconds, as an example
    }

    // NOTE: the method below is automatically derived for you, but you can override it
    // make sure that the object is equal to T::default() by the end, otherwise it will panic!
    // fn reset(&mut self) {
    //     self.0 = String::default();
    // }

    // NOTE: below was not implemented since we want the default of DropFailAction::Continue
    // fn drop_fail_action(&self) -> DropFailAction;
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    {
        let _example_obj = AsyncThing(String::from("test"));
        eprintln!("here comes the (async) drop");
        // drop will be triggered here
        // you could also call `drop(_example_obj)`
    }

    Ok(())
}

Full support for both tokio and async-std runtimes so that’s nice.

🐘 pg_idkit (2y ago)

🔗 Github

pg_idkit is a pgrx-powered Postgres extension that makes it easy to generate IDs inside your database.

I wrote so I could easily get UUIDv7 and other ID types inside Postgres, and because I wanted to take pgrx for a spin, for writing Postgres extensions in Rust.

I’ve continued working on it, and have even spent some time productionizing it (automated builds, etc), which has been a blast.

Rust is quite nice

Rust has been pretty great for a while now. Some time has passed since I wrote kcup in both Rust and Golang to compare them a bit, and even then I knew that Rust was probably the way I’d go for a very long time, and that was obviously very right.

Go clearly has it’s uses (an alternative to Java, and enerprise workhorse with codebases that are too simple to muck up, but with lots of footguns), but Rust feels like a language for the next 10+ years. At this point I’m probably preaching to the choir though.

There are great new options out there today, lately I’ve been sniffing around Zig and Swift (mostly waiting for 6.0!).

WebAssembly is coming

Another thing I’m probabl going to start writing about more is WebAssemblythe funny thing is “modern” WebAssembly it’s neither “Web” nor “Assembly”.

Basically, WebAssembly is Java done right (at least “better”).

Here’s what the WebAssembly ecosystem is driving towards

  • You write code in your $LANGUAGE
  • $LANGUAGE toolchain compiles to WebAssembly (a minimal bytecode that only knows about computations and numbers) + WASI (A standard that enhances webassembly to do things like read STDIN and files, etc) – you hold a .wasm binary now
  • You run your .wasm binary in a compatible runtime (i.e. wasmtime)or a program that wraps a compatible runtime (and gives you some better features)

The wasmtime ecosystem is written in Rust (and better for it) – and it’s been great to contribute over there.

WebAssembly looks like a whole lot of things if you squint at it:

  • Effectful programming (writing WebAssembl with WIT contracts is just like Free(r)/Effects)
  • Capability driven security model (programs can’t read local disk unless you let them)
  • Performant AOT and JIT, type-driven programming
  • Effective RPC-over-network mechanism (turns out if you fully specify types in WIT, you can somewhat easily work out a gRPC-like solution)

I’m pretty excited about it (so much that I joined a company doing it). Not sure what I’ll write about here but I’m sure a bunch of WASM related stuff will be mixed in.

NOTE WASM actually isn’t a valid spelling of “WebAssembly” – WASM isn’t an acronym, but on this blog it is.

I’ll be using WASM to mean “Web ASM” i.e. Web asm i.e. web assembly language.

What’s next?

Well I have no clue. I’ve had a backlog of posts I’ve been meaning to write for a long time. I probably need to go through those and do some trimming because the world has changed a lot since that list was checked.

So much of my backlog is dedicated to Kubernetes, and at this point Kubernetes is already mainstream – back in 2017 when I was really getting into it and writing about it it wasn’t, but maybe that content isn’t so interesting (or relevant) anymore. Well we all know what I write about here is mostly driven by what I find interesting/fun to tinker on so that’ll guide this blog, like always.

Also, you may see some ads on this blog on the future (well, most of you probably won’t – you should have uBlock Origin installed!) – thanks for funding some server costs!