<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>ENOSPC</title>
		<description>breaking things with style since 199;</description>
		<link>https://eno.space/blog</link>
		<atom:link href="https://eno.space/blog/feed.xml" rel="self" type="application/rss+xml" />
		
			<item>
				<title>Getting on the syncing ship</title>
				<description>&lt;p&gt;For a couple of years now we’ve been interested in trying this &lt;a href=&quot;https://www.Mozilla.org/en-US/firefox/sync/&quot;&gt;Firefox sync&lt;/a&gt; thing.
It always sounded like a pretty nice idea, and being able to host your own storage server that holds all your data sounded very nice indeed.
Unfortunately the same can not be said about the &lt;em&gt;accounts&lt;/em&gt; server that is also needed to get sync working:
while we can keep control of our &lt;em&gt;data&lt;/em&gt; just fine, the &lt;em&gt;metadata&lt;/em&gt; would always live on Mozilla servers in the Mozilla accounts system.
This obviously would not do.&lt;/p&gt;

&lt;p&gt;Fortunately Mozilla publishes their code for the accounts server complex in their &lt;a href=&quot;https://github.com/mozilla/fxa/&quot;&gt;fxa repository&lt;/a&gt;.
It’s pretty easy to clone this repository to look at what the accounts server does (although it’s still not obvious, the accounts server being a massive pile of javascript), and the repository even contains a number of dockerfiles for self-hosters.
Awesome!
Right?&lt;/p&gt;

&lt;!--content--&gt;

&lt;p&gt;Yeah, not so much.
Mozilla, as we’ve learned, does not have any resources to implement features for a self-hosted setup.
While this may not sound bad at first, consider that fxa is built to handle every Firefox &lt;em&gt;on the planet&lt;/em&gt;.
It’s made to be very scalable, and to this end integrates pretty tightly with both Google and Amazon cloud services.
Obviously neither of those are necessary for &lt;em&gt;development&lt;/em&gt; of fxa because emulators for all those cloud services are available, but fxa very much assumes that if it is deployed “properly”, it’ll be deployed in a cloud.&lt;/p&gt;

&lt;p&gt;This is obviously not great, and a weak point of &lt;a href=&quot;https://github.com/jackyzy823/fxa-selfhosting&quot;&gt;the sole remaining self-hosting guide we know of&lt;/a&gt;.
There have been other guides in the past, all using heaps of docker containers (and this one is no different), but all except this one have faded or gone away.
And even using this guide has its problems: fxa just remains a huge &lt;em&gt;thing&lt;/em&gt;, requiring at least 2GB of memory to run at all.
It won’t even run “properly” by some definition of the word since using cloud emulators in production setups is not expected and only possible in test mode.&lt;/p&gt;

&lt;p&gt;This obviously would not do.&lt;/p&gt;

&lt;h2 id=&quot;if-you-dont-like-the-x-make-your-own-x&quot;&gt;if you don’t like the $X, make your own $X!&lt;/h2&gt;

&lt;p&gt;After having spent a couple of days trying to figure out how all this sync stuff goes together and reading through the (actually pretty good) &lt;a href=&quot;https://mozilla.github.io/ecosystem-platform/&quot;&gt;documentation of the protocols&lt;/a&gt; we decided to just make our own.
It’s just a REST API that manages very little data, the cryptography is fully specified, we don’t even have to implement most of the endpoints.
It should be easy, right?
Right?&lt;/p&gt;

&lt;p&gt;Right.&lt;/p&gt;

&lt;h3 id=&quot;insert-rant-about-how-mozilla-violates-their-own-specs-a-lot&quot;&gt;insert rant about how mozilla violates their own specs a lot&lt;/h3&gt;

&lt;p&gt;Getting something to demonstrate that going down this badger den is feasible wasn’t all that hard.
The javascript auth client from fxa is sufficiently disconnected from the rest of fxa that we could just use it with no modification, and the cryptography used is well specified.
Creating accounts and having Firefox log in wasn’t hard to achieve.
Pretty soon there was support for sending tabs between Firefox instances.
It should’ve been easy to add the rest.&lt;/p&gt;

&lt;p&gt;And then the fun started.&lt;/p&gt;

&lt;p&gt;As it turns out we have an Android phone, and wanted to connect Fenix (the Android version of Firefox) to sync as well.
But it couldn’t connect to sync.
At all.
We double- and triple-checked that the &lt;a href=&quot;https://mozilla.github.io/ecosystem-platform/reference/webchannels-in-fenix-webextensions&quot;&gt;webchannel protocol&lt;/a&gt; was correctly implemented, and it was!
Desktop Firefox worked just fine, but Fenix could not even query the current account status.
Something was &lt;em&gt;wrong&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;del&gt;After some questioning and searching we found out that what was wrong is that Fenix still has &lt;a href=&quot;https://github.com/mozilla-mobile/android-components/issues/6225&quot;&gt;a bug that was allegedly fixed&lt;/a&gt;, but either the fix was incomplete from day one or it was broken again in the interim. It’s hard to tell and doesn’t much matter, because now we had to figure out how to interact with the login system at all when the only documented way to do so did not work.&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;&lt;del&gt;Luckily that same bug report, and the bugfix pull request it linked to, provided a clue: the protocol &lt;em&gt;can&lt;/em&gt; be implemented, since there’s an extension that handles the protocol and translates it into a different protocol that Fenix &lt;em&gt;actually&lt;/em&gt; uses internally. And as it turns out it is possible to send and receive the necessary messages within that extension. Problem solved, right?&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;&lt;del&gt;Not really, since both sending and receiving requires debug access to the protocol translator extension. This is of course achievable with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adb&lt;/code&gt; and a desktop Firefox instance, but it makes for a very bad user experience to require &lt;em&gt;two&lt;/em&gt; debuggers to log in to your sync account on a phone.&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;&lt;del&gt;Naturally that is how it works to this day because the bug remains unfixed (and possibly ignored).&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE (7.8.2022):&lt;/strong&gt; the above is not a truthful representation of things. The bug has indeed been fixed with the pull request linked in the issue. The problem we had lies somewhere else entirely. Fenix does not seem to have an fxa-over-http mode like desktop Firefox has and also seems to reject fxa over HTTPS if the certificate is not permanently trusted. When run “properly”, with a certificate signed by a trusted authority, it &lt;em&gt;still&lt;/em&gt; replies to every account management request with an error. &lt;em&gt;After&lt;/em&gt; that error it replies with the expected response, which totally breaks an application that treats an error as fatal on Fenix because it is also fatal on Firefox. As it turns out all that’s necessary is to ignore the error, and things just work as expected.&lt;/p&gt;

&lt;h4 id=&quot;so-login-works-now&quot;&gt;so login works now!&lt;/h4&gt;

&lt;p&gt;Except it didn’t.
Sending the login &lt;em&gt;messages&lt;/em&gt; worked, but login itself still failed.
Instead of logging in properly Fenix just crashed.
Digging through the debug logs of the phone for a bit we found out that Fenix absolutely &lt;strong&gt;requires&lt;/strong&gt; a certain field in a certain fxa API response to be set, even though this field is marked as optional in the API specification.
If the field is not present, Fenix crashes with a null reference exception.
So we just send some placeholder data with no actual information content to make Fenix not crash now.&lt;/p&gt;

&lt;h4 id=&quot;pushing-through&quot;&gt;pushing through&lt;/h4&gt;

&lt;p&gt;With logins now working (albeit very painfully) we could finally test tab sending from Firefox to Fenix.
Which worked fine, but didn’t immediately pop up a notification on the phone in the way it did when sending to a desktop browser.
Receiving the notification (and thus tab data) required opening the tab menu on the phone, which should not actually be necessary.&lt;/p&gt;

&lt;p&gt;As it turns out, Fenix does not support push notifications for tab sending, at least when run from an ungoogled phone.
&lt;em&gt;Technically&lt;/em&gt; it should support this, but after more digging through the logs we found out that it simply doesn’t register itself for push notifications because it’s missing some google cloud API config, presumably because that will only be available with an active google account on the phone.
No idea, at that point we just stopped asking and accepted that push notifications will not work.
(Which naturally also includes all &lt;em&gt;other&lt;/em&gt; push notifications fxa sends, such as “display name has changed” or “your device has been removed from the account”.)&lt;/p&gt;

&lt;h4 id=&quot;at-least-its-not-an-nft&quot;&gt;at least it’s not an NFT&lt;/h4&gt;

&lt;p&gt;After having figured out how do stuff that works fully within the database of the account server the natural next step is to attach the storage server and see what happens there.
And, naturally, nothing worked.
And, naturally, it was because Mozilla does not implement exactly what they specified.&lt;/p&gt;

&lt;p&gt;One of the great features about Firefox sync in particular is that Firefox encrypts all encryptable data before it leaves your computer.
All data put into the sync storage server is encrypted with a key only your browsers know, command payloads like which url the tab you just sent to another device has is encrypted with a key only the target browser knows, it’s actually quite good.
There’s even a &lt;a href=&quot;https://mozilla.github.io/ecosystem-platform/explanation/scoped-keys&quot;&gt;specification&lt;/a&gt; on how to derive key material for services, and one would expect that the specified procedure would also be used for sync itself.
Right?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;… Right.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As it turns out,&lt;/em&gt; the procedure for deriving &lt;em&gt;sync&lt;/em&gt; keys is very different from the procedure for deriving &lt;em&gt;all other keys&lt;/em&gt;.
But that, too, is a problem that can be solved with sufficient amounts of special-casing.&lt;/p&gt;

&lt;h4 id=&quot;-and-other-problems&quot;&gt;… and other problems&lt;/h4&gt;

&lt;p&gt;Needless to say that it did not end there.
Instead there are a surprisingly large number of &lt;em&gt;other&lt;/em&gt; bugs and problems.
Some because Mozilla does not follow their own specifications, some probably due to configurations Mozilla does not think are possible, some because they seem to not implement &lt;em&gt;other&lt;/em&gt; specifications correctly either (relying on their own servers behaving as they expect instead).
It’s all reasonable from the position of Mozilla, but it does not shine the best light on this part of the ecosystem.&lt;/p&gt;

&lt;h2 id=&quot;it-lives&quot;&gt;it lives!&lt;/h2&gt;

&lt;p&gt;But after all this trouble we now have a small API server that implements &lt;em&gt;just enough&lt;/em&gt; of fxa to make sync possible.
It does not implement any payment processing for VPN services nobody asked for.
It can’t integrate with third-party to-do list applications.
It just syncs.
And nothing more.
If you’re interested and want to try self-hosting a &lt;em&gt;complete&lt;/em&gt; sync stack you can &lt;a href=&quot;https://git.eno.space/minor-skulk.git/&quot;&gt;check out the code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But beware:&lt;/strong&gt; this is unsupported software.
We can’t provide support for this if something breaks, use it at your own risk.
It may not even work for you if the one thing you need for &lt;em&gt;your&lt;/em&gt; sync usage hasn’t made it into the code yet.
It hasn’t been tested on anything older than Firefox 101 and almost certainly won’t render the UI correctly on anything older than 98.
Emailed patches may or may not be accepted.&lt;/p&gt;

&lt;p&gt;Best of luck!&lt;/p&gt;
</description>
				<pubDate>Tue, 26 Jul 2022 00:00:00 +0200</pubDate>
				<link>https://eno.space/blog/2022/07/getting-on-the-syncing-ship</link>
				<guid isPermaLink="true">https://eno.space/blog/2022/07/getting-on-the-syncing-ship</guid>
			</item>
		
			<item>
				<title>NixOS on underpowered devices</title>
				<description>&lt;p&gt;Imagine a little ARM board with a 64 bit processor on it.
Something like a Raspberry Pi 3, or a newer Pi 2.
These things aren’t exactly the first to come to mind when anyone asks for “a fast computer”.&lt;/p&gt;

&lt;p&gt;So what should we do if we want to run NixOS on one of these, which routinely uses a gigabyte of memory or more to do system updates?
We could add a lot of swap space and wait a while, sure, but that gets annoying pretty quickly and doesn’t exactly speed things up either.&lt;/p&gt;

&lt;p&gt;Now what could we do to make this whole process a little nicer?&lt;/p&gt;

&lt;!--content--&gt;

&lt;p&gt;There are two approaches to making a slow system run NixOS maintenance operations faster:
do them Somewhere else, or do them somewhere Else.&lt;/p&gt;

&lt;h1 id=&quot;capital-s-somewhere-else&quot;&gt;capital S somewhere else&lt;/h1&gt;

&lt;p&gt;One of these is to use &lt;a href=&quot;https://nixos.org/manual/nix/stable/#chap-distributed-builds&quot;&gt;remote builds&lt;/a&gt;.
Remote builds are a feature of nix that allow one machine to offload work to another machine, in any fraction from none at all to everything—including the tasks &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nixos-rebuild&lt;/code&gt; runs to turn a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configuration.nix&lt;/code&gt; file into a system configuration.
When a nix task is to be run (and any remote builder configured on the system has resources to run that task) nix may run the task on a remote builder instead of running it locally.
(There are many variables that determine whether or not a task will be run locally or remotely.
 The manual has a much better explanation of these than can be provided here, but we can summarize it as “as much as possible as quickly as possible”.)&lt;/p&gt;

&lt;p&gt;Offloading work to another machine of course requires that other machine to be able to &lt;em&gt;do&lt;/em&gt; that work.
For nix this usually requires that the machine doing the offloading and the machine being offloaded to be the same architecture (we won’t go much into why that is, only note &lt;em&gt;that&lt;/em&gt; it is).
If you have a small ARM board and a very powerful ARM board this works perfectly fine, but if you have a small ARM board and a more powerful x86 laptop it works a lot less well.&lt;/p&gt;

&lt;p&gt;The way to make it work at all requires the x86 machine to &lt;em&gt;emulate&lt;/em&gt; an ARM CPU, and run all tasks within that emulation.
NixOS conveniently provides a few options to enable such emulation, for the case of supporting a 64 bit ARM machine it’s as easy as setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;boot.binfmt.emulatedSystems = [ &quot;aarch64-linux&quot; ];&lt;/code&gt; on the systems tasks are offloaded to.
For the ARM machine to &lt;em&gt;use&lt;/em&gt; this new emulated sibling it must be told where to look for help, and what help that help can actually provide.
Since requests for help are sent via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh&lt;/code&gt; commands the machine doing the helping must also be configured to &lt;em&gt;accept&lt;/em&gt; those commands:&lt;/p&gt;
&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# slow machine&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;nix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;distributedBuilds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;nix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;buildMachines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;hostName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fast-machine&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;sshUser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;builder&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;systems&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x86_64-linux&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aarch64-linux&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;supportedFeatures&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;big-parallel&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# allow big compile tasks&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# fast machine&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;users&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;users&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;isNormalUser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;openssh&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;authorizedKeys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;keys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;...&quot;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# ssh public key of root on the slow machine&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;nix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;trustedUsers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;builder&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Unfortunately emulating a different CPU architecture is not a very efficient thing to do, so offloaded tasks may not run much faster than they did originally on the machine that did the offloading.
But they &lt;em&gt;do&lt;/em&gt; run and they often have more memory to work with, which in itself can speed up a task a lot even when running under emulation—just from not having to swap constantly.&lt;/p&gt;

&lt;p&gt;This approach works well if only a few small packages have to be compiled from source, otherwise it will still take a pretty long time from all the emulation overhead.&lt;/p&gt;

&lt;h1 id=&quot;somewhere-capital-e-else&quot;&gt;somewhere capital E else&lt;/h1&gt;

&lt;p&gt;The other option is to cross-compile everything.
Where before we were running lots of small tasks &lt;em&gt;on&lt;/em&gt; the architecture of the slow machine and generated results &lt;em&gt;for&lt;/em&gt; the architecture of the slow machine, now we simply skip that first bit: we run all tasks on a fast machine (what architecture isn’t important any more) and generate results &lt;em&gt;for&lt;/em&gt; the architecture of the slow machine.
This can give us a &lt;em&gt;massive&lt;/em&gt; speed boost on large tasks.&lt;/p&gt;

&lt;p&gt;The easiest way to achieve this kind of thing is setting a few &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configuration.nix&lt;/code&gt; options on the slow machine and using remote builders as before, just without the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;binfmt&lt;/code&gt; trick:&lt;/p&gt;
&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;localSystem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x86_64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;crossSystem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aarch64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Setting these two will run &lt;em&gt;all&lt;/em&gt; tasks on an x86 machine, but produce results for an aarch64 machine (eg, a Pi 3).&lt;/p&gt;

&lt;p&gt;Sounds perfect, right?&lt;/p&gt;

&lt;h2 id=&quot;not-so-fast&quot;&gt;not so fast&lt;/h2&gt;

&lt;p&gt;There are two big problems with cross-compiling: build system bugs, and not being able to use binary caches.&lt;/p&gt;

&lt;p&gt;Build system bugs cause cross-compile taks to fail where they shouldn’t, usually because the possiblity of being cross-compiled was ignored or forgotten in some corner of a piece of software.
These bugs &lt;em&gt;can&lt;/em&gt; be fixed, but they are extremely tedious and best avoided if at all possible.&lt;/p&gt;

&lt;p&gt;Not being able to use binary caches is perhaps more important for the “small pi running a webserver from the corner of the room” use case.
Since cross-compiling uses a different compiler and a different set of build instructions than building “normally” does, and nix folds everything that goes into creating a package into the &lt;em&gt;identity&lt;/em&gt; of that package, a cross-compiled package has a different identity than a natively compiled package.
The binary caches provided by the nixos project only contain &lt;em&gt;natively&lt;/em&gt; compiled packages.
Both of these together mean that using this method requires &lt;strong&gt;a lot&lt;/strong&gt; of time spent compiling packages that already exist in compiled form, but a form we can’t use when cross-compiling because what &lt;em&gt;creates&lt;/em&gt; those packages differes between us and the cache.&lt;/p&gt;

&lt;h1 id=&quot;best-of-both-worlds&quot;&gt;best of both worlds?&lt;/h1&gt;

&lt;p&gt;We can however combine both approaches if most of the packages we’re using can be pulled from binary caches.
To do this we’ll once more enable emulation on a fast machine:&lt;/p&gt;
&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;boot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;binfmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;emulatedSystems&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aarch64-linux&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;users&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;users&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;isNormalUser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;openssh&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;authorizedKeys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;keys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;...&quot;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# ssh public key of root on the slow machine&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;nix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;trustedUsers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;builder&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;and enable remote builds on the slow ARM board:&lt;/p&gt;
&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;nix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;distributedBuilds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;nix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;buildMachines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;hostName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fast-machine&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;sshUser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;builder&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;systems&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x86_64-linux&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aarch64-linux&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;supportedFeatures&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;big-parallel&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# allow big compile tasks&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So far there is no cross-compilation to be found, everything is either pulled from binary caches or compiled “natively” (in emulation on the fast machine, if necessary).
Once we find a package that takes too long to compile like this (we will use the linux kernel as an example), we can use the &lt;a href=&quot;https://nixos.org/manual/nixpkgs/stable/#sec-cross-usage&quot;&gt;cross-compilation support of nixpkgs&lt;/a&gt; to &lt;em&gt;selectively&lt;/em&gt; cross-compile only that one package—although we will also have to compile (possibly cross-compile) every package that depends on anything we’ve cross-compiled so far.
Since we want to use a fast machine to assist a slow board we aren’t done with just specifying which architecture a compile task should run &lt;em&gt;for&lt;/em&gt; (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;crossSystem&lt;/code&gt;), but we must specify which architecture the task should run &lt;em&gt;on&lt;/em&gt; (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localSystem&lt;/code&gt;).
The architecture we’ll compile &lt;em&gt;for&lt;/em&gt; is chosen later, by taking it from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pkgs.pkgsCross.&amp;lt;architecture&amp;gt;&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pkgs&lt;/code&gt; as we normally would.
In the case of cross-compiling the kernel, we’d use this configuration snippet:&lt;/p&gt;
&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;boot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;kernelPackages&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# was: pkgs.linuxPackages_latest&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;localSystem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x86_64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;kn&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pkgsCross&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;aarch64-multiplatform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;linuxPackages_latest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If this seems rather contrived (since we can just use the kernel from a binary cache instead), here’s the snippet for cross-compiling a pinebook pro kernel using the &lt;a href=&quot;https://github.com/samueldr/wip-pinebook-pro&quot;&gt;pinebook pro overlay&lt;/a&gt; by &lt;a href=&quot;https://github.com/samueldr&quot;&gt;samueldr&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;boot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;kernelPackages&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# was: pkgs.linuxPackages_pinebookpro_lts&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;localSystem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x86_64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;crossSystem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aarch64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;# linuxPackages_pinebookpro_lts is defined by this overlay&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;overlays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;sx&quot;&gt;./modules/wip-pinebook-pro/overlay.nix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;kn&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;linuxPackages_pinebookpro_lts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Another more general (and perhaps easier) way to specify which packages to cross-compile and which to take from a cache or compile natively is to create an overlay that takes &lt;em&gt;specific&lt;/em&gt; packages from the cross-compiled set, but leaves the rest intact.&lt;/p&gt;
&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;boot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;kernelPackages&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;linuxPackages_pinebookpro_lts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;overlays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;mkAfter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;localSystem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x86_64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;crossSystem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aarch64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;overlays&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;sx&quot;&gt;./modules/wip-pinebook-pro/overlay.nix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;kn&quot;&gt;inherit&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;linuxPackages_pinebookpro_lts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib.mkAfter&lt;/code&gt; in the definition of overlays.
This is necessary because including the pinebook pro overlay as a module per its instructions adds the overlay that defines &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linuxPackages_pinebookpro_lts&lt;/code&gt; and we want to &lt;em&gt;replace&lt;/em&gt; this package, so our overlay must be ordered &lt;em&gt;after&lt;/em&gt; other overlays.
If we merely wanted to replace a package that already exists (eg firefox) ordering is not as important.
Replacing one package like this triggers a rebuild of all packages that &lt;em&gt;depend&lt;/em&gt; on the replaced package (or packages that depend on &lt;em&gt;that&lt;/em&gt; package, etc), but it doesn’t automatically &lt;em&gt;cross-compile&lt;/em&gt; those depending packages.
Each package that is to be cross-compiled has to be listed in the overlay, what isn’t listed in such a manner will be compiled natively (or using emulation).&lt;/p&gt;

&lt;h1 id=&quot;but-nixops&quot;&gt;but nixops..?&lt;/h1&gt;

&lt;p&gt;All of these tricks also work within machine configurations in nixops!
When deploying across architectures with nixops it is necessary to ensure that all packages are compiled for the  architecture being deployed to, which means that for such deployments either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nixpkgs.localSystem&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nixpkgs.crossSystem&lt;/code&gt; &lt;em&gt;must&lt;/em&gt; be set.
The combined approach—in this case, setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localSystem = &quot;aarch64-linux&quot;;&lt;/code&gt; and using overlays to cross-compile specific packages—is probably easiest to get started and uses binary caches to save a lot of compilation time.
For nixops we don’t need remote builders either and can instead run everything on just a single machine (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;binfmt&lt;/code&gt; emulation where required), making things easier still.&lt;/p&gt;

&lt;p&gt;The easiest case, where all machines nixops manages are aarch64 ARM board, can be as simple as setting&lt;/p&gt;
&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;defaults&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;localSystem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aarch64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nixops.nix&lt;/code&gt;!&lt;/p&gt;
</description>
				<pubDate>Mon, 30 Aug 2021 00:00:00 +0200</pubDate>
				<link>https://eno.space/blog/2021/08/nixos-on-underpowered-devices</link>
				<guid isPermaLink="true">https://eno.space/blog/2021/08/nixos-on-underpowered-devices</guid>
			</item>
		
			<item>
				<title>Keyboard To Launch Control</title>
				<description>&lt;p&gt;We all know that science can go wrong, and that of all science that can go wrong rocket science has lots of potential to go the wrongest.
Usually you would want to launch your rocket smoothly, on a trajectory pointing in the general direction of “up”, and then control it for a good long while after.
But sometimes things do not turn out quite as planned, and you get an explosion instead.&lt;/p&gt;

&lt;p&gt;Computer science is a lot like rocket science in that respect, except that our explosions tend to be somewhat less spectacular.&lt;/p&gt;

&lt;!--content--&gt;

&lt;h2 id=&quot;we-have-a-problem&quot;&gt;We have a problem&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/blog/images/keyboard-to-launch-control/oops.jpg&quot; alt=&quot;A laptop keyboard. The windows key has disintegrated, the keycap is lying beside flipped on its top side and the scissor mechanic that stabilizes the key is in pieces.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;(Why yes keyboards are filthy!)&lt;/small&gt;
So our window manager key (otherwise known as the Windows key, Super, Meta, or Mod4) has just.
Exploded.
Pressed it, let go, had pieces fly off exploded.&lt;/p&gt;

&lt;p&gt;We are now short a key that is needed to do &lt;em&gt;absolutely anything&lt;/em&gt; with our computer (because tiling window manager), but we can’t fix the key.
Tiny plastic pieces broke off, and it simply will not go back together again.&lt;/p&gt;

&lt;p&gt;Luckily there is a key on the keyboard that we do not use, and it’s even in the photo!
It’s the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&amp;lt;|&lt;/code&gt; key, also known as 102nd.
(We’ll get back to that.)&lt;/p&gt;

&lt;p&gt;Now we cannot simply tell our computer to use that key for window manager functions because it’s a character key that, on this layout, sends the same characters as some other keys.
That means we will somehow have to inform this wondrous device of crystal magic that this key in particular should now be a different key, kthx.&lt;/p&gt;

&lt;h2 id=&quot;mapping-sans-geography&quot;&gt;Mapping, sans geography&lt;/h2&gt;

&lt;p&gt;But to do that we’ll have to understand a bit about how keyboards work.
This will be somewhat simplified, but we don’t need to know the exact details for this adventure.&lt;/p&gt;

&lt;p&gt;When a key on the keyboard is pressed, say the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt; key, the keyboard does not send the character &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;.
Some keyboards of old did in fact do that, but this hasn’t been standard practice for many years because it makes changing keyboard layouts hard to impossible.
After all it’d be the &lt;em&gt;keyboard&lt;/em&gt; that has to do the change, and reprogramming the keyboard does not sound like good times.
Instead keyboards send what is called a &lt;em&gt;scancode&lt;/em&gt; that identifies a key, but has no further meaning.
All the scancode is supposed to do is &lt;em&gt;be the same&lt;/em&gt; across all keyboards of a given type—in this case, PC keyboards.
All PC keyboards, even if they’ve got very different print on the keys, will send the same scancode for that physical key.
Pressing a key sends a scancode, releasing the key sends a different scancode.
That’s all keyboards do.&lt;/p&gt;

&lt;p&gt;This makes it possible—and in fact necessary—to translate from scancodes to characters or other things.
The translation takes scancodes as inputs, and gives &lt;em&gt;keycodes&lt;/em&gt; as outputs.
Where a scancode for the character &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt; on the US layout may be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7&lt;/code&gt;, the &lt;em&gt;keycode&lt;/em&gt; can be something entirely different.
On Linux the X11 keycode for this key is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15&lt;/code&gt;, with symbolic name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;AE06&amp;gt;&lt;/code&gt;.
Most importantly, that’s not a character 6 yet.&lt;/p&gt;

&lt;p&gt;There’s another step of translation involved, from keycodes to &lt;em&gt;keysyms&lt;/em&gt;.
We need this extra step because pressing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt; key while holding down shift will still only send the scancode &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7&lt;/code&gt;, only that this time we know the shift key is held down because it has sent its own scancode.
This fact of “shift is held down” is encoded in &lt;em&gt;modifier bits&lt;/em&gt; that the computer keeps track of.
Pressing shift will set the “shift” modifier bit to 1, releasing shift will reset it to 0.
(We’ll get back to that too.)
The translation from keycodes to keysyms takes these modifiers into account, and finally produces the character 6 for the key &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt; we pressed. (Or it may have produced ^ if shift was held. Or ¼ if AltGr and shift were held on us-altgr-intl layout. Or …)&lt;/p&gt;

&lt;p&gt;We can change how either translation operates to remap keys, and both have their advantages and disadvantages.&lt;/p&gt;

&lt;h2 id=&quot;i-know-that-code&quot;&gt;I know that code&lt;/h2&gt;

&lt;p&gt;Possibly the easiest way to remap a key is to ignore that anything other than X11 exists, and remap it using a modified xkb layout.
This obviously has the downside of ignoring that anything other than X11 exists, but for this particular case it’s not too bad.
Never used the windows key on a virtual console anyway.&lt;/p&gt;

&lt;p&gt;First we’ll need a &lt;em&gt;description&lt;/em&gt; of the current layout.
We’ll get that from the X server by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setxkbmap -print&lt;/code&gt;, and for this machine it gives&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xkb_keymap {
	xkb_keycodes  { include &quot;evdev+aliases(qwerty)&quot;	};
	xkb_types     { include &quot;complete&quot;	};
	xkb_compat    { include &quot;complete&quot;	};
	xkb_symbols   { include &quot;pc+us(altgr-intl)+inet(evdev)+ctrl(nocaps)&quot;	};
	xkb_geometry  { include &quot;pc(pc104)&quot;	};
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xev&lt;/code&gt; and press what’s left of the windows key, we’ll see that it has its own keycode and keysym: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;133&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Super_L&lt;/code&gt;, respectively.
Pressing this key sets the state bit for Mod4, releasing it resets that bit.
If that sounds a lot like Shift acts, that’s because both in fact use the same mechanism.
Pressing Shift does not directly set a state bit, instead it is translated (ignoring &lt;em&gt;all&lt;/em&gt; state bits) to its own keysym, and &lt;em&gt;that&lt;/em&gt; keysym then sets the state bit when the X server processes it.
Likewise letting go of the key will clear the bit when the X server processes the corresponding event for that.&lt;/p&gt;

&lt;p&gt;Super, or Mod4, already has a keysym associated that does all the modifier bit handling.
This means that all we have to do is change the layout a tiny bit such that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&amp;lt;|&lt;/code&gt; key always translates to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Super_L&lt;/code&gt; keysym instead.
Since this is about symbols, we’ll add a line to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xkb_symbols&lt;/code&gt; block:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xkb_keymap {
	xkb_keycodes  { include &quot;evdev+aliases(qwerty)&quot; };
	xkb_types     { include &quot;complete&quot;      };
	xkb_compat    { include &quot;complete&quot;      };
	xkb_symbols   {
		include &quot;pc+us(altgr-intl)+inet(evdev)+ctrl(nocaps)&quot;
		key &amp;lt;LSGT&amp;gt; { [ Super_L, Super_L, Super_L, Super_L ] };
	};
	xkb_geometry  { include &quot;pc(pc104)&quot;     };
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;LSGT&amp;gt;&lt;/code&gt; is the X11 internal name for the 102nd key.
Now there’s four occurences of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Super_L&lt;/code&gt;.
As we’ve noted earlier each keycode can translate to different keysyms, depending on which modifier bits are set.
The list here is what does that: the first element is used when no modifier bits are set, the second is used when shift is set, the third is for AltGr, and the fourth is for AltGr and shift simultaneously.
Since we want the key to ignore all of these we translate it to the same keysym regardless of modifiers.&lt;/p&gt;

&lt;p&gt;This description can now be saved to a file (say, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exploded.xkb&lt;/code&gt;) and loaded into the X server with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xkbcomp exploded.xkb $DISPLAY&lt;/code&gt;.
And voilà: the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&amp;lt;|&lt;/code&gt; key functions like the windows did before its failed launch attempt.
Success!&lt;/p&gt;

&lt;p&gt;Unfortunately changing the X keyboard layout, as already mentioned, does not work very well outside of X.
It also doesn’t work very well with USB keyboards, because those will use the same layout—and maybe we want to use that key for something different there?
There are ways around both of those problems, but ideally we’d remap that one key only on that one exact keyboard.&lt;/p&gt;

&lt;p&gt;Changing the X11 keyboard layout messed with the translation of keycodes to keysyms.
To affect everything on the system, even stuff that’s not run inside X, we’re better off messing with the translation from scancodes to keycodes instead.
And because that translation is done by the kernel, and the kernel has easy access to hardware information, we can even do it for just the broken keyboard.
Sadly it also looks a lot more arcane, but it really isn’t!&lt;/p&gt;

&lt;h2 id=&quot;this-doesnt-scan-though&quot;&gt;This doesn’t scan though&lt;/h2&gt;

&lt;p&gt;Translation of scancodes to keycodes goes through the kernel, and the translation can be modified by adding entries to the udev hardware database.
This database is made up of a bunch of files with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.hwdb&lt;/code&gt; extension and usually resides in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/udev/hwdb.d&lt;/code&gt;, but that can be different on different systems.&lt;/p&gt;

&lt;p&gt;hwdb files consist of a number of blocks, each containing a &lt;em&gt;match pattern&lt;/em&gt; that determines to which hardware devices to block applies, and a number of indented key-value pairs that describe properties of said device.
We’ll use this to change the translation from scancodes to keycodes, which is conveniently done by assigning to a device a key-value pair like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;KEYBOARD_KEY_07=z&lt;/code&gt;.
This particular example would translate the key &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt; to the keycode that is in the US layout translated to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt;.
(If this sounds like an odd way to write it, then welcome to the world of hardware specifications.)&lt;/p&gt;

&lt;p&gt;To write a block we will of course need to know two things: which hardware device we’re talking about, and what the scancode of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&amp;lt;|&lt;/code&gt; key is.
Getting the hardware id of the keyboard is most easily done with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;evemu-describe&lt;/code&gt;, which will list all input devices and ask which to describe.
We only have one keyboard attached and it is described as an AT keyboard with identifier &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dmi:bvnLENOVO:...&lt;/code&gt;, and since it’s unlikely that we’ll ever find another Lenovo AT keyboard to plug into this thing we’ll just use the simplest match pattern: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;evdev:atkbd:dmi:bvnLENOVO:*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next for the scancode.
We’ll get this one out of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;evtest&lt;/code&gt;, which also lists all devices and asks for which to read from.
Using the same keyboard as the source and pressing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&amp;lt;|&lt;/code&gt; key, we get the scancode 56:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Event: time 1628119925.557511, type 4 (EV_MSC), code 4 (MSC_SCAN), value 56
Event: time 1628119925.557511, type 1 (EV_KEY), code 86 (KEY_102ND), value 0
Event: time 1628119925.557511, -------------- SYN_REPORT ------------
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The scancode given here is already in hexadecimal as expected by hwdb, so we can just copy it over.
Also, see that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;KEY_102ND&lt;/code&gt; in there?
Yeah, that’s the name of that key.
Because it’s the 102nd key on the keyboard and doesn’t have any function on the US keyboards, what for not being a thing there.
Brilliant naming, right?&lt;/p&gt;

&lt;p&gt;So we end up with a hwdb file that looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;evdev:atkbd:dmi:bvnLENOVO:*
 KEYBOARD_KEY_56=leftmeta
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leftmeta&lt;/code&gt; is the name for the windows key.
The &lt;a href=&quot;https://hal.freedesktop.org/quirk/quirk-keymap-list.txt&quot;&gt;list of all key names&lt;/a&gt; is available, and there are quite a few unexpected entries in there.
(Ever needed a bass boost key on your computer? We got you covered!)&lt;/p&gt;

&lt;p&gt;Now it’s just a matter of saving this file, running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udevadm trigger&lt;/code&gt; to reload the translation table, and we have a fix that works outside of X.&lt;/p&gt;

&lt;p&gt;Neat!&lt;/p&gt;
</description>
				<pubDate>Wed, 04 Aug 2021 00:00:00 +0200</pubDate>
				<link>https://eno.space/blog/2021/08/Keyboard-to-launch-control</link>
				<guid isPermaLink="true">https://eno.space/blog/2021/08/Keyboard-to-launch-control</guid>
			</item>
		
			<item>
				<title>Adventures in 4k video editing</title>
				<description>&lt;p&gt;Recently we got access to a camera that records reasonable 4k video at 10bit and 12bit color depth.
So naturally we now need a way to &lt;em&gt;edit&lt;/em&gt; this 4k video.
While proprietary software like &lt;a href=&quot;https://www.blackmagicdesign.com/products/davinciresolve/&quot;&gt;DaVinci Resolve&lt;/a&gt; does have free variants that could be useful, those free variants often come with unfortunate restrictions like “no 4k exports” or “no commercial use” (sometimes with questionable definitions of “commercial”).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; this post will not contain much information about editing at all, just a colorful recollection of all the problems had on the way to a 4k editing setup that works reasonably well.
Software is a horrible mess most of the time.
This will be quite a rambling.
So sit comfortable, grab a soothing object and/or foodstuff of your choice, and prepare to be disappointed.&lt;/p&gt;

&lt;!--content--&gt;

&lt;h1 id=&quot;requirements&quot;&gt;Requirements&lt;/h1&gt;

&lt;p&gt;Having the camera isn’t enough to make good videos.
The camera is being had, but it’s not the camera that makes the videos–it’s the person wielding the camera.
Which means that, if the person is not very experienced at camera-wieldage, has only one lens that introduces a lot of distortion into the image, and no stabilization gear except a tripod with a ball head, the videos just won’t come out that great.&lt;/p&gt;

&lt;p&gt;To fix all of that the video editor of choice will have to at least make credible attempts at fixing each of those: lens distortion correction is a must, and camera stabilization is much appreciated if it exists.
Can’t do much about not knowing what we’re doing, but that’ll come with time.
(touch wood)&lt;/p&gt;

&lt;h1 id=&quot;humble-beginnings&quot;&gt;Humble beginnings&lt;/h1&gt;

&lt;p&gt;Running on Linux, the first choice for video editing was (of course) &lt;a href=&quot;https://kdenlive.org&quot;&gt;Kdenlive&lt;/a&gt;.
Kdenlive does support lens distortion correction and camera stabilization with its many, &lt;em&gt;many&lt;/em&gt; video filters.
So that’s good.&lt;/p&gt;

&lt;p&gt;Now Kdenlive hasn’t implemented these filters from scratch, it uses a filter framework called &lt;a href=&quot;https://frei0r.dyne.org/&quot;&gt;frei0r&lt;/a&gt; to do the heavy lifting.
Sadly the frei0r lens correction filter uses a rather old quadratic algorithm that doesn’t reflect the real-world behavior of lenses very well, and it doesn’t do any interpolation either.
Together these two mean that it can be impossible to set up the filter in such a way that distortion mostly vanishes without introducing a lot of discrete jumps at the borders of the image.
So … Kdenlive lens correction isn’t really up to it, but maybe that’s not a big deal in all.
Moving on.&lt;/p&gt;

&lt;p&gt;Unfortunately stabilization doesn’t fare much better.
There’s no way to set control points to stabilize &lt;em&gt;on&lt;/em&gt;, instead the filter attempts to keep the image completely stable apart from some guesses at panning the camera operator may have done.
For cameras that sit on a shaky tripod this works.
For everything else it doesn’t.&lt;/p&gt;

&lt;p&gt;Oh, and Kdenlive repeatedly crashes trying to edit 4k projects because it runs out of memory.&lt;/p&gt;

&lt;p&gt;We’re going to need a bigger &lt;s&gt;boa&lt;/s&gt;editor.&lt;/p&gt;

&lt;h1 id=&quot;will-it-blend&quot;&gt;Will it blend?&lt;/h1&gt;

&lt;p&gt;There are a lot of other video editors out there, even when excluding proprietary offerings as mentioned before.
&lt;a href=&quot;https://jliljebl.github.io/flowblade/&quot;&gt;Flowblade&lt;/a&gt; and &lt;a href=&quot;https://shotcut.org/&quot;&gt;Shotcut&lt;/a&gt; use the same filter system Kdenlive uses, so they’re out.
&lt;a href=&quot;http://pitivi.org/&quot;&gt;Pitivi&lt;/a&gt; can’t even load the files the camera creates.
&lt;a href=&quot;https://cinelerra-gg.org&quot;&gt;Cinelerra&lt;/a&gt; isn’t packaged on Arch and bothering with custom builds for a video editor is out of scope for now.
It also seems to use ffmpeg for lens distortion correction, and ffmpeg uses the same (unsuitable) algorithm seen before.
Others don’t even &lt;em&gt;have&lt;/em&gt; distortion correction or stabilization.&lt;/p&gt;

&lt;p&gt;That leaves … &lt;a href=&quot;https://www.blender.org/&quot;&gt;Blender&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Yup, Blender includes a video editor.
The lens correction it does is &lt;em&gt;pretty good&lt;/em&gt;, if a bit fiddly to configure.
It uses a similar method to that found in cameras themselves and in photo processing suites like &lt;a href=&quot;https://www.darktable.org/&quot;&gt;Darktable&lt;/a&gt;, i.e. once configured the results are very good.
Stabilization exists as well, and can be tuned to within an inch of its life.
Objects to stabilize on can be set, pan and zoom motions can be configured and will be rendered correctly.
It’s Nice!™&lt;/p&gt;

&lt;p&gt;So all is well now, right?
Well.
Spoiler alter: endless disappointment is the penance of the optimist.&lt;/p&gt;

&lt;h1 id=&quot;grade-needs-improvement&quot;&gt;Grade: Needs Improvement&lt;/h1&gt;

&lt;p&gt;So let’s load our clips into Blender, fix lens distortion, stabilize the camera, add some effects (bleh there’s not a lot of them, but it’ll do!), do some color grading with that nice 10 bit color depth on the input …&lt;/p&gt;

&lt;p&gt;Oh.
Oh no.
Is that … &lt;a href=&quot;https://en.wikipedia.org/wiki/Colour_banding&quot;&gt;&lt;em&gt;banding&lt;/em&gt;&lt;/a&gt;, in the sky?
10 bit input should mean we can grade this pretty harshly and still not introduce any banding!
Surely it can’t be.
But let’s experiment, let’s expand the sky some more an see.
Let’s take, oh, 2% of the color space and expand to 100%, on some part of the sky.
We should see at least 20 different values of color now, with 10 bit inputs and using only 2% of them.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/blog/images/4k-video/banding.png&quot; alt=&quot;Screenshot of the blender luma waveform view. Five distinct luminance values per color channel are shown.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Except we don’t.
We see five different values, not 20, which suggests that video is handled with 8 bit color instead of 10 bit.
A quick chat with the developers confirms this: video is decoded with 8bit color regardless of what the input file is, and is likewise encoded at 8bit during rendering even when the rendered image exceeds that.
Image sequences should work as expected though, the 8bitness of video seen here is just a limitation of the video path.&lt;/p&gt;

&lt;p&gt;So let’s try that.
Convert the video from just now to an image sequence with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ffmpeg -i input.mov '%08d.png'&lt;/code&gt;, load it into blender as an image sequence instead of as a movie, and repeat the same process.
Aaaand–much better!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/blog/images/4k-video/less-banding.png&quot; alt=&quot;Screenshot of the blender luma waveform view. Distinct values can be made out, but there are at least 20 of them now.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So converting videos to image sequences solves all the problems.
Blender will process the sequences as expected, rendering to image sequences is fully supported, and ffmpeg can even turn an image sequence into a video.
Sunshine and unicorns, right?
Technically the problem is solved now.&lt;/p&gt;

&lt;h1 id=&quot;but-video-compression-exists-for-a-reason&quot;&gt;But video compression exists for a reason&lt;/h1&gt;

&lt;p&gt;Unfortunately solving the problem has not made a problem go away, it has merely transformed one problem into another problem.
That new problem is disk space, are more precisely, lack of sufficient thereof.
At 4k resolution with 16 bit colors (because image formats don’t support 10 bit or 12 bit well) each frame is over 30 megabytes in size, even with maximum compression applied.
That makes about one gigabyte &lt;em&gt;per second&lt;/em&gt; of video, or around 3.5 &lt;em&gt;terabytes&lt;/em&gt; per hour.&lt;/p&gt;

&lt;p&gt;Editing large projects at these sizes requires a lot of disks, and that gets expensive very quickly.
Disks also have limited read and write speeds, so even using very fast disks that can read and write at 200MB/s we won’t get any decent frame rate out of the system.
Converting videos to images will also take quite a while, easily three times as long as the input videos would play for.
And the rendered video will need as much space again.&lt;/p&gt;

&lt;p&gt;This is what we in the industry call “a bit of a problem”.
Luckily we are by now well versed in turning one problem into another problem, and there’s &lt;em&gt;just the thing&lt;/em&gt; for that just on the doorstep.&lt;/p&gt;

&lt;h1 id=&quot;on-a-short-fuse&quot;&gt;On a short FUSE&lt;/h1&gt;

&lt;p&gt;That something is &lt;a href=&quot;https://github.com/libfuse/libfuse&quot;&gt;FUSE&lt;/a&gt;, a library that lets an application create a virtual file system.
This virtual file system will look like the real thing for any application in the system–like, say, Blender.
There’s also &lt;a href=&quot;https://ffmpeg.org/&quot;&gt;ffmpeg&lt;/a&gt;, a library that can read and decode many formats of multimedia files, including the 10 bit files written by the camera.
Combining those two alleviates the need to store all decoded frames on disk–instead we’ll simply decode the frame Blender accesses when it accesses it, and when we’re done decoding we throw the decoded frame away.
FUSE ensure that Blender gets the data, but the data is never stored anywhere except in system memory.&lt;/p&gt;

&lt;p&gt;Perfect!&lt;/p&gt;

&lt;p&gt;Except that FUSE is a C library.
And ffmpeg is a C library.
And for any language that’s reasonably fun to use at least one of them doesn’t have a proper binding.
So this virtual file system will have to be written in C, or something much like it.&lt;/p&gt;

&lt;h1 id=&quot;and-what-a-rough-c-it-be&quot;&gt;And what a rough C it be&lt;/h1&gt;

&lt;p&gt;It didn’t end up being C, obviously.
C is a relic of a century long past and has no right to exist any more, and yet it carries on.
That leaves only C++, which is its own kind of unfortunate.
Not because C++ is a horrible language (even though it definitely is a horrible language), but because ffmpeg doesn’t have useful C++ bindings either except those that have long since been abandoned, or those that only work on Windows.
Which means writing a binding from scratch, and luckily it doesn’t have to be complete by any means.
Only just enough to open a video, grab some frames from it, and convert them to some image format.&lt;/p&gt;

&lt;p&gt;That should be easy!&lt;/p&gt;

&lt;h3 id=&quot;17th-century-architecture&quot;&gt;17th century architecture&lt;/h3&gt;

&lt;p&gt;It wasn’t easy.
Resource management in ffmpeg is too complicated to go into here, but suffice it to say that it is even more baroque than resource management in C usually is.
And resource management in C is the kind of baroque that turns little mistakes like forgetting to add one to a value in the program into problems like ransomware.&lt;/p&gt;

&lt;p&gt;It was not fun, but eventually it did work correctly.&lt;/p&gt;

&lt;h1 id=&quot;finding-frames&quot;&gt;Finding frames&lt;/h1&gt;

&lt;p&gt;Two properties video players usually have that this file system will lack are linearity and seek inaccuracy.
Video players usually play a video from start to finish without jumping around in it.
This file system will jump around all the time to provide just the frame some application has asked for.
Video players have fast-forward and rewind buttons that skip forward or back by some number of seconds, but usually not the &lt;em&gt;exact&lt;/em&gt; number of seconds written on the button.
This file system will have to jump around to &lt;em&gt;the exact frame&lt;/em&gt; an application asks for.&lt;/p&gt;

&lt;p&gt;Now video codecs are usually designed to suit video players that will simply play a video, and when they &lt;em&gt;do&lt;/em&gt; seek they don’t need to hit an exact frame.
Just something close to the frame they went for.
(There are codecs are designed to make such exact seeks easy, but we’ll not discuss them here.)&lt;/p&gt;

&lt;p&gt;The reason for this is the same reason that makes video files much smaller than sequences of still images:
video codecs don’t compress each frame of the video individually as still images.
Instead they compress a few frames as still images (maybe one every few seconds).
For the following frames they try to predict how previous frames would change, and only store whatever information is needed to fix up anything they predicted incorrectly.
Since video often moves, and seldom moves very fast or in many different directions at once, this approach works very well.
It also means that a decoder can only jump to individually compressed frames, not into the much smaller predicted frames–if you’ve ever seen video artifacts like objects melting into a blocky mess, that’s what happens when a decoder seeks into predicted frames without having the reference image.&lt;/p&gt;

&lt;p&gt;Luckily this can worked around by seeking to the nearest possible frame, then dropping all frames until the requested frame comes around!&lt;/p&gt;

&lt;h1 id=&quot;this-aint-my-first-time&quot;&gt;This ain’t my first time&lt;/h1&gt;

&lt;p&gt;Many video formats store not just individual frames, but also the time at which each frame should appear on screen.
(A notable exception to this seems to be AVI, which doesn’t store such timestamps. Why.)&lt;/p&gt;

&lt;p&gt;Seeking to a specific frame should then be no more complicated than calculating the timestamp of that frame, seeking to the nearest frame we can find, and then decode frames until we arrive at the timestamp we just calculated.
Usually this will work, but (unbeknownst to many) video files do not have a fixed frame rate.
In many video formats it is perfectly legal to state that a video runs at 30 frames per second, but &lt;em&gt;this specific frame here&lt;/em&gt; will stay on screen for 2.1 seconds before the next frame is shown.
In this case we could return the same frame 63 times, which is easy enough.&lt;/p&gt;

&lt;p&gt;It is however also possible to show a frame for &lt;em&gt;far less&lt;/em&gt; than one normal frame duration.
Naturally this makes seeking to an exact frame rather difficult, since now we may have more than one image in the time of one frame.&lt;/p&gt;

&lt;p&gt;Easy enough to just forbid that and report an error when an instance of such an unexpected jump in time is found, so on we go to convert the frames we find to images and return them.&lt;/p&gt;

&lt;h1 id=&quot;spitting-images&quot;&gt;Spitting images&lt;/h1&gt;

&lt;p&gt;Fortunately ffmpeg cannot only decode videos, but encode them as well.
Some of these options are, as mentioned earlier, sequences of PNG or TIF files.&lt;/p&gt;

&lt;p&gt;Unfortunately ffmpeg cannot export uncompressed PNG images (but compressing them would be a waste of time here), and the TIF exporter is very slow even when exporting to uncompressed images.
Copying the exported image is three times as fast as creating that image in the first place, even though the image contains only a few hundred bytes of metadata followed by the raw data of the frame.
It really shouldn’t take that long to create that image, in fact it should be nearly free!&lt;/p&gt;

&lt;p&gt;Which (of course) means that a custom TIF exporter is necessary.
Writing one is easy enough, the &lt;a href=&quot;https://www.adobe.io/open/standards/TIFF.html&quot;&gt;TIFF specification&lt;/a&gt; is freely available and creating TIF files is not complicated for the purposes found here.&lt;/p&gt;

&lt;p&gt;Okay, now we have access to high bit-depth images in blender, and we can edit!
Finally!&lt;/p&gt;

&lt;p&gt;… But it’s slow.
Like, really slow.
Only four frames per second on this machine, at best.&lt;/p&gt;

&lt;h1 id=&quot;this-doesnt-look-right&quot;&gt;This doesn’t look right&lt;/h1&gt;

&lt;p&gt;For cases like these Blender supports something called &lt;em&gt;proxies&lt;/em&gt;.
Instead of working with the full input data all the time it’ll use a much smaller &lt;em&gt;proxy&lt;/em&gt; file with lower resolution, enabling real-time playback and editing of the project even when the machine can’t handle the full input at that rate.
For videos the proxy file would be video encoded at lower resolution, for image sequences (like created here) the proxy is a folder with JPG images crated at lower resolution and lower quality.&lt;/p&gt;

&lt;p&gt;The file system has to generate these folders, and when Blender reads from these folders it’ll have to take the frames from a previously created proxy clip instead.
That’s easy enough, it’s just more of the same.&lt;/p&gt;

&lt;p&gt;Except …
For image sequences blender requires at least &lt;em&gt;two&lt;/em&gt; proxy folders, one for the main video editor and one for the part that does image stabilization.
(Remember that? That was important before we got sidetracked 20 levels deep.)
But well, it’s still more of the same.&lt;/p&gt;

&lt;p&gt;Except …
Blender does not deal well when the proxy images it reads aren’t 8 bit color.
Color management goes off the rails (everything turns very dark suddenly), and it’s not much faster than reading the full input file directly either.
That’s easy enough to fix at least, just encode the proxy clips with 8 bit colors instead of 10 or 12 and return them appropriately.&lt;/p&gt;

&lt;p&gt;Except …
So far these precreated proxies are for the videos from the camera, but maybe there’s been some lens distortion correction applied (remember that?).
For these corrected clips Blender supports proxies as well, and they live in the virtual file system we’re building … But Blender can’t write to that, because what would that even &lt;em&gt;mean&lt;/em&gt;?
That’s easy enough to fix at least, we’ll just put a symbolic link where Blender would put the folder, and redirect it to where our precreated proxies live.
It’s not very efficient like that, but proxies are small enough that it’s no big deal.&lt;/p&gt;

&lt;h1 id=&quot;except-&quot;&gt;Except …&lt;/h1&gt;

&lt;p&gt;Except nothing.
That’s it!
It’s done!
It finally works!&lt;/p&gt;

&lt;p&gt;We can do 4k editing, with proper color grading, without unsightly artifacts, in real-time.
The adventure is over!&lt;/p&gt;

&lt;p&gt;Well, mostly.
But that’s a story for another time.&lt;/p&gt;
</description>
				<pubDate>Tue, 20 Oct 2020 00:00:00 +0200</pubDate>
				<link>https://eno.space/blog/2020/10/4k-video</link>
				<guid isPermaLink="true">https://eno.space/blog/2020/10/4k-video</guid>
			</item>
		
			<item>
				<title>(Almost) upstream boot for Pinebook Pro</title>
				<description>&lt;h1 id=&quot;why-pray-tell&quot;&gt;Why, pray tell?&lt;/h1&gt;

&lt;p&gt;The &lt;a href=&quot;https://www.pine64.org/pinebook-pro/&quot;&gt;Pinebook Pro&lt;/a&gt; is a pretty nifty device.
A 200$ laptop.
With an RK3399 SoC in it, a hex-core AArch64 chip from Rockchip.
That’s just &lt;em&gt;amazing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The default OS it ships with is a Debian with a vendor kernel and U-Boot provided by Rockchip.
For the shipped Debian this bootloader works well, but other OSes may have some problems.
In the case we’re adressing here it is the Manjaro kernel booting very slowly, caused by the Rockchip bootloader not bringing up all CPU cores of the chip properly.
There are a few workarounds for this (booting with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nr_cpus=4&lt;/code&gt; for example), but all of them have their drawbacks.
Also, booting with an (almost) fully upstream bootloader is just, you know, &lt;em&gt;more satisfying&lt;/em&gt;.&lt;/p&gt;

&lt;!--content--&gt;

&lt;h1 id=&quot;procedure-overview-of&quot;&gt;Procedure, overview of&lt;/h1&gt;

&lt;p&gt;The bootloader on the pinebook consist of two main parts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ARM-software/arm-trusted-firmware&quot;&gt;ATF&lt;/a&gt;, the ARM Trusted Firmware, which provides platform functionality like power managemt of CPU cores, DDR frequency management, and other very low-level things that happen &lt;em&gt;on the SoC itself&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.denx.de/wiki/U-Boot/WebHome&quot;&gt;U-Boot&lt;/a&gt;, which gets the ATF into the chip and actually boots the rest of the system. ATF itself cannot interact with the rest of the board and thus boot anything, or even turn on peripherals required for boot. U-Boot will do that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will have to build both of these. ATF needs no patches to work, although suspend/resume does not work at the time of writing.
U-Boot needs a few patches to properly detect the Pinebook Pro hardware.&lt;/p&gt;

&lt;h1 id=&quot;preparation&quot;&gt;Preparation&lt;/h1&gt;

&lt;p&gt;This guide assumes that all compilation is done on a Pinebook Pro, running a recent Manjaro image.&lt;/p&gt;

&lt;p&gt;First, install some tools necessary to build ATF and U-Boot.
We will need the aarch64 gcc, a 32 bit arm gcc for some parts of ATF, and the device tree compiler for U-Boot:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;pacman &lt;span class=&quot;nt&quot;&gt;-S&lt;/span&gt; bc base-devel arm-none-eabi-gcc dtc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then clone both ATF and U-Boot. We will use upstream ATF and a patched version of U-Boot that is very close to mainline.
ATF is locked to a specific version here because we know it works, aside from the known bugs such as suspend not working.&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git clone https://github.com/ARM-software/arm-trusted-firmware.git atf
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; atf checkout 22d12c4148c373932a7a81e5d1c59a767e143ac2
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git clone https://git.eno.space/pbp-uboot.git uboot
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The U-Boot repository we’ve checked out comes with all patches required and builds to something we may consider reasonable: boot order is SD first, USB second, eMMC third. The boot delay is short, and U-Boot will light the Pinebook LEDs early in the boot process to signal liveness of the Pinebook.&lt;/p&gt;

&lt;h1 id=&quot;building-and-installation&quot;&gt;Building and Installation&lt;/h1&gt;

&lt;p&gt;Building ATF is very simple, but requires custom &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CFLAGS&lt;/code&gt; to build reliably on some machines.
Yours may not need the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CFLAGS&lt;/code&gt;, omitting them does no harm if the compilation succeeds.&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; atf &lt;span class=&quot;nt&quot;&gt;-j8&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PLAT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;rk3399 &lt;span class=&quot;nv&quot;&gt;CFLAGS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;‘-gdwarf-2’
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Building on other platforms will require setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CROSS_COMPILE&lt;/code&gt; and possibly &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M0_CROSS_COMPILE&lt;/code&gt;, depending on the distribution you’re working with.&lt;/p&gt;

&lt;p&gt;U-Boot requires a little more, but not much. It also requires the path of the ATF binary we built in the previous run.&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; uboot &lt;span class=&quot;nt&quot;&gt;-j8&lt;/span&gt; pinebook_pro-rk3399_defconfig
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; uboot &lt;span class=&quot;nt&quot;&gt;-j8&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;BL31&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;../atf/build/rk3399/release/bl31/bl31.elf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will produce two binaries, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uboot/idbloader.img&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u-boot.itb&lt;/code&gt;, that have to be written to eMMC or SD card.
We will write them to eMMC here.&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo dd &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;uboot/idbloader.img &lt;span class=&quot;nv&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/dev/mmcblk2 &lt;span class=&quot;nv&quot;&gt;seek&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;64
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo dd &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;uboot/u-boot.itb &lt;span class=&quot;nv&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/dev/mmcblk2 &lt;span class=&quot;nv&quot;&gt;seek&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;16384
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now reboot your Pinebook, and you will be running with an (almost) upstream bootloader.
Like the Rockchip bootloader used by the stock Debian install and many other distributions, this U-Boot supports &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/boot.scr&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/boot/boot.scr&lt;/code&gt; scripts on the first partition of a storage device.&lt;/p&gt;
</description>
				<pubDate>Sat, 11 Jan 2020 00:00:00 +0100</pubDate>
				<link>https://eno.space/blog/2020/01/pbp-uboot</link>
				<guid isPermaLink="true">https://eno.space/blog/2020/01/pbp-uboot</guid>
			</item>
		
			<item>
				<title>A Bucket Of Sand</title>
				<description>&lt;p&gt;Recently I wanted to deploy a service of marginal trustworthiness, namely &lt;a href=&quot;https://github.com/matrix-org/synapse&quot;&gt;synapse&lt;/a&gt;, the reference server application for the &lt;a href=&quot;https://matrix.org&quot;&gt;Matrix&lt;/a&gt; protocol.
More precisely, I wanted to deploy synapse on boxes I &lt;em&gt;already had&lt;/em&gt; for economic reasons.
Since I do consider it not fully trustworthy (it being network-facing and monolithic and all) there were some safeguards that needed taking.
Among other things I wanted to be sure that synapse would not interfere with other services on the same box in ways it could not have done had it lived on a separate machine.&lt;/p&gt;

&lt;p&gt;“Great!”, I hear the internet say, “Just dockerize it and everything will be fine!”.
If only it were that simple.&lt;/p&gt;

&lt;!--content--&gt;

&lt;h1 id=&quot;containers-containers&quot;&gt;Containers, Containers&lt;/h1&gt;

&lt;p&gt;Docker does work for exactly this purpose.
It works rather well for this purpose, too.
Unfortunately it didn’t quite do everything I wanted it to do in this case.
Synapse, being an application that is nowhere near complete, still in active development, and open to the entire internet, cannot (and should not) be considered “secure”.
Regular updates are absolutely essential for secure, or at least secure enough, operation of any network-facing service.
And regular updates are the first thing docker let me down at.
Unless you run your containers from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latest&lt;/code&gt; tag on the hub and regularly pull and restart it, you will over time end up with out-of-date images.&lt;/p&gt;

&lt;p&gt;Automatic updates of this kind are fine for many things, but they do take control out of your hands.
They also require trust in the docker hub, which is another thing I decidedly do not have.
(Building images from scratch and deploying those after testing is fine, but building images without the hub is a serious pain with docker. Rkt is a bit easier, but since IPv6 is something rkt has, at best, only heard of I can’t consider using it.)&lt;/p&gt;

&lt;h1 id=&quot;what-is-a-container&quot;&gt;What is a container?&lt;/h1&gt;

&lt;p&gt;With docker gone, rkt not even in the competition, and running it on a VM out of the question for a variety of reasons, another solution had to be found.
Docker, for all its faults, does very well on the containerization side side of my requirements.
So the question that begs to be asked is, what is a container anyway?&lt;/p&gt;

&lt;p&gt;Most importantly a container isolates its contents from the host the container is running on.
For the sake of argument, let us adopt a non-interference style of definition for what a container is.
Docker guarantees a number of intereferences the container cannot produce:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;it cannot write to the filesystem of the host, unless this is explicitly allowed&lt;/li&gt;
  &lt;li&gt;it can’t even read the filesystem of the host. It has its entirely separate filesystem to work on.&lt;/li&gt;
  &lt;li&gt;it cannot access the host via localhost networking&lt;/li&gt;
  &lt;li&gt;processes on the host remain hidden to the container&lt;/li&gt;
  &lt;li&gt;shared memory, inter-process communication and other kernel resources are not shared between container and host&lt;/li&gt;
  &lt;li&gt;and a number of other things, mostly resource limits and capabilities of root containers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ideally we want to keep all of these different isolations, but sadly we can’t.
Providing all of them requires a full container solution like docker, rkt, or any of the others.
Or does it?
Let’s check, one by one.&lt;/p&gt;

&lt;h3 id=&quot;protecting-the-host-filesystem&quot;&gt;Protecting the host filesystem&lt;/h3&gt;

&lt;p&gt;Mount namespaces can be used to give a process or a set of processes an independent view of the filesystem hierarchy.
This allows hiding of host directories in the container, hiding container directories in the host, even separating host and container filesystems completely.
Linux allows us not only to split views on the filesystem and mount &lt;em&gt;directories&lt;/em&gt; differently, but &lt;em&gt;files&lt;/em&gt; as well.
Combining these two we can build a completely locked-down filesystem for a container in which, for example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp&lt;/code&gt; is not shared with the host, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc&lt;/code&gt; is empty except for the few files a container needs from the host.
We cannot completely isolate host and container without building a completely fresh root filesystem for the container, but we &lt;em&gt;can&lt;/em&gt; make a lot of the host filesystem inaccessible or read-only.&lt;/p&gt;

&lt;p&gt;All of this can be easily done with systemd.
As listed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd.exec(5)&lt;/code&gt;, there are a sizable number of options for manipulating the filesystem hierarchy, most of which are very useful for isolation.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProtectSystem=strict&lt;/code&gt; mounts the entire filesystem hierarchy read-only, except for a few API filesystems.
Some of these can be made read-only with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProtectKernelTunables&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProtectKernelModules&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProtectControlGroups&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProtectHome=true&lt;/code&gt; makes home directories (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/root&lt;/code&gt;) empty to the service. It can also make them read-only instead, or mount a tmpfs instance there.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrivateTmp=true&lt;/code&gt; creates private instances of the system temporary directories (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/tmp&lt;/code&gt;), making communication via tempfiles impossible.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PrivateDevices=true&lt;/code&gt; hides almost all hardware devices from the service.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TemporaryFileSystems=/dev/shm&lt;/code&gt; hides all POSIX shared memory.
Adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc&lt;/code&gt; to the list also hides all system configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;blocking-localhost-networking&quot;&gt;Blocking localhost networking&lt;/h3&gt;

&lt;p&gt;Two solutions exist to protect localhost from a rogue service: the one that uses network namespaces, and the one that doesn’t.
Network namespaces are the analog of mount namespaces for the network stack: different network namespaces can see a completely different picture of what “network” even means.
Among other things this leads to a completely different view of what “localhost” means, since loopback devices (and the localhost addresses with them) are not shared between namespaces.
This is the solution commonly used by containers unless told otherwise: create a network namespace, add a virtual ethernet link from the host to container, and use network address translation on the host.&lt;/p&gt;

&lt;p&gt;The other solution involves not network namespaces, but control groups.
One very interesting feature of control groups is that the netfilter subsystem (most usefully the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables&lt;/code&gt; family) can filter packets depending on which control group sent them.
If we can stick our service into a control group we can thus use iptables filters to drop all packets to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; we do not want to see.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# assuming that our service lives in cgroup service.group:&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# create a new filter chain for just our service&lt;/span&gt;
ip6tables &lt;span class=&quot;nt&quot;&gt;-N&lt;/span&gt; service-group-filter
&lt;span class=&quot;c&quot;&gt;# block all packets to localhost, allow all others&lt;/span&gt;
ip6tables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; service-group-filter &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; ::1 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; REJECT
ip6tables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; service-group-filter &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT
&lt;span class=&quot;c&quot;&gt;# hook the filter into the OUTPUT chain&lt;/span&gt;
ip6tables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; OUTPUT &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; cgroup &lt;span class=&quot;nt&quot;&gt;--path&lt;/span&gt; service.group &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; service-group-filter

&lt;span class=&quot;c&quot;&gt;# the same can be done for legacy IP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Systemd already places each service in a control group, and can be instructed to place it in a special control group as well.
(Unfortunately, systemd slice paths are ridiculously verbose.)&lt;/p&gt;

&lt;p&gt;This script can be called from a systemd unit on startup, for example with a drop-in:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# /etc/systemd/system/synapse.service.d/iptables.conf
[Service]
Slice = system-sandbox-synapse.slice

ExecStartPre = -+/usr/bin/ip6tables -N synapse.service-sandbox
ExecStartPre =  +/usr/bin/ip6tables -A synapse.service-sandbox -d &quot;::1&quot; -j REJECT
ExecStartPre =  +/usr/bin/ip6tables -A synapse.service-sandbox -j ACCEPT
ExecStartPre =  +/usr/bin/ip6tables -A OUTPUT -m cgroup --path system.slice/system-sandbox.slice/system-sandbox-synapse.service.slice -j synapse.service-sandbox
ExecStopPost = -+/usr/bin/ip6tables -D OUTPUT -m cgroup --path system.slice/system-sandbox.slice/system-sandbox-synapse.service.slice -j synapse.service-sandbox
ExecStopPost = -+/usr/bin/ip6tables -F synapse.service-sandbox
ExecStopPost = -+/usr/bin/ip6tables -X synapse.service-sandbox

# same can be done for legacy IP
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;hiding-all-processes&quot;&gt;Hiding all processes&lt;/h3&gt;

&lt;p&gt;PID namespaces provide an independent view of the process tree.
In a PID namespace all processes see their PIDs as though they were alone on the machine, while the parent namespace sees them as normal processes in its own tree.
Each PID namespace must have an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; process, i.e. a process with PID 1.
This init process is treated specially by the kernel, so it cannot be a part of our service.&lt;/p&gt;

&lt;p&gt;Systemd offers nothing to make PID namespaces happen.
The rest of the system does though.&lt;/p&gt;

&lt;p&gt;Using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unshare&lt;/code&gt; utility a privileged process can create new namespaces, PID namespaces among them.
After creating those namespaces, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unshare&lt;/code&gt; will call any program with any arguments we provide.
In order to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unshare&lt;/code&gt; we will need a specific set of capabilities we &lt;em&gt;do not&lt;/em&gt; want the service to have.
Luckily &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setpriv&lt;/code&gt; also exists, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unshare&lt;/code&gt; combined with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setpriv&lt;/code&gt; lets us create a new PID namespace in which the service we are sandboxing cannot acquire capabilities needed to break out of the sandbox.&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# /etc/systemd/system/synapse.service.d/exec.conf
[Service]
Type = simple
Group = synapse
ExecStart = \
  /usr/bin/unshare -pf --mount-proc --kill-child=SIGTERM \
    /usr/bin/setpriv --inh-caps=-all --bounding-set=-all \
      --reuid synapse \
    /usr/bin/python2.7 -m synapse.app.homeserver \
      --config-path=/etc/synapse/homeserver.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The service started by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unshare&lt;/code&gt; in this manner will be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; process of the PID namespace.
As such it &lt;strong&gt;must not&lt;/strong&gt; terminate, lest the namespace be torn down and the remaining service processes killed in the process.&lt;/p&gt;

&lt;h3 id=&quot;hiding-ipc-shared-memory-etc&quot;&gt;Hiding IPC, shared memory, etc.&lt;/h3&gt;

&lt;p&gt;POSIX shared memory is already hidden by mounting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/shm/&lt;/code&gt; with a new tmpfs instance using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TemporaryFileSystems&lt;/code&gt;.
SysV IPC facilities and other POSIX mechanisms can also be hidden with IPC namespaces.
Doing that is as simple as turning the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-pf&lt;/code&gt; argument to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unshare&lt;/code&gt; seen earlier into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-ipf&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;capabilities-and-such&quot;&gt;Capabilities and such&lt;/h3&gt;

&lt;p&gt;Most of the changes we have made so far can be undone by a process with enough privileges.
Such privileges can come from being &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;, or they can be obtained from the filesystem by using the capabilities mechanism.
We have already cleared some capability sets while we opened the PID namespace.
There is one more set we can clear however, and a single bit that, when set, will lock our service into its sandbox forever.
This is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NoNewPrivileges&lt;/code&gt; bit, and it does exactly what it says on the tin: once a process has dropped a capability, it can’t get it back.
Ever.&lt;/p&gt;

&lt;p&gt;We might also want to ensure that the kernel keyring is not shared between host and sandbox.&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# /etc/systemd/system/synapse.service.d/caps.conf
[Service]
AmbientCapabilities =
NoNewPrivileges = true
SecureBits =
KeyringMode = private
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;putting-it-all-together&quot;&gt;Putting it all together&lt;/h1&gt;

&lt;p&gt;Combining all of these we can write a single drop-in for synapse that combines all of changes above.
So how well have we done on our list?&lt;/p&gt;

&lt;p&gt;Pretty well, actually.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Synapse cannot write to the filesystem of the host at all now.
We must make some paths writable, or at least readable, for synapse to even start.
These directories can be added with additional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReadWritePaths&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReadOnlyPaths&lt;/code&gt; settings for system directories.
For directories we have turned into an empty tmpfs we’ll have to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BindPaths&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BindReadOnlyPaths&lt;/code&gt; instead, to much the same effect.&lt;/li&gt;
  &lt;li&gt;Localhost networking is completely cut off.
If we want to use a local proxy to handle TLS this will have to be loosened.&lt;/li&gt;
  &lt;li&gt;Host processes are entirely invisible to synapse.&lt;/li&gt;
  &lt;li&gt;IPC facilities are separated.&lt;/li&gt;
  &lt;li&gt;Synapse retains no capabilities.
It can’t send signals to other processes, can’t modify the kernel, can’t trigger reboots, it can’t even write to a tty that might be currently in use–because it can’t see any of them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What we have not (yet!) done is hiding all parts of the host filesystem that isn’t needed for synapse to function, eg the package manager caches or static websites served by our web server.
But we still have the good old UNIX user/group/world permissions (and maybe ACLs) that’ll take care of that.&lt;/p&gt;

&lt;p&gt;If a web server can read it, chances are something on the internet could read it anyway.
Always check your mode bits.&lt;/p&gt;

&lt;h1 id=&quot;further-reading&quot;&gt;Further reading&lt;/h1&gt;

&lt;p&gt;Most systemd options used here are documented (and rather well, too) in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd.exec(5)&lt;/code&gt;.
Many other options that might be useful can be found in other man pages, eg &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd.resource-control(5)&lt;/code&gt; for resource limits.&lt;/p&gt;

&lt;p&gt;Capabilities are described at length in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capabilities(7)&lt;/code&gt;, including a list of all capabilities currently implemented in Linux and how the capability sets work.
The capability system has been described as “confusing”, and that’s a polite description.&lt;/p&gt;

&lt;p&gt;Nearly exhaustive descriptions of which namespaces exist, what they do, and how they interact can be found in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;namespaces(7)&lt;/code&gt; and the pages referenced from there.&lt;/p&gt;

&lt;p&gt;Looking through the iptables documentation, especially the list of extensions, is usually enlightening.
iptables is an incredibly powerful system that has been barely scratched here.&lt;/p&gt;
</description>
				<pubDate>Mon, 17 Sep 2018 00:00:00 +0200</pubDate>
				<link>https://eno.space/blog/2018/09/A-bucket-of-sand</link>
				<guid isPermaLink="true">https://eno.space/blog/2018/09/A-bucket-of-sand</guid>
			</item>
		
			<item>
				<title>Getting A Handle On Things</title>
				<description>&lt;p&gt;Today we will take a very simple intrusive linked list written in Rust and make it safe. Kind of, anyway.&lt;/p&gt;

&lt;!--content--&gt;

&lt;p&gt;Before we start making something safe we need an unsafe thing to make safe.
Let’s not pretend that what we are doing here is the least bit useful, let us instead do it just for the fun of it.
(What we are doing actually &lt;em&gt;is&lt;/em&gt; useful, the explanation of which this margin is too narrow to contain.)&lt;/p&gt;

&lt;p&gt;Any programmer will have come across linked lists at some point.
Linked lists are great because elements can be inserted at any position very quickly, and similarly we can remove elements at any position with very little cost.
Intrusive lists are linked lists as well, but unlike the normal linked lists the Rust stdlib provides an intrusive list stores its links in the elements it contains rather than outside of them.
(Often this kind of thing is done to avoid memory allocations, or for exposition. Linked lists depicted in computer science textbooks for example are &lt;em&gt;always&lt;/em&gt; intrusive.)&lt;/p&gt;

&lt;h2 id=&quot;the-gathering&quot;&gt;The Gathering&lt;/h2&gt;

&lt;p&gt;If we want to make a safe intrusive linked list from scratch, we must first invent &lt;del&gt;the universe&lt;/del&gt;an unsafe linked list.
To do so we need a few requirements to guide our implementation.
Let’s collect a few at random.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt; stores a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u32&lt;/code&gt; value.&lt;/li&gt;
  &lt;li&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt; may be added only to the front of the list.&lt;/li&gt;
  &lt;li&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt; may be removed regardless of its position in the list, discarding the stored value.&lt;/li&gt;
  &lt;li&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt; may be removed from the beginning of the list, returning the stored value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To make this happen we need two structures: one, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt;, for the elements of the list, and another, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt;, for the list itself.
We also need three operations: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; to add an element, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop&lt;/code&gt; to remove an element and return its value, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unlink&lt;/code&gt; to remove an element.&lt;/p&gt;

&lt;p&gt;To make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; possible our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; will need a link to the first element of the list, and our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt; will need a link to its successor.
Unfortunately we cannot use plain references because removing an element from the list would then be impossible–as long as the element is in the list it is borrowed and cannot be mutated.
We also cannot use Cells with references inside because the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; would borrow any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt; for longer than the element itself lives:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;// boom&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We &lt;em&gt;really&lt;/em&gt; want to be able to add elements to a list though rather than replacing the list with an entirely new version (as we would do in functional languages).
References are out, then.
The only thing we have left now are pointers, which are unsafe.
So at least we have the &lt;em&gt;un&lt;/em&gt;safety nailed down already, even though we don’t have any operations yet.
The pointers will have to be &lt;em&gt;mutable&lt;/em&gt; because adding and removing elements mutates their successors in the list.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can now implement the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop&lt;/code&gt; operations, but we can’t implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unlink&lt;/code&gt; yet.
To implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unlink&lt;/code&gt; with this setup we need to know which collection the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Element&lt;/code&gt; is part of, search for the element, and then remove it from its position using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;head&lt;/code&gt; links.
This is rather inefficient, so we would like to do better.
We can do better if every element knows not only the next element in the list, but also knows which &lt;em&gt;link&lt;/em&gt; leads to itself from the previous element or the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt; instance, whichever it might be.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;With &lt;em&gt;this&lt;/em&gt; setup we can now implement all three operations.
All three are rather simple, moving only a bit of data around between pointers.
We have no idea how long any element of the list will live, or even the list itself will live, thus all our operations must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unsafe&lt;/code&gt;.
Nevertheless we can apply some codpiece engineering (“If you can’t make it safe, at least make it so that nobody gets hurt”) and not attempt to unlink elements that are not in the list (or not in the list anymore).&lt;/p&gt;

&lt;figure class=&quot;foldable&quot;&gt;
					&lt;input class=&quot;fold-toggle&quot; type=&quot;checkbox&quot; id=&quot;foldable-toggle-1&quot; /&gt;
					&lt;label for=&quot;foldable-toggle-1&quot;&gt;&lt;/label&gt;
					

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_none&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


				&lt;/figure&gt;

&lt;p&gt;This looks reasonably good.
It might be unsafe, but we’re prepared to deal with that.
There’s just one problem: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unlink&lt;/code&gt; violates the reference model.
Nothing prevents an application using this data structure from keeping a mutable reference to an element around and popping the element from the list while the reference is held.
Popping the element will then create &lt;em&gt;another&lt;/em&gt; mutable reference to the element, but having two mutable references to the same object is not allowed.
Replacing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*mut _&lt;/code&gt; fields with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&amp;lt;*const _&amp;gt;&lt;/code&gt; would not solve that problem: we might no longer have two mutable references, but we would still have two references of which one is mutable.
Which is also not allowed.&lt;/p&gt;

&lt;p&gt;Nothing short of disallowing mutable references to elements in lists can solve this particular problem.
As long as an element is a member of a list there must be &lt;em&gt;no&lt;/em&gt; mutable references to this element.
None at all.
Not even within the implementation of the list, since otherwise &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unlink&lt;/code&gt; would not be allowed to have a reference to the element.&lt;/p&gt;

&lt;p&gt;But before we tackle that let’s replace the pointers with Cells.
The result will still  be incorrect, but it will probably be a step closer to a solution.&lt;/p&gt;

&lt;figure class=&quot;foldable&quot;&gt;
					&lt;input class=&quot;fold-toggle&quot; type=&quot;checkbox&quot; id=&quot;foldable-toggle-2&quot; /&gt;
					&lt;label for=&quot;foldable-toggle-2&quot;&gt;&lt;/label&gt;
					

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_none&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


				&lt;/figure&gt;

&lt;p&gt;The original problem remains unsolved, but its severity has been reduced slightly.
How can we know that no part of the transformation we just applied could have solved the problem?&lt;/p&gt;

&lt;h2 id=&quot;contracts-and-capabilities&quot;&gt;Contracts and capabilities&lt;/h2&gt;

&lt;p&gt;We know because we have not changed the signature of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt;.
Adding an item to the list required a mutable reference before the transformation, and after the transformation it still does.
Both before and after the transformation we can take a mutable reference on an element, and call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; with it, at any time.
But adding an element to a list not only changes the element, &lt;em&gt;it also allows the list to change the element later on&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Since our implementation does not allocate memory we cannot take ownership of elements during &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt;.
Likewise we want to add elements to the list without having to put them into a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Box&lt;/code&gt; first because that too would be an allocation.
If we &lt;em&gt;could&lt;/em&gt; transfer ownership all of our safety problems would solve themselves, but ownership we cannot transfer.
Luckily we don’t &lt;em&gt;have&lt;/em&gt; to transfer ownership, we only have to exclude all the same operations transferring ownership would exclude.&lt;/p&gt;

&lt;p&gt;Let us examine &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; once again.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; receives a mutable reference.
Anyone with a mutable reference to an element can also add that element to a list.
Extending the lifetime of the reference excludes a second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; to the list for the extended lifetime.
Once the extended lifetime has elapsed the capability to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; is once again available, thus to be safe the element must no longer be in a list when the extended lifetime ends.&lt;/p&gt;

&lt;p&gt;To summarize: we must extend the lifetime of the reference given to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; and remove the element before the lifetime ends.
Removing the element can be done by a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Drop&lt;/code&gt; implementation on some struct we have yet to invent.
This leaves only lifetime extension to be figured out.&lt;/p&gt;

&lt;p&gt;To extend the lifetime from the function call to something larger we must bind it to something.
Anything within the function body will of course not live long enough, so the &lt;em&gt;something&lt;/em&gt; must live in the caller of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt;.
Using a variable in the caller and passing it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; as well will not suffice though: the lifetime must not only &lt;em&gt;enter&lt;/em&gt; the function, it must &lt;em&gt;leave&lt;/em&gt; it as well.
If the lifetime enters the function and never leaves, it can be tightened to encompass only the function invocation itself.
Only if it leaves and is bound to a name live &lt;em&gt;after&lt;/em&gt; the function is called can the lifetime be extended.&lt;/p&gt;

&lt;p&gt;Since now our goal is both to extend the lifetime and to remove the element before the lifetime ends we need both the lifetime and a struct to hold it.
To remove the element we also need a reference to the element.
Putting these three things together gives us a struct that owns the capability to modify an elements’ list membership: a &lt;em&gt;handle&lt;/em&gt; to the element in the list.
Adding an element to the list returns a handle to the element, dropping the handle removes the element and allows a new set of operations.&lt;/p&gt;

&lt;figure class=&quot;foldable&quot;&gt;
					&lt;input class=&quot;fold-toggle&quot; type=&quot;checkbox&quot; id=&quot;foldable-toggle-3&quot; /&gt;
					&lt;label for=&quot;foldable-toggle-3&quot;&gt;&lt;/label&gt;
					

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ptr_to_self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Handle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Drop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Handle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;drop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;null_mut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Handle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.next&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.ptr_to_self&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;Handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;u32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.head&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.unlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
                &lt;span class=&quot;nf&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;.value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;unsafe&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.is_none&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


				&lt;/figure&gt;

&lt;p&gt;Note how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop&lt;/code&gt; became safe.
Since we can only call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop&lt;/code&gt; on a list that &lt;em&gt;exists&lt;/em&gt; and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Handle&lt;/code&gt;s guarantee that no list can point to elements that no longer exist we can derive the guarantee that no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop&lt;/code&gt; operation will ever attempt to remove a dead element.&lt;/p&gt;

&lt;p&gt;On the other side of the API &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; is not safe yet.
While the handles protect list from dying (or moved) elements, nothing protect &lt;em&gt;elements&lt;/em&gt; from dying (or moved) &lt;em&gt;lists&lt;/em&gt;.
As the implementation stands we may drop a list before all of its elements have been removed, leaving behind dangling pointers for elements to find when they are dropped.&lt;/p&gt;

&lt;p&gt;There are two ways to solve this: either the list removes all elements when it is dropped, or all handles must have been dropped before the list is dropped.
Clearing the list during drop is straightforward, so let us investigate the other variant instead.
To protect the list from dying elements we must ensure that the list outlives all handles–after a handle has been dropped we no longer care about the element itself.
In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; we already know the lifetime of the handle.
All we have to do is constrain the lifetime of the list to be just as long or longer, then the list will always be empty when dropped.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt;&lt;span class=&quot;gd&quot;&gt;-    pub unsafe fn push&amp;lt;'a&amp;gt;(&amp;amp;mut self, e: &amp;amp;'a mut Element) -&amp;gt; Handle&amp;lt;'a&amp;gt; {
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+    pub unsafe fn push&amp;lt;'a&amp;gt;(&amp;amp;'a mut self, e: &amp;amp;'a mut Element) -&amp;gt; Handle&amp;lt;'a&amp;gt; {&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This change come with an unfortunate side effect: the list can only contain one element because it too, like the element, will be immutable as long as the handle lives.
Having turned the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;head&lt;/code&gt; link into a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&lt;/code&gt; though we do not need the mutable reference for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop&lt;/code&gt;, we simply drop the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mut&lt;/code&gt; and the list works again.&lt;/p&gt;

&lt;p&gt;Our list is safe! … as long as no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Handle&lt;/code&gt; is leaked.
Once handles are leaked (eg with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mem::forget&lt;/code&gt;) all lifetime guarantees are invalidated, allowing elements to be dropped while they are still part of a list.
Removing an item from the list when item itself is dropped is equally unhelpful since the item itself may be leaked just as well as its handle.
The Rust standard library goes through great pains to avoid breaking any invariants when handles are dropped, even though it does not guarantee your program will continue to make forward progress afterwards (eg when a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MutexGuard&lt;/code&gt; instance is leaked the lock will never be available again).&lt;/p&gt;

&lt;p&gt;Whether we can provide more safety with our implementation depends greatly on the constraints under which we intend to use our list.
If the list must outlive its elements there is &lt;em&gt;no way&lt;/em&gt; we can make it intrisically safe by design of the list interface alone: handles may be leaked and never remove their item from the list, items placed on the stack may be forgotten by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mem::forget&lt;/code&gt; and become invalid.
Macros can be used to solve this, encapsulating the unsafe interface into a safe exterior–as long as we do not rely on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;drop&lt;/code&gt; to do our cleanup but explictly write the cleanup out (maybe automatically).
Under different constraints, for example when the list need not outlive its elements, other interfaces with other restrictions and guarantees are possible.&lt;/p&gt;

&lt;h2 id=&quot;epilogue&quot;&gt;Epilogue&lt;/h2&gt;

&lt;p&gt;Resources handles, the main technique we used to make our list as safe as possible, are a very powerful tool.
They cannot solve every problem that requires some sort of cleanup, but for many problems they can go long way towards a good solution.
Most resource handles used in daily life bind the lifecycle of &lt;a href=&quot;https://doc.rust-lang.org/std/fs/struct.File.html&quot;&gt;operating system objects&lt;/a&gt; (such as network sockets or files) or &lt;a href=&quot;https://doc.rust-lang.org/std/sync/struct.MutexGuard.html&quot;&gt;synchronization scopes&lt;/a&gt; to control flow in the program, neither of which break semantics of the program when they are leaked–in worst case scenarios the program may &lt;em&gt;stop&lt;/em&gt; working, but it should never crash.
Using resource handles to bind lifetimes of references to control flow is a lot uncommon, but not unheard of.
&lt;a href=&quot;https://doc.rust-lang.org/std/collections/struct.BTreeMap.html#method.entry&quot;&gt;BTreeMap::entry&lt;/a&gt; and its relatives in the other maps are a good example of this in the standard library.&lt;/p&gt;

&lt;p&gt;Once we leave the cozy confines of userspace programming on large (or large-ish) machines we see patterns like this become more and more common.
Many resource &lt;em&gt;restrictions&lt;/em&gt; we can statically reason about, such as “no allocations for list items” here, can also be phrased as a resource &lt;em&gt;applications&lt;/em&gt;.
(Not all of them can, of course, take for example the restriction “will not exceed 1GB memory usage”. Static reasoning about such restrictions is often possible for real-world application, but we very soon leave compilers behind and work mostly in deeply formalized logic and proof assistants.)
The more severe our restriction is, the less likely it becomes that resource handles alone can solve the problem of enforcing those restrictions.&lt;/p&gt;

&lt;p&gt;All of these handles follow the same pattern though: let the compiler and the libraries do stuff for you whenever it is possible (and safe) to do so.
Some assembly may be required, some warnings might be issued.
But, if possible, embrace your new &lt;a href=&quot;https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization&quot;&gt;RAII&lt;/a&gt; overlords.
They mean well.&lt;/p&gt;

&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;

&lt;p&gt;User Diggsey on &lt;a href=&quot;https://www.reddit.com/r/rust&quot;&gt;/r/rust&lt;/a&gt; poked me to clarify that handles are unsafe when leaked.&lt;/p&gt;

</description>
				<pubDate>Sat, 03 Mar 2018 00:00:00 +0100</pubDate>
				<link>https://eno.space/blog/2018/03/Getting-a-handle-on-things</link>
				<guid isPermaLink="true">https://eno.space/blog/2018/03/Getting-a-handle-on-things</guid>
			</item>
		
			<item>
				<title>Ferrous Oxide For Jaguars And Incremented Crocodiles</title>
				<description>&lt;p&gt;Caveat lector: the primary purpose of the article is to introduce a reader proficient in one of the popular object-oriented languages how &lt;em&gt;not&lt;/em&gt; to program in Rust.
While each feature of the language will be briefly introduced where it is used, no great efforts will be made to explain the feature in detail.
Links to the Rust book should provide that.&lt;/p&gt;

&lt;p&gt;That being said, let’s jump into the pit.&lt;/p&gt;

&lt;!--content--&gt;

&lt;p&gt;Let’s assume, for the sake of argument, that we want to write a Rust library that will add numbers.
The library should not just add numbers, like an ordinary example library would, with some &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add&lt;/code&gt; function it provides as its only interface.
We want something better.
We want something that will add any number of numbers, in any combination: we want to evaluate &lt;em&gt;expressions&lt;/em&gt;.
And not just constant expressions either, we want to evaluate expressions with constants and &lt;em&gt;variables&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;“Aha!” our observant mind says, “I will need an abstract base class with an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;evaluate&lt;/code&gt; function and I’m done!”
We then notice that Rust has no abstract base classes.
It has no classes at all.
It doesn’t even have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;abstract&lt;/code&gt;, really.
What it does have though are &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/structs.html&quot;&gt;ty&lt;/a&gt;&lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/enums.html&quot;&gt;pes&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/traits.html&quot;&gt;traits&lt;/a&gt;.
Types and traits, used together, are not quite enough to implement our idea.
We will also need &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/trait-objects.html&quot;&gt;trait objects&lt;/a&gt;, which behave a lot like interfaces.&lt;/p&gt;

&lt;p&gt;“Okay, so I’ll do an interface-based design”, we say.
Since we want to add constants and variables, in any combination, we will need variables, constants, and combinations.
Variables will be one struct that contains the variable (somehow), constants will be a struct that contain the constant, and combinations will be another struct that contains any of the three.
Easy, right?&lt;/p&gt;

&lt;h2 id=&quot;first-steps&quot;&gt;First steps&lt;/h2&gt;

&lt;p&gt;After some quiet hacking, we might have come up with something like this.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.1&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Constant&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That looks good so far, much like we’d do it in any other language.
Unfortunately it doesn’t work.
The compiler complains in line 5, about the first member of the struct: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;does not have a constant size known at compile-time&lt;/code&gt;.
Remember that trait objects are similar to interfaces.
Interface have to &lt;em&gt;point&lt;/em&gt; somewhere, they are not objects on their own, because we cannot know what kind of data their implementation may contain.
We only know which methods it has.
Rust is no different here: trait objects are effectively pointers.
And what we’ve written down so far treats them like they were objects, which they are not.
So we’ll have to put the trait objects onto the heap, by putting them into &lt;a href=&quot;https://doc.rust-lang.org/nightly/std/boxed/struct.Box.html&quot;&gt;boxes&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.1&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Constant&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))));&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This version works, but it still does not have variables.
In any other language we’d implement variables much like constants are implemented here: we create a few variables and insert them into the expression wherever they are needed by passing a reference.
We keep the original reference around to modify the variable struct, and all is well.&lt;/p&gt;

&lt;p&gt;But rust won’t let us do that so easily.
We could keep the object on the stack and pass it as a reference, if we were to implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression&lt;/code&gt; for the reference to our variable.
But we can’t modify the object anymore, because we put a reference to it into the box.
We could also put a reference to a rust variable into our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Variable&lt;/code&gt; and create multiple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Variable&lt;/code&gt;s, just like constants.
But we can’t modify the rust variable anymore, because we put a reference to it into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Variable&lt;/code&gt;.
Those are just &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/references-and-borrowing.html&quot;&gt;the rules&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Clearly we need to get around this reference business somehow and write the variable anyway.
Luckily for us, rust provides a few types that let us mutate a variable through a simple reference, &lt;a href=&quot;https://doc.rust-lang.org/nightly/std/cell/struct.Cell.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&lt;/code&gt;&lt;/a&gt; for example.
We might want to put a reference to that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&lt;/code&gt; into our variable type somehow.
But no matter how we write it down, the compiler complains about missing lifetimes or things having to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'static&lt;/code&gt;,  whatever that means.
We then search the internet, and the internet says “use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rc&lt;/code&gt;”.
So we’ll put the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&lt;/code&gt; onto the heap too, with an &lt;a href=&quot;https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rc&lt;/code&gt;&lt;/a&gt;!
Feels just like home, just so much more verbose.&lt;/p&gt;

&lt;p&gt;After a lot more hacking (and maybe cursing) we arrive at something that works.&lt;/p&gt;

&lt;h2 id=&quot;its-alive&quot;&gt;It’s alive!&lt;/h2&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.1&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Constant&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Rc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.clone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())),&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))));&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=0, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=4, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wow, finally it works!
But it’s so … verbose.
And long.
And has way too many wrappers for things nobody else would ever wrap.
Clearly, rust is not a good language to do even the most simples of things.&lt;/p&gt;

&lt;p&gt;Or is it? Let me explain.&lt;/p&gt;

&lt;p&gt;So far we have built our program like a programmer coming from Java or C++ might build it, but in doing so we’ve built a rather bad rust program.
It contains interfaces because abstract bases are not a thing, it mutates state that is actually shared between multiple users, and it is very much afraid of the lifetime system.&lt;/p&gt;

&lt;p&gt;Let’s clear those up one by one.&lt;/p&gt;

&lt;h2 id=&quot;references-and-lifetimes&quot;&gt;References and lifetimes&lt;/h2&gt;

&lt;p&gt;References and lifetimes are probably the most tricky part of rust for any novice.
We won’t go into detail on their inner workings here, because the rust book has a fairly complete description of &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/ownership.html&quot;&gt;ownership&lt;/a&gt;, &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/references-and-borrowing.html&quot;&gt;references and borrows&lt;/a&gt;, and &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/lifetimes.html&quot;&gt;lifetimes&lt;/a&gt;.
What we will do though is explain the transformation we do, and why it is safe.&lt;/p&gt;

&lt;p&gt;First of all, what do we want to transform at all?
Let’s start with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rc&lt;/code&gt;.
We introduced it originally because the compiler complained about lifetimes, so that’s probably a good place to start.
Rust complains about references whenever it cannot prove that the reference does not outlive the thing being referenced.
For this the compiler uses two pieces of information: the scope of the thing that is being referenced (including lifetimes, where applicable), and the lifetime of the reference itself.
In our example we will have to add a lifetime to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Variable&lt;/code&gt; struct if we want to store a reference in it.
Let’s just start there.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.1&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Constant&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))));&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=0, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=4, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That, unfortunately, does not work at all.
The compiler still complains about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt; not living long enough: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt; has to be valid for the static lifetime.
That’s because we never specified how long any references within the trait object &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression&lt;/code&gt; must be valid.
We haven’t specified anything, so rust assumed the only safe thing: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'static&lt;/code&gt;, which means “forever”.&lt;/p&gt;

&lt;p&gt;To specify this kind of bound rust provides us with some syntax: writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression + 'lifetime&lt;/code&gt; means that any reference contained in the object backing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression&lt;/code&gt; must be valid for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'lifetime&lt;/code&gt;.
We cannot name any lifetime that is not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'static&lt;/code&gt; though, so we will have to introduce a lifetime parameter on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sum&lt;/code&gt; as well and let the compiler infer appropriate lifetimes from the scopes of the values used.
This is actually an important thing to remember: in rust, lifetimes and scopes can often be treated as being the same thing.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.1&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Constant&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;.0&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))));&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=0, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=4, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;on-interfaces&quot;&gt;On “interfaces”&lt;/h2&gt;

&lt;p&gt;We created the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression&lt;/code&gt; trait because we wanted something like an abstract base class, which rust can’t provide, so we went for the next closest thing instead.
Using traits in such a way is often not a good idea, and often it is in fact &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/trait-objects.html#object-safety&quot;&gt;impossible&lt;/a&gt;.
Rust instead encourages the use of its powerful &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/generics.html&quot;&gt;generics&lt;/a&gt; system.
These generics, like C++ templates and Java generics, are checked at compile time.
Unlike Java generics, there is no runtime cost associated with rust generics.
Unlike C++ templates, the compiler gives very good error messages if a generic type is instantiated in a way the type does not expect.&lt;/p&gt;

&lt;p&gt;Say, for example, we want to write a global function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;evaluate&lt;/code&gt; that can evaluate all kinds of expressions.
Using trait objects we can write this down succinctly;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This does not capture our full intent though.
We do not want to evaluate &lt;em&gt;an expression trait object&lt;/em&gt;, we want to evaluate &lt;em&gt;anything that is an expression&lt;/em&gt;.
This is actually a &lt;em&gt;type bound&lt;/em&gt;: we are prepared to evaluate an object of any type the user wants to give us, as long as that type has an implementation for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression&lt;/code&gt;.
Making the parameter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; generic and at the same time bounding it to be an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Expression&lt;/code&gt; gives us the best of both worlds: we have full type safety, good error messages when we mess up somewhere down the line, and we do not introduce runtime overhead because the type of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; is known at compile time.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But in our case, we don’t even need generics.
In fact, we could not use them if we wanted to because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sum&lt;/code&gt; is inherently recursive: any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sum&lt;/code&gt; may contain any number of more &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sum&lt;/code&gt;s, so we &lt;em&gt;cannot&lt;/em&gt; know the full type of our final expression at compile time—unless we give up our ability to dynamically create the sums, which would make them quite pointless to have.
Luckily we know all possible parts of any summation beforehand: it’s either a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Constant&lt;/code&gt;, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Variable&lt;/code&gt; or a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sum&lt;/code&gt;.
That looks suspiciously like an &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/enums.html&quot;&gt;enum&lt;/a&gt; …&lt;/p&gt;

&lt;p&gt;So let’s rewrite our program and use an enum instead of structs, using &lt;a href=&quot;https://doc.rust-lang.org/book/first-edition/match.html&quot;&gt;match&lt;/a&gt; to destructure the enum instead of dynamic dispatch.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;'a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
      &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
        &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))));&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=0, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=4, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that we still need to put the left- and right-hand sides of an addition into boxes, otherwise we’d end up with same dilemma we had with generics: the type would be impossible to write down.
Also note that when evaluating an addition we match the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r&lt;/code&gt; parts of the addition by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ref&lt;/code&gt;.
If we didn’t do that rust would assume that we want to take ownership of the two boxes, attempt to move them, and fail because we don’t own the entire sum.&lt;/p&gt;

&lt;p&gt;This looks a lot nicer already.
But we still have the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&lt;/code&gt; to take care of.&lt;/p&gt;

&lt;h2 id=&quot;mutating-shared-state&quot;&gt;Mutating shared state&lt;/h2&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&lt;/code&gt; is generally frowned upon.&lt;/p&gt;

&lt;p&gt;Mutating shared state, which &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&lt;/code&gt; provides, is often a sign of a design wanting to be improved.
Other languages rely heavily on mutation of shared state and the premise of “the programmer knows what they are doing”.
Which totally makes sense, for those languages.
Rust, however, was designed to be memory safe.
No rust program should ever act on memory in such a way that afterwards memory is somehow &lt;em&gt;bad&lt;/em&gt;.
This could be caused by any number of things: writing to a pointer that no longer points to valid memory, two threads writing to the same variable simultaneously, one thread reading what another is writing, et cetera.&lt;/p&gt;

&lt;p&gt;What we have in our program is &lt;em&gt;one thread reading what another is writing&lt;/em&gt;, or rather a variant thereof.
We cannot let &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Variable&lt;/code&gt; carry a reference to its value and still mutate the value afterwards because rust enforces, in the compiler, a critical difference between constant and mutable references:
if a mutable reference to a variable &lt;em&gt;exists&lt;/em&gt;, no user of an immutable reference can know that anything read from the reference is valid.
At all.&lt;/p&gt;

&lt;p&gt;Without this rule bad things could happen, for example when referencing “large” objects:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;br&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;put_into_shared_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;br&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Rc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We have placed a reference to the object &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; into a shared object.
Another thread may read this reference from the shared object, and subsequently attempt to access the referenced object.
If this access happens while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; is writing to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;—the referenced object—the other thread might read some part of the object before it was changed, and some parts after.
In the case of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rc&lt;/code&gt; this could mean that the other thread reads a pointer to an object that no longer exists, which violates memory safety.&lt;/p&gt;

&lt;p&gt;But how does this relate to the expressions we wanted to evaluate?&lt;/p&gt;

&lt;p&gt;Rust only enforces these rules on its own references.
There are many kinds of references, and references to memory are only one kind.
Instead of these memory references we could also use an integer index into an array, and provide the array to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eval&lt;/code&gt; on every call.
This index will not change over time, is always linked to (or &lt;em&gt;references&lt;/em&gt;) the same item in a given array, and our expression does not hold a reference to the array.
As a consequence we are free to modify the array between calls to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eval&lt;/code&gt;, and we can thus drop the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cell&lt;/code&gt;s for variables.
Since we no longer have any references in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sum&lt;/code&gt; type we can also get rid of the lifetime &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'a&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-rs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sum&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
      &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
        &lt;span class=&quot;nn&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))));&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=0, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v=4, value is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now it looks quite neat, doesn’t it?&lt;/p&gt;

</description>
				<pubDate>Thu, 15 Feb 2018 00:00:00 +0100</pubDate>
				<link>https://eno.space/blog/2018/02/Ferrous-oxide-for-jaguars-and-incremented-crocodiles</link>
				<guid isPermaLink="true">https://eno.space/blog/2018/02/Ferrous-oxide-for-jaguars-and-incremented-crocodiles</guid>
			</item>
		
			<item>
				<title>On Error Handling</title>
				<description>&lt;p&gt;Error handling is something we all have to do.
Error handling is hard, and there are just &lt;em&gt;so many&lt;/em&gt; times we think we &lt;em&gt;know&lt;/em&gt; that some piece of code just &lt;em&gt;cannot&lt;/em&gt; fail. So why check for errors there?&lt;/p&gt;

&lt;p&gt;Well …&lt;/p&gt;

&lt;p&gt;Your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chdir()&lt;/code&gt; could fail because the target was deleted, causing your program to dump files &lt;em&gt;somewhere&lt;/em&gt; in the failesystem.
Your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log2()&lt;/code&gt; could fail because your input was negative for some reason, summoning nasal demons as it fails.
Your spacecraft guidance system could crash, causing your spacecraft to crash and burn as well. (&lt;a href=&quot;https://en.wikipedia.org/wiki/Cluster_(spacecraft)#Launch_failure&quot;&gt;This has happened.&lt;/a&gt;)
All that just because error handling is hard, or expensive, or &lt;em&gt;&amp;lt;insert other reason here&amp;gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;All because error handling sucks. But why is that so?&lt;/p&gt;

&lt;!--content--&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;state-of-the-error&quot;&gt;State of the error&lt;/h1&gt;

&lt;p&gt;If your background is in C, Perl, some parts of PHP, or basically anything else that tries to be like-C-but-different, you will be familiar with the humble &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;errno&lt;/code&gt; and it’s friend &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return -1&lt;/code&gt;.
If your background is Java, Python or other parts of PHP, you will know exceptions.
If your background is C with external libraries, you will probably be familiar with functions that return error codes.
If your background is basically anything except Haskell, you will be familiar with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALSE&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt; or whatever else your language may call it as the canonical value for &lt;em&gt;well, that didn’t work out so well, did it?&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let’s look at each one of these mechanisms and try to find out why they make error handling harder than it should be.
The discussion will focus on C and C++, because these two languages are widely used and account for a truly horrifying number CVEs (so some discussion on how to avoid errors may be in order).
In particular we will assume that the purpose of error handling is to not only to handle unexpected conditions gracefully, but also to prove that some parts of the program handle &lt;em&gt;all&lt;/em&gt; unexpected conditions gracefully without croaking.&lt;/p&gt;

&lt;h2 id=&quot;sentinel-values&quot;&gt;Sentinel values&lt;/h2&gt;

&lt;p&gt;Or as they are more commonly known, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;returns -1 and sets errno appropriately&lt;/code&gt;.
But it’s not just the pairing of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;errno&lt;/code&gt;, we call any value that &lt;em&gt;could&lt;/em&gt; be a valid result of a function &lt;em&gt;but isn’t (by convention)&lt;/em&gt; a sentinel value.
Programming in C (and many languages derived from C) is full of sentinel values:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;C strings are terminated with a &lt;em&gt;NUL&lt;/em&gt; byte, which is a valid character—but by convention used as string
terminator because “nobody needs a string that contains &lt;em&gt;NUL&lt;/em&gt;”&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc()&lt;/code&gt; and friends return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt; pointers if they can’t allocate memory, because address 0 is always
invalid (obviously (hello microcontrollers!))&lt;/li&gt;
  &lt;li&gt;the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;str*&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mem*&lt;/code&gt; family of C functions often return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt; on failure (if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strstr&lt;/code&gt; cannot find a
substring in a string, for example)&lt;/li&gt;
  &lt;li&gt;almost any C function that involves a file descriptor returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt; on failure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The common theme for all of these is that &lt;em&gt;an error is also a value&lt;/em&gt;.
If you ever call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open()&lt;/code&gt; and it fails, you will get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt; as a result—which is fine as far as signalling goes, but you can happily use that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt; later for reading or writing or unlinking or whatever you may wish.
You don’t &lt;em&gt;have&lt;/em&gt; to handle the error, you can just &lt;em&gt;assume&lt;/em&gt; that the operation has succeeded.
In C, this is an especially bad problem because it is just &lt;em&gt;so easy&lt;/em&gt; to forget error checking and not notice.
Call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lseek(fd, 0, SEEK_END)&lt;/code&gt; to get the length of some file.
That never fails if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fd&lt;/code&gt; is valid, right?
Not quite.
On 32bit systems, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lseek&lt;/code&gt; might conceivably fail for files larger than 2GB.
In that case, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lseek&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt;.
If you use the return value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lseek&lt;/code&gt; to allocate memory (because lseek never fails, right?), your allocator will almost certainly say &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Which leads us to the next kind of error indicator.&lt;/p&gt;

&lt;h2 id=&quot;almost-sentinel-values&quot;&gt;Almost-sentinel values&lt;/h2&gt;

&lt;p&gt;Like sentinel values, almost-sentinel values are error signals that have to be checked.
Unlike sentinel values, almost-sentinel values are also &lt;em&gt;valid results in their own right&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Examples for almost-sentinel values are legion among the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f*&lt;/code&gt; family of IO functions in the C library.
The functions always return something that &lt;em&gt;makes sense&lt;/em&gt; as a return value.
For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fread&lt;/code&gt; will always return the number of items read.
If an error occurs, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fread&lt;/code&gt; might have read nothing yet, and thus return 0.
If end of file occurs, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fread&lt;/code&gt; will also return 0.&lt;/p&gt;

&lt;p&gt;Not all is lost though, because the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f*&lt;/code&gt; family also contains &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feof&lt;/code&gt; to check whether a file has been read to end, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ferror&lt;/code&gt; to check whether an error has happened.
All you need to do to make your code safe is check one (or both) if you get a result you didn’t expect.
Easy, right?&lt;/p&gt;

&lt;p&gt;Enter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readdir&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Take this excerpt from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readdir(3)&lt;/code&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;If the end of the directory stream is reached, NULL is returned and errno is not changed.
If an error occurs, NULL is returned and errno is set appropriately.
To distinguish end of stream and from an error, set errno to zero before calling readdir()
and then check the value of errno if NULL is returned.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readdir&lt;/code&gt; correctly, you must always clear &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;errno&lt;/code&gt; before you call it.&lt;/p&gt;

&lt;p&gt;(Fortunately, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readdir_r&lt;/code&gt; has none of these problems, and it even is thread-safe where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readdir&lt;/code&gt; is not required to be.
&lt;a href=&quot;https://lwn.net/Articles/696474/&quot;&gt;Unfortunately, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readdir_r&lt;/code&gt; has its own problems and has been deprecated by glibc in favour of thread-safe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readdir&lt;/code&gt;&lt;/a&gt;.)&lt;/p&gt;

&lt;h2 id=&quot;error-code-returns&quot;&gt;Error code returns&lt;/h2&gt;

&lt;p&gt;So far we’ve only looked at the C standard library.
There are, however, multitudes of other C libraries out there, for everything the C standard library doesn’t do (and everything it does do, of course).
Not all of these libraries can use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;errno&lt;/code&gt; facility the C library provides.
Faced with this problem, libraries usually do one of two things:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;use sentinel values and an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;errno&lt;/code&gt;-like symbol or function (the mysql C connector does this, for example)&lt;/li&gt;
  &lt;li&gt;return the error code directly and place results in out parameters (gnutls and others)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the first of these two is a case of sentinel values, it won’t be discussed further.
The second is a lot more interesting and warrants closer inspection.
Not because it allows for more and more precise error indications, but because it decouples error signalling from the actual values produced by any given operation.&lt;/p&gt;

&lt;p&gt;This little change is so important that it bears repeating: error code returns decouple error indications from result values.
&lt;strong&gt;Error indicators and result values are distinct types.&lt;/strong&gt;
This type distinction needn’t be enforced at the language level to be significant.
The distinction allows for statements like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x is valid if and only if err == 0&lt;/code&gt;, which in turn allows us to communicate this information to the compiler.
A compiler using information like this could (for example) warn about uses of possibly uninitialized values for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; if it cannot prove that the use occurs in a codepath that ensures that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;err == 0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unfortunately, compilers do not actually do this.
Runtimes do this.
They call it by another name entirely though.&lt;/p&gt;

&lt;h2 id=&quot;exceptions&quot;&gt;Exceptions&lt;/h2&gt;

&lt;p&gt;Languages that call themselves “object oriented” usually come with a system they call &lt;em&gt;exceptions&lt;/em&gt;.
These exceptions are used to signal errors without the need for error codes, thus removing a lot of perceived clutter from code.
“Removing clutter” is actually a fitting description, since a lot of code does not care &lt;em&gt;which&lt;/em&gt; error occured, only &lt;em&gt;whether&lt;/em&gt; an error occured—and if so, it usually wants to just notify another thing of the error and stop running.
Exceptions allow for this by making error signalling &lt;em&gt;a piece of control flow&lt;/em&gt;: if an error happens, just tell the runtime about the details of the error by throwing an exception.
The runtime will then clean up your partial state (as best it knows how) and find &lt;em&gt;someone&lt;/em&gt; interested in the error (or crash your program, if nobody is).&lt;/p&gt;

&lt;p&gt;This makes exceptions a curious mixture of error signalling, error handling, and control flow.
It makes exception-based error handling systems very easy because forgetting error handling will not silently produce &lt;em&gt;incorrect&lt;/em&gt; results, it will produce &lt;em&gt;no results at all&lt;/em&gt;.
If an exception is thrown that the program does not know how to handle, the program merely crashes.&lt;/p&gt;

&lt;p&gt;What exception systems save in clutter where the exact type of error is not important, it adds in clutter where it is.
Take, for example, &lt;a href=&quot;https://docs.oracle.com/javase/7/docs/api/java/net/SocketException.html&quot;&gt;SocketException&lt;/a&gt; in Java.
For every kind of error a program may want to handle, Java has a different type of exception.
Python tries to steer a middle course by putting an error number into an exception, allowing you to catch just one exception for a number of different errors.&lt;/p&gt;

&lt;p&gt;Contrast this to C++: where in Java (especially Java, but also Python and the other) every error condition is an exception, in C++ many error conditions &lt;em&gt;are not&lt;/em&gt;.
C++ inherits much of C, and thus also inherits the error number system, which is a large part of the reason for this.
C++ also has exceptions, and some parts of the STL use them for most of their error signalling.
Some parts don’t, though.
In fact, the filesystem library added in C++17—by number of operations possibly the largest single part of the STL that throws exceptions—has non-throwing overloads for &lt;em&gt;everything&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Upon closer inspection, almost all other instances of exceptions thrown in the STL do not merely signal some kind of error, they signal an &lt;em&gt;invariant violation&lt;/em&gt;.
Regex contains invalid character classes?
Throw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;regex_error&lt;/code&gt;.
Try to access a vector element beyond the end of the vector?
Throw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;out_of_range&lt;/code&gt;.
Call a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;function&lt;/code&gt; object that does not contain a function?
Throw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bad_function_call&lt;/code&gt;.
Extract something from a stream that is at EOF?
Set a bit to indicate failure.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Set a bit to indicate failure.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In C++, any error that &lt;em&gt;can&lt;/em&gt; be handled reasonably is not thrown as an exception.
Exceptions are reserved for those cases where a piece of code cannot execute because it would violate a program invariant, or because such a violation has already happened.
In Java they do that too, among all other kinds of error signalling.
But in C++, exceptions are primarily a tool to communicate invariant violations.&lt;/p&gt;

&lt;p&gt;Why does this make exceptions a bad error handling strategy though?&lt;/p&gt;

&lt;p&gt;Well, it doesn’t.
It does make them less-than-ideal if you need information about what can fail and what can’t though.
If your program is one of those lucky few that can just print some error information and die when an error occurs, by all means use exceptions.
If you want to prove that certain parts of your code cannot ever crash because you forgot to handle an error condition, &lt;em&gt;do not use exceptions&lt;/em&gt; for those parts.&lt;/p&gt;

&lt;p&gt;If you shouldn’t use error numbers, or error code returns, or exceptions, what should you use?&lt;/p&gt;

&lt;h1 id=&quot;its-two-values-right&quot;&gt;It’s two values, right?&lt;/h1&gt;

&lt;p&gt;Any operation that can fail does not &lt;em&gt;just&lt;/em&gt; return an error when it fails and a value when it doesn’t.
It returns an error &lt;em&gt;if and only if&lt;/em&gt; it fails, and a value &lt;em&gt;if and only if&lt;/em&gt; it doesn’t.
Effectively it returns &lt;em&gt;two&lt;/em&gt; values of distinct types, because it cannot return a &lt;em&gt;single&lt;/em&gt; value that can cover &lt;em&gt;both&lt;/em&gt; types—error and proper return value.&lt;/p&gt;

&lt;p&gt;Except that it can.
Sort of.
C++ has &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;union&lt;/code&gt; types that have exactly these semantics: each instance of a union contains a single value of &lt;em&gt;exactly one&lt;/em&gt; of the contained types.
Add a little indicator like so, and you’re done, right?&lt;/p&gt;
&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;error_or&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;has_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;something_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;error_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Almost, but not quite.
You can still access &lt;em&gt;the wrong member&lt;/em&gt; of your struct, just like you could ignore the error.
Only now the results can be much worse, because the error and the value you access share a memory location, thus invoking completely undefined behaviour if you ignore the error and always read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;value&lt;/code&gt; from the struct.&lt;/p&gt;

&lt;h1 id=&quot;enter-sum-types&quot;&gt;Enter Sum types&lt;/h1&gt;

&lt;p&gt;What we really want is some type that just &lt;em&gt;does not&lt;/em&gt; let you access the wrong member.
If your operation yielded an error, you want the compiler to just not let you access the value.
Similarly, if the operation succeeded, you want the compiler to just not let you access the error.&lt;/p&gt;

&lt;p&gt;This is not actually a new concept—functional languages have been doing this since the olden days.
In these functional langues, a type that contains &lt;em&gt;one&lt;/em&gt; value of one of &lt;em&gt;many&lt;/em&gt; types is called a &lt;strong&gt;sum type&lt;/strong&gt;.
Any language inspired by functional languages usually has sum types as well.
&lt;a href=&quot;https://www.rust-lang.org&quot;&gt;Rust&lt;/a&gt;, being inspired by functional programming languages, has them—they call them &lt;em&gt;enum types&lt;/em&gt;.
C has them, they call them &lt;em&gt;unions&lt;/em&gt;.
C++17 has a different formulation, here they are called &lt;a href=&quot;http://en.cppreference.com/w/cpp/utility/variant&quot;&gt;&lt;em&gt;variants&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What sets the functional languages and Rust apart from C and C++ in this list though is that the functional langues and Rust have &lt;em&gt;matching&lt;/em&gt; constructs, whereas C and C++ &lt;em&gt;don’t&lt;/em&gt;.
Take the example of Rust, and the &lt;a href=&quot;https://doc.rust-lang.org/std/result/enum.Result.html&quot;&gt;Result&lt;/a&gt; type there:&lt;/p&gt;
&lt;div class=&quot;language-rust highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;Err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The compiler will generate code that will execute the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ok&lt;/code&gt; branch of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;match&lt;/code&gt; if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;res&lt;/code&gt; contains a value and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Err&lt;/code&gt; branch otherwise.
And there is no other way to get at the contents of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Result&lt;/code&gt; instance.
Just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;match&lt;/code&gt;.
(Rust does provide helper methods that, for example, either return the value or panic and crash the program if the object does not contain a value.)&lt;/p&gt;

&lt;p&gt;This matching is immensely powerful.
Unfortunately, C++ has no matching feature built into the language, and C is right out.
We can however use features of modern C++ to emulate matching by encapsulating the union we need to hold the values and the indicator which part of the union is valid into a new type, and giving this type a &lt;em&gt;dispatch&lt;/em&gt; operation that takes a number of callable objects and just calls the right one.
We might also not want to call the operation &lt;em&gt;dispatch&lt;/em&gt;, but something else—something that describes not what it does, but what it can be used to do.
Since the Rust example above takes one value, executes one of two branches, and returns one value, we may want to call it … &lt;em&gt;reduce&lt;/em&gt;, because it reduces two branches to one?&lt;/p&gt;
&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;error_or&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// return the value on success, or -1 on error&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error_code&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// as opposed to ...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error_or&lt;/code&gt; class demonstrated here is available in &lt;a href=&quot;https://git.eno.space/nu.git&quot;&gt;the nu utilities library&lt;/a&gt;.
Documentation is available at the &lt;a href=&quot;https://eno.space/docs/nu/&quot;&gt;nu documentation site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The first variant (the call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt;) has one decisive advantage over the second variant: the first variant can &lt;em&gt;guarantee&lt;/em&gt; that no exceptions will be thrown by the execution of the code, while the second variant cannot.
This is very useful if you use noexceptness as a guarantee that the program does not crash due to faulty error handling.
It is more useful still if you use &lt;a href=&quot;/blog//blog/2016/12/Static-Noexcept-Checking&quot;&gt;a compiler plugin&lt;/a&gt; to check that you haven’t messed up your noexcept.&lt;/p&gt;

&lt;h2 id=&quot;making-error_or-more-useful&quot;&gt;Making &lt;strong&gt;error_or&lt;/strong&gt; more useful&lt;/h2&gt;

&lt;p&gt;This interface is a bit lacking if you want to chain multiple operations together.
Since the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt; operation above turns two values into one, chaining operations requires the branches of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt; to return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error_or&lt;/code&gt; instances as well.&lt;/p&gt;
&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;error_or&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;some_operation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error_code&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error_or&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;some_other_operation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error_code&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error_or&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error_code&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NAN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first two operations applied to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; in this example return another &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error_or&lt;/code&gt; instance, once with a different value type.
Both operations just forward the error if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; didn’t contain a value.&lt;/p&gt;

&lt;p&gt;This pattern of calling one operation that may fail, applying another operation that may fail on the result, and applying another operation that may fail on the result … is rather common.
For this reason, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error_or&lt;/code&gt; also provides an operator for exactly this purpose.&lt;/p&gt;
&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;error_or&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// apply some_operation to value in e and return the result, or&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// cast the error in e to the result type of some_operation.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// then do the same with some_other_operation&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;some_operation&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;some_other_operation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error_code&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NAN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Another common thing to do is to extract a part of the result to reuse in later processing.
To make this a little easier, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error_or&lt;/code&gt; provides another operator to apply a transformation only on the contained value, not on the whole &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error_or&lt;/code&gt; object.&lt;/p&gt;
&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;error_or&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// transform the contained value of e into a vector,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// creating an error_or&amp;lt;std::vector&amp;lt;double&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// then call some_other_operation_again on the resulting object.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;some_other_operation_again&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;p&gt;Easy, right?&lt;/p&gt;
</description>
				<pubDate>Tue, 27 Dec 2016 00:00:00 +0100</pubDate>
				<link>https://eno.space/blog/2016/12/On-Error-Handling</link>
				<guid isPermaLink="true">https://eno.space/blog/2016/12/On-Error-Handling</guid>
			</item>
		
			<item>
				<title>Static Noexcept Checking</title>
				<description>&lt;p&gt;C cannot be said to be a language that supports its users along all paths they may take.
It is notoriously hard to write memory-safe programs (as the myriad CVEs we see every year make clear).
Error handling in the C standard library relies on in-band signaling of error conditions and a very limited set of error numbers to communicate what went wrong.
Indirectly called functions can either be not generic or receive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void*&lt;/code&gt; arguments and cast them like maniacs—look no further than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qsort&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But the one thing C has that actually &lt;em&gt;does&lt;/em&gt; help programmers in a meaningful way is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;!--content--&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;you-still-gonna-change-that&quot;&gt;You still gonna change that?&lt;/h1&gt;

&lt;p&gt;C++, being mostly-but-not-quite C with a lot of things bolted on, inherited the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; qualifier and its wonders.
In C, writing to a memory location that has been marked &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; is an error—but calling a function that expects an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int*&lt;/code&gt; with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const int*&lt;/code&gt; will often only net you a compiler warning.
In C++, that same call will be an error.&lt;/p&gt;

&lt;p&gt;In fact, C++ has strengthened its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; system so much over the version found in plain C that during the standardization of C++11 the committee discovered that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; may just as well mean &lt;strong&gt;threadsafe&lt;/strong&gt;.
As in: any piece of data marked &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; may be shared freely between threads, because no thread will ever modify that piece of data.
(I am oversimplifying this a bit. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const_cast&lt;/code&gt; is a thing that exists and should never ever ever be used, and a whole host of other evil things ).&lt;/p&gt;

&lt;p&gt;Let that sink in for a second: if your program contains some piece of data called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&lt;/code&gt;, and all accesses to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&lt;/code&gt; are made through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; pointers or references, then your program cannot have a data race for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&lt;/code&gt;.
Data races are one of the prinicipal sources of errors in multithreaded programs.
Ergo, if your program is const-correct and you use constant data liberally, you can almost eliminate a large class of problems from your program.&lt;/p&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;doesnt-race-still-crashes-though&quot;&gt;Doesn’t race, still crashes though&lt;/h1&gt;

&lt;p&gt;Unfortunately, data races are not the only source of bad behaviour in programs.
You can still pass around null pointers and dereference them by accident, which crashes your program.
You can still divide by 0, which probably also crashes your program.
You can summon all sorts of other &lt;a href=&quot;https://en.wikipedia.org/wiki/Undefined_behavior&quot;&gt;nasal demons&lt;/a&gt; and not even realize—or worse, realize only when a new compiler version suddenly insist on the nasal demons and breaks your program in the process.
You can throw exceptions and never catch them.
There’s a myriad and one ways to write programs that just don’t do what they should.&lt;/p&gt;

&lt;p&gt;Fortunately, we have tools to help us with most of these problems.
Valgrind and AddressSanitizer are immensely useful for tracking memory errors like segfaults or out-of-bounds accesses.
UndefinedBehaviourSanitizer helps with divide by 0 and many of the other nasal demons.
Clang for has added &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_Nullable&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_Nonnull&lt;/code&gt; qualifiers, which can at the very least warn about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nullptr&lt;/code&gt; arguments to functions that don’t except them.
But for exceptions, nothing of the sort seems to exist.&lt;/p&gt;

&lt;p&gt;But not all hope is lost, we do have one small tool in the language that could help us with that: the exception specifications.
Most of us will have seen at least one of these in our life and maybe wondered about it, thinking, &lt;em&gt;what does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;throw()&lt;/code&gt; really mean?&lt;/em&gt;
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;throw&lt;/code&gt; specification, formally known as a &lt;em&gt;dynamic exceptions specification&lt;/em&gt;, does look a lot like the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;throws&lt;/code&gt; specification that is so ubiquitous in Java, and they even do the same thing if you don’t look too closely.&lt;/p&gt;

&lt;p&gt;In Java, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void f() throws X&lt;/code&gt; means “f will never throw a checked exception that is not X or derived from X”.
Note the &lt;em&gt;checked&lt;/em&gt; in that sentence.
Even if a function does not declare anything it might throw, it may always throw &lt;em&gt;unchecked&lt;/em&gt; exceptions like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NullPointerException&lt;/code&gt;.
In C++ though, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void f() throw(X)&lt;/code&gt; really &lt;em&gt;does&lt;/em&gt; mean that a function will never throw anything not derived from X—except that it doesn’t actually mean that.
What it really means is that &lt;em&gt;f will crash your program if anything not derived from X was thrown from within f&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But that’s not even the whole story.
If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; did somehow manage to throw, say, an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt;, the program would not immediately abort.
Instead, the runtime will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unexcepted&lt;/code&gt;, which by default calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::terminate&lt;/code&gt;, which then crashes your program.
You may set your own handlers for such unexpected exceptions—but those handlers, too, are expected to crash your program.&lt;/p&gt;

&lt;p&gt;Oh, and nothing checks dynamic exceptions specifications.&lt;/p&gt;

&lt;p&gt;It gets even better.
Not only are these specifications not checked at compile time at all (at least not by any compiler I use), they aren’t even part of the type of the function they were stuck to.
&lt;em&gt;They are not part of the type&lt;/em&gt;.
For that reason, they &lt;em&gt;cannot&lt;/em&gt; be checked reliably at compile time as soon as the first function pointer is passed around.&lt;/p&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;enter-noexcept&quot;&gt;Enter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noexcept&lt;/code&gt;&lt;/h1&gt;

&lt;p&gt;C++11 finally realized that dynamic exception specifications are not too useful the way they are, and because they aren’t they are mostly unused or only used as little sprinkles of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;throw()&lt;/code&gt; on functinos in libraries.
Dynamic exception specifications were deprecated and replaced with &lt;em&gt;noexcept exception specifications&lt;/em&gt;, which differ from dynamic exception specifications in two major ways.&lt;/p&gt;

&lt;p&gt;First, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noexcept&lt;/code&gt; specs actually &lt;strong&gt;do&lt;/strong&gt; guarantee that a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void f() noexcept&lt;/code&gt; will unconditionally crash your program instead if propagating an exception to its caller.
No more &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unexpected&lt;/code&gt;, any violation will result to a call in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::terminate&lt;/code&gt; at runtime.
No way out.&lt;/p&gt;

&lt;p&gt;Second, &lt;strong&gt;noexcept specs are computable flags&lt;/strong&gt;.
You cannot just declare that your function does not throw exceptions.
You can declare that your function does not throw exception &lt;strong&gt;if&lt;/strong&gt;.
You can write something like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Given that declaration, the compiler will ensure that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&amp;lt;T&amp;gt;&lt;/code&gt; does not propagate any exception if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sizeof(T)&lt;/code&gt; is greater than 4, &lt;em&gt;if&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mangle(arg)&lt;/code&gt; also declares that it does not propagate any exception with a noexcept specification of its own.&lt;/p&gt;

&lt;p&gt;Obviously, this is immensely more powerful than dynamic exception specifications, but in a slightly different way.
Dynamic exceptions specifications can tell you &lt;em&gt;what&lt;/em&gt; a function may throw, noexcept specifications can tell you &lt;em&gt;whether&lt;/em&gt; a function may throw—the latter of which is often the most useful.&lt;/p&gt;

&lt;p&gt;Oh, and nothing checks noexcept specifications.&lt;/p&gt;

&lt;p&gt;It gets even better.
Not only are these specifications not checked at compile time at all (at least not by any compiler I use), they aren’t even part of the type of the function they were stuck to.
&lt;em&gt;They are not part of the type&lt;/em&gt;.
For that reason, they &lt;em&gt;cannot&lt;/em&gt; be checked reliably at compile time as soon as the first function pointer is passed around.
I think I’m having a déjà vu …&lt;/p&gt;

&lt;p&gt;Until C++17, that is.
In that release of the standard, noexcept specification &lt;strong&gt;are&lt;/strong&gt; part of the function type, so theoretically, compilers could also check noexcept correctness.
I still don’t know of any that do.&lt;/p&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;theres-no-engineering-like-overengineering&quot;&gt;There’s no engineering like overengineering&lt;/h1&gt;

&lt;p&gt;Not having any checkers for this certainly won’t hurt anyone too badly, but it also is kind of a minor pain.
Maybe if C++ had dependent types (and thus dependent function types with noexcept specs)?
If we could determine whether a function can throw anything by looking not only at types, but also at values, we would have … well, something that essentially wouldn’t be C++ anymore, so let’s not fret that too much.&lt;/p&gt;

&lt;p&gt;What this absence of checkers &lt;em&gt;does&lt;/em&gt; do is make noexcept more dangerous than it should be.
Placing a noexcept spec incorrectly can introduce crashes that might not have been there if the spec hadn’t been there either.
On the other hand, liberal use of noexcept is powerful documentation, and if it is used right it can even be &lt;strong&gt;a guarantee to not crash&lt;/strong&gt;.
Which was the goal I had when I started looking into noexcept spec checking—using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noexcept&lt;/code&gt; as a guarantee to not crash just like C++ uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; as a guarantee to not have data races (well, of sorts).&lt;/p&gt;

&lt;p&gt;Out of this mess came the &lt;a href=&quot;https://git.eno.space/static-noexcept.git&quot;&gt;static-noexcept&lt;/a&gt; clang plugin.
Get it, build it, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export CXX=&quot;clang++ -fplugin=${PATH_TO_static-noexcept.so}&quot;&lt;/code&gt; and you’re set!&lt;/p&gt;

&lt;p&gt;This checker will look at, essentially, only one thing: &lt;em&gt;is this code noexcept-correct?&lt;/em&gt;
To determine whether it is, the checker will inspect all function definitions with a positive noexcept specification (this includes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noexcept&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noexcept(true)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noexcept(X)&lt;/code&gt; for any other &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&lt;/code&gt; that is true, and almost all destructors).
If a function is declared as noexcept but contains &lt;em&gt;any&lt;/em&gt; expression that could possibly throw an exception, the checker will raise an error and tell you where the offending expression is.
If you are so inclined, it can even tell you which functions &lt;em&gt;could be noexcept&lt;/em&gt; if they aren’t already.&lt;/p&gt;

&lt;h2 id=&quot;examples-from-the-test-suite&quot;&gt;Examples (from the test suite)&lt;/h2&gt;

&lt;p&gt;Any throw expression is immediately a violation of noexcept.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-error {{is noexcept}}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-note {{is here}}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Calling a function (explicitly or implicitly as constructors/destructors) that does not have a positive noexcept spec is also a violation.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nothrow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// no error reported&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;nothrow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The checker can also look into a function definition to determine correctness if the function does not have a noexcept spec.
If no definition can be found, the function is assumed to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noexcept(false)&lt;/code&gt;, and calling it is also a violation:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Bad&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Bad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Bad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-error {{is noexcept}}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Bad&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-note {{is here}}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Bad&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Base classes and fields of classes are checked.
If a constructor is marked noexcept, all constructors called by it (explicitly or implicitly) must be noexcept.
The same holds for destructors.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-error {{violates destructor noexcept spec}}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Any function that is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extern &quot;C&quot;&lt;/code&gt; will automatically be assumed to be noexcept.
Not doing so would break basically everything imported from C, and throwing exceptions from a function that is supposed to be called by C is just an idea that is bad enough to assume nobody really does it.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;extern&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;C&quot;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ext_c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// no error&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ext_c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Any indirect calls will be treated as violations.
Yes, even in C++17 mode&lt;del&gt;, because I’m lazy like that&lt;/del&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;extern&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;C&quot;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;c_call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-error {{is noexcept}}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-note {{is here}}&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-error {{is noexcept}}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-note {{is here}}&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Language expression other than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;throw&lt;/code&gt; are also recognized as violations if they may throw.
Currently, these are some versions of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dynamic_cast&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;typeid&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-error {{is noexcept}}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;dynamic_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-note {{is here}}&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-error {{is noexcept}}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-note {{is here}}&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If you are &lt;em&gt;absolutely&lt;/em&gt;, &lt;strong&gt;positively&lt;/strong&gt; sure that some expression in your programm will not throw exceptions, you can instruct the checker to just ignore it.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// no error reported&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And finally, to round it all off, you can have the checker tell you when a function could be noexcept, but isn’t.
The command line fragment to do this is truly hideous, but that’s compiler plugins for you.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// run with -Xclang -plugin-arg-static-noexcept -Xclang -Rcandidates&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// expected-remark {{could be noexcept}}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

</description>
				<pubDate>Wed, 07 Dec 2016 00:00:00 +0100</pubDate>
				<link>https://eno.space/blog/2016/12/Static-Noexcept-Checking</link>
				<guid isPermaLink="true">https://eno.space/blog/2016/12/Static-Noexcept-Checking</guid>
			</item>
		
	</channel>
</rss>
