oxidecomputer / oxidecomputer/omicron

[type safety] Strongly typed UUIDs

Open
#745 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

cleanup
Dominant language
Rust
Stars
572
Forks
97
Avg merge
2d 12h
Merged PRs (30d)
96

Description

We use UUIDs for everything in Omicron - identifying internal objects, external objects, and probably just for fun in a couple other spots too.

These all use the uuid::Uuid structure, which is functionally sufficient, but less safe than it could be. For example, in PRs like https://github.com/oxidecomputer/omicron/pull/741 , where a "Disk ID" was replaced by a "Volume ID", the underlying types are both UUIDs, but they're fundamentally different.

Building a simple "newtype" wrapper around a UUID for each object would prevent this misuse, along the lines of:

#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
struct DiskID(pub uuid::Uuid);

impl std::ops::Deref for DiskID {
  type Target = uuid::Uuid;
  fn deref(&self) -> &Self::Target { &self.0 }
}

#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
struct VolumeID(pub uuid::Uuid);

impl std::ops::Deref for VolumeID {
  type Target = uuid::Uuid;
  fn deref(&self) -> &Self::Target { &self.0 }
}

These structs would be usable wherever a standard uuid::Uuid object could be used, but would avoid being interchangeable with each other.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inventorying the repository's uses of uuid::Uuid and identifying which UUIDs represent distinct object types. Review the proposed newtype approach and its serialization and API implications. Done means relevant object identifiers are distinct types and cannot be passed interchangeably.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.