Get integer type of specified width

Is there a nicer way of implementing this? Perhaps without hardcoding UInts of specific sizes?

# Inverse sizeof, basically.
of_exact_size(::Val{1})  = UInt8
of_exact_size(::Val{2})  = UInt16
of_exact_size(::Val{4})  = UInt32
of_exact_size(::Val{8})  = UInt64
of_exact_size(::Val{16}) = UInt128

# Returns the smallest unsigned integer type not narrower than
# min_width.
#
# There's no dynamic dispatch according to JET.jl, at least with Julia
# 1.8.
of_size(::Val{min_width}) where {min_width} =
  of_exact_size(Val(nextpow(2, min_width)))

The built-in sizeof is handy. You might try…

function of_size(min_width)
	types = sort(subtypes(Unsigned), by=sizeof)
	for type in types
		sizeof(type) >= min_width && return type
	end
	nothing
end

…although it looks like you want to compute of_size it at compile time. If you’re doing code generation, I’d use a macro-approach, and use of_size as normal function. (I try to avoid Val.)