Rust vs Common C++ Bugs

I used to like C++. I still do, but I used to, too. Joking aside, I am not one to tell any C++ programmer to just use Rust. There are a ton of valid reasons why companies or individuals decide to use C++ and I don’t wish to deter anyone from doing so. What I am trying to do in this article is to see how Rust stacks up against a handful of very common (and severe) bugs that are easy to produce in C++. I tried to make this article worthwile for both Rust and C++ programmers.

Posted 2022-12-21 , Last Updated 2022-12-26
Read More

Implementing Rayon’s Parallel Iterators - A Tutorial

The rayon library is a brilliant piece of engineering that is so simple to use that one seldom has to descend into its depths. This is the story of how I learned to implement rayon’s ParallelIterator trait for my own type. There are tons of guides on how to use rayon’s parallel iterators and there are a few explanations on how they work under the hood. However, I found no tutorials on how to implement a parallel iterator from the ground up. This article aims to close that gap.

Posted 2022-10-30 , Last Updated 2022-11-01
Read More

Compile-Time If-Expressions for Types in C++11

The other day I wondered if there is a way to metaprogam a syntax that returns a type based on a conditional expression akin to an ifelse ifelse expression. The idea came to me when I searched for a programmatic way to choose the right size of integer for a bitset, given a certain number of bits. And yes, I know there’s std::bitset, but I’ll take any excuse to investigate an interesting metaprogramming problem.

Posted 2022-08-20
Read More

Debugging A Stack Overflow In Rust

Recently I hit a stack overflow error in Rust and I found a helpful quick and dirty way of debugging it using a tool that a friendly member of the community hacked. If you hit a stack overflow and are too lazy to fire up your debugger, this article is for you.

Posted 2022-07-25
Read More

External Post: Raspberry Pi GPIO with Zig

A while ago I wrote an article about a small project that I did in the Zig Programming Language about controlling GPIO on the Raspberry Pi. Zig is a neat language and the whole project ended up being a whole lot of fun. You can read the article here. Although Zig is a very new – and, at the time of writing, immature– language I encourage everyone to take a look, because rather than aiming to replace C++ in all its power and complexity, it aims to replace C which makes it conceptually very different from a language like Rust.

Posted 2022-03-21
Read More

Now with Light and Dark Mode

By popular demand this site now supports both a light and a dark theme. There is no explicit way to switch between light and dark mode, instead the site will adapt to the browser preferences.

Posted 2021-10-23
Read More

C++ Metaprogramming: Safer Reading from Buffers

I am currently getting into embedded programming with C at work and while there are some things I learned to appreciate about the simplicity of C, two of the things I really miss are generics and stricter type safety. I wondered if C++ can offer a safer way of reading types from arrays of bytes. This, of course, gives me an excuse to work out my C++ template metaprogramming muscles, which have atrophied a little.

Posted 2021-10-08
Read More

Mutually Exclusive Traits in Rust

In a recent attempt to write some clever code, I set out to implement mutually exclusive traits and some blanket implementations involving these. In doing so, I hit the limits of what the compiler can do for me, but after a lot of trial-and-error, researching, and flashing the bat signal to the Rust community I had something that worked. This is how to get mutually exclusive traits working in current Rust for Future Me™ and anyone else who might be interested.

Posted 2021-07-31
Read More

Project Spotlight: varpro - Nonlinear Fitting with A Simple Interface

In the last months, I have been developing three crates for Rust. All of these crates are open source libraries that use different aspects of the language. The first crate is a numerics library, which emphasizes interfaces that are powerful but simple to use. The second one is a fluent coding library using mostly declarative macros and the third crate uses procedural macros to help with expressive assertions. For my next series of posts I am going to introduce these three crates and highlight a particular aspect of each one. First up is the varpro library.

Posted 2021-05-27
Read More

Rust: A Common Interface for Functions Taking Different Numbers of Arguments

I am working on a little numerics project where the goal is to let the user pass base functions to a data-fitting model in a simple fashion. And while I really like what Rust’s lambdas can already do for me, I was wondering if I can pass functions with different (but compile time constant) numbers of parameters using a single interface. It turns out that I can, and the answer is a nice combination of Rust and C++ metaprogramming idioms.

Posted 2021-02-15
Read More

Understanding the DFT: Spectral Leakage, Windowing, and Periodicity

I was going to write something about how windowing could be used to mitigate the effects of spectral leakage in the DFT. However, I went down a rabbit hole trying to truly understand where spectral leakage comes from and how it relates to the periodicity of the DFT. What I ended up writing, is a post that is more about understanding the DFT and less about windowing, although it plays an important role in this post.

Posted 2021-01-17
Read More

Back to Basics: The (Discrete) Fourier Transform

I have extensively worked with the Fourier transform and the DFT but never really taken the time to understand how these two things are truly connected and how that connection explains some of the properties of the DFT. In this article I explore this connection with the aim of answering one question: what does the DFT actually compute?

Posted 2020-12-30
Read More

Mutexes in Rust and C++: Protecting Data versus Protecting Code

I have been using Rust in production for a couple of months now and there is lots about it that I like. One neat thing is that Rust’s compile time guarantees make memory and thread safety much harder to mess up from the get go. This allows Rust programmers to confidently use some paradigms that are harder to use correctly in C++. One such example is protecting shared data with mutexes, which works very differently in Rust than in C++. This post is about exploring those fundamental differences.

Posted 2020-12-01
Read More

Replacing the Ternary Operator with Rust-style If Expressions in C++11 - Part 1

Right now I am getting into Rust, which lets me see the paradigms of C++ in a new light. This is a small thing, but for example: C++ has if statements and Rust has if expressions which can return a value. This inspired me to implement an expressive syntax for mimicking Rust’s if expressions in C++. To keep things simple, I only targeted the use case of replacing nested ternary operators.

Posted 2020-07-06
Read More

Implementing a Pipe Syntax for std::optional - Part 3: Indended Side Effects and Unintended Limitations

This is the last part of my series on a pipe syntax for operations on std::optional types. We will look at switching between function overloads at compile time using std::enable_if to implement the last puzzle piece we were missing before. I will also show some limitations of my implementation and describe how my pipe operator fits in to the overarching idea of functional programming.

Posted 2020-06-07
Read More