Chapter 2 - An Id Manager
-
2 - An Id Manager
The first piece of code that I’m going to play with in Rust is a “reusable id manager”. This is a collection of numerical ids which can be allocated, used and then released back to a pool for later reuse. I tend to use … -
2.1 - An interval
We’ll start with an interval, this is a struct with an upper and lower limit and represents an inclusive range, such as [0,255] or [1] where both upper and lower are the same value, 1. I’m sure there’s a better way to do … -
2.2 - A collection of intervals
Now that I have a simple interval I need a collection of them to represent the available ids that we can allocate. I’m going to create a new file for this new struct, intervals.rs and move the code from last time into interval.rs. I … -
2.3 - Adding intervals correctly
Our simple collection of intervals has a major failing, it doesn’t merge intervals and so we end up with as many intervals as we perform insert operations. For example, if we start with an empty collection and add [4] and then [6] we … -
2.4 - Removing values
Now that we can insert intervals into our collection we need to be able to remove them. There are three ways to remove values from our collection: remove the first interval in the collection remove the first value in the collection remove … -
2.5 - Building an id manager from a collection of intervals
Now that we have a collection of intervals we can begin to build our id manager. The id manager turns the collection of intervals on its head a bit in that the intervals represent the available ids, so the manager allocates an id by … -
2.6 - Smart Ids; object lifetime and mutability
The simple id manager that I built last time is just that, simple. However, it’s enough to start exploring some more complex ideas in Rust. With the current interface you can allocate an id from the id manager and never give it back. … -
2.7 - Renaming without restructuring
We now have a ThreadSafeIdManager that can provide SmartId’s. It would probably be better to simply have an IdManager that provides Id’s, especially since, in Rust, we can’t even reliably use the original IdManager … -
2.8 - Generic code in Rust
Now that we have an IdManager that works reasonably well and does much of what we require of it we can look at how to make it a bit more flexible. At present the IdManager can only provide Ids that are BYTE sized. I’d like to make it … -
2.9 - Clean up and additional functionality
We now have a generic IdManager so now we just need to finish off the required functionality. The missing pieces are: a “reuse policy”, so we can dictate how new ids are allocated the ability to restrict the range of ids used. … -
2.10 - The journey so far
I’ve now built a generic IdManager which does everything I want it to do, for now. I’ve bumbled along in a very non-scientific manner, mostly using the compiler errors to guide me towards things I’ve then looked up on the …