As told by Matthttps://rodusek.com/Recent content on As told by MattHugo -- gohugo.ioen©2020-2024 Matthew Rodusek.Sat, 14 Dec 2024 00:00:00 +0000- Go Tip: Creating Closed Interfaceshttps://rodusek.com/posts/2024/12/14/go-tip-creating-closed-interfaces/Sat, 14 Dec 2024 00:00:00 +0000https://rodusek.com/posts/2024/12/14/go-tip-creating-closed-interfaces/<p>In <a class="language link" href="https://rodusek.com/languages#go">go</a>
, interfaces are satisfied by any type that implements all of the
methods defined in the interface. This makes Go interfaces “open” types, meaning
that any type can satisfy an interface at any time.</p>
<p>However, did you know that Go actually has a mechanism for restricting who can
implement these types? This <a class="category link" href="https://rodusek.com/categories/#go-tip">go-tip</a>
will tell you how you can
accomplish this.</p>
- Blog Reboothttps://rodusek.com/posts/2024/01/02/blog-reboot/Tue, 02 Jan 2024 23:47:29 +0000https://rodusek.com/posts/2024/01/02/blog-reboot/<p>It’s been a <em>long</em> time since I’ve last updated this blog – and I think I’m
finally ready to reboot it.</p>
<p>A lot has changed since my last article nearly 3 years ago:</p>
<ul>
<li>I changed jobs a number of times, and am now begrudgingly working as a <a href="https://go.dev"class="markdown-link"target="_blank" rel="noopener noreferrer">Go</a>
developer professionally.</li>
<li>I found new love of the <a href="https://www.rust-lang.org/"class="markdown-link"target="_blank" rel="noopener noreferrer">Rust</a>
programming language and started writing rust
projects in my spare time.</li>
<li>I’ve learned a lot about working in domains ranging from <a href="https://cloud.google.com/"class="markdown-link"target="_blank" rel="noopener noreferrer">GCP</a>
to working
embedded on <a href="https://www.ti.com/microcontrollers-mcus-processors/arm-based-processors/overview.html"class="markdown-link"target="_blank" rel="noopener noreferrer">Texas Instruments SoCs</a>
.</li>
<li>I haven’t written any practical C++ in nearly a year.</li>
</ul>
<p>I have a lot to talk about, and hope to use this blog as somewhere I can share
my knowledge and experiences with others that hope to learn and grow from them.</p>
- Reflecting Over Members of an Aggregatehttps://rodusek.com/posts/2021/03/21/reflecting-over-members-of-an-aggregate/Sun, 21 Mar 2021 11:31:40 -0400https://rodusek.com/posts/2021/03/21/reflecting-over-members-of-an-aggregate/<p>A little while back a friend of mine and I were talking about serialization of
<code>struct</code> objects as raw bytes. He was working with generated objects that contain
padding, but the objects needed to be serialized <em>without</em> the padding; for
example:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Foo</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kt">char</span> <span class="n">data0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="c1">// 3 bytes padding here
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="kt">int</span> <span class="n">data1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span></code></pre></div><p>In the case he described, there are dozens of object types that need to be
serialized, and all are:</p>
<ul>
<li>Generated by his organization (so they can’t be modified), and</li>
<li>Are guaranteed to be <a href="https://en.cppreference.com/w/cpp/language/aggregate_initialization"class="markdown-link"target="_blank" rel="noopener noreferrer">aggregates</a>
</li>
</ul>
<p>Being a template meta-programmer, I thought it would be a fun challenge to try
to solve this in a generic way using <a class="language link" href="https://rodusek.com/languages#c++17">c++17</a>
– and in the process I
accidentally discovered a generic solution for iterating all members of
<strong>any aggregate type</strong>.</p>
- Getting an Unmangled Type Name at Compile Timehttps://rodusek.com/posts/2021/03/09/getting-an-unmangled-type-name-at-compile-time/Tue, 09 Mar 2021 19:21:59 -0500https://rodusek.com/posts/2021/03/09/getting-an-unmangled-type-name-at-compile-time/<p>Getting the name of a type in C++ is a hassle. For something that should be
trivially known by the compiler at compile-time, the closest thing we have to
getting the type in a <strong>cross-platform</strong> way is to use
<a href="https://en.cppreference.com/w/cpp/types/type_info/name"class="markdown-link"target="_blank" rel="noopener noreferrer"><code>std::type_info::name</code></a>
which
is neither at compile-time, nor is it guaranteed to be human-readable.</p>
<p>In fact, both GCC and Clang actually return the compiler’s <strong>mangled</strong> name
rather than the human-readable name we are used to. Let’s try to make something
better using the modern utilities from <a class="language link" href="https://rodusek.com/languages#c++17">c++17</a>
and a little creative problem
solving!</p>
- Builder Pattern: Expressing ownership transferhttps://rodusek.com/posts/2021/03/02/builder-pattern-expressing-ownership-transfer/Tue, 02 Mar 2021 17:06:40 -0500https://rodusek.com/posts/2021/03/02/builder-pattern-expressing-ownership-transfer/<p>The Builder pattern is a common design in software development for late-binding
inputs to allow for iterative construction. This pattern, when applied in <code>c++</code>,
leaves one very obvious question: <strong>Can we present this to consumers in an
optimal way?</strong></p>
<p>More specifically: when we call <code>build()</code>, should we be moving any temporary
internal state, or copy it? Can we express both in a safe and idiomatic way to
consumers?</p>
- Final Poetryhttps://rodusek.com/posts/2021/02/28/final-poetry/Sun, 28 Feb 2021 01:23:18 -0500https://rodusek.com/posts/2021/02/28/final-poetry/Note The C++WTF segment shines a light on the dark and esoteric corners of the C++ language for fun and profit The C++ standards committee is very strongly against producing any kind of breaking changes when considering new language features – especially keywords. Over the course of standardization, this has lead to terms like auto being repurposed, or awkward keywords like co_await and co_yield being introduced in C++20 to avoid breaking a handful of projects in the world that might possibly be using await or yield in their vocabulary.
- Creating a Fast and Efficient Delegate Type (Part 3)https://rodusek.com/posts/2021/02/26/creating-a-fast-and-efficient-delegate-type-part-3/Fri, 26 Feb 2021 22:19:51 -0500https://rodusek.com/posts/2021/02/26/creating-a-fast-and-efficient-delegate-type-part-3/<aside class="alert alert-note" role="alert">
<header class="alert-header">
<p class="alert-title">
<svg class="alert-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>
Note
</p>
</header>
<div class="alert-message">This is part 3 of a 3 part series.</div>
</aside>
<p>In the <a href="https://rodusek.com/posts/2021-02-26/creating-a-fast-and-efficient-delegate-type-2">previous post</a>
we updated our <code>Delegate</code> object that we’ve been working on since
<a href="https://rodusek.com/posts/2021-02-24/creating-a-fast-and-efficient-delegate-type">the first post</a>
to support covariance.</p>
<p>In this post, we will look at how to make this a true <strong>zero-overhead</strong> utility</p>
- Creating a Fast and Efficient Delegate Type (Part 2)https://rodusek.com/posts/2021/02/26/creating-a-fast-and-efficient-delegate-type-part-2/Fri, 26 Feb 2021 22:18:32 -0500https://rodusek.com/posts/2021/02/26/creating-a-fast-and-efficient-delegate-type-part-2/<aside class="alert alert-note" role="alert">
<header class="alert-header">
<p class="alert-title">
<svg class="alert-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>
Note
</p>
</header>
<div class="alert-message">This is part 2 of a 3 part series.</div>
</aside>
<p>In the <a href="https://rodusek.com/posts/2021-02-24/creating-a-fast-and-efficient-delegate-type">previous post</a>
,
we saw how we could build a simple and light-weight <code>Delegate</code> type that binds
free functions, and member functions. However we have a notable limitation that
we require specifying the <em>type</em> of the members being bound
(e.g. <code>d.bind<Foo,&Foo::do_something>()</code>). Additionally, we’re forced to bind
only the <em>exact</em> type. We can’t bind anything that is <em>covariant</em> to it.</p>
<p>Lets improve upon that.</p>
- Creating a Fast and Efficient Delegate Type (Part 1)https://rodusek.com/posts/2021/02/24/creating-a-fast-and-efficient-delegate-type-part-1/Wed, 24 Feb 2021 20:03:14 -0500https://rodusek.com/posts/2021/02/24/creating-a-fast-and-efficient-delegate-type-part-1/<aside class="alert alert-note" role="alert">
<header class="alert-header">
<p class="alert-title">
<svg class="alert-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>
Note
</p>
</header>
<div class="alert-message">This is part 1 of a 3 part series.</div>
</aside>
<p>When working in C++ systems, a frequent design pattern that presents itself is
the need to bind and reuse functions in a type-erased way. Whether it’s
returning a callback from a function, or enabling support for binding listeners
to an event (such as through a <code>signal</code> or observer pattern), this is a pattern
that can be found everywhere.</p>