Description
We have this handy macro newtype_index!
that creates a, well, newtype'd index:
newtype_index!(RegionAtLocationIndex { DEBUG_FORMAT = "RegionAtLocationIndex({})" });
However, I think it could be improved in four ways:
First, the syntax should change to include the keyword struct
and pub
. This would help with people trying to find the definition of the type. I'd probably switch to {}
form at the same time, but that's not necessary:
newtype_index! {
pub struct RegionAtLocationIndex { DEBUG_FORMAT = "RegionAtLocationIndex({})" }
}
Second, doing this would also allow us to support arbitrary visibilities. For example, I'd like to make crate struct RegionAtLocationIndex
:
newtype_index! {
crate struct RegionAtLocationIndex { DEBUG_FORMAT = "RegionAtLocationIndex({})" }
}
Third, we should change to incorporate NonZero
. That is, the original example would currently expands to something like:
pub struct RegionAtLocationIndex(u32);
but I want it to expand to:
pub struct RegionAtLocationIndex { non_zero: NonZero<u32> }
Of course, 0
is a valid index, so the various initialization and accessor routines would have to add or subtract one as appropriate.
Using NonZero
would mean that Option<T>
would be represented still as a single u32.
Finally, fourth, as a convenience, it would be nice to define inherent (non-trait) methods, so that using these types did not require importing the Idx
trait:
impl $type {
#[inline]
fn new(value: usize) -> Self {..}
#[inline]
fn index(self) -> usize {..}
}
Activity
spastorino commentedon Apr 30, 2018
I can tackle this!
sgrif commentedon Apr 30, 2018
FWIW I'd prefer we do
pub(crate) struct
instead ofcrate struct
, so we continue to map to the corresponding Rust syntax. We should be able to just the vis matcherpetrochenkov commentedon Apr 30, 2018
Oh, that thing stopping "Go To Definition" from working 😄
nikomatsakis commentedon Apr 30, 2018
@sgrif
crate
is Rust syntax =) -- the vis matcher would be finesgrif commentedon Apr 30, 2018
Oh neat, I was not aware of #45388
NonZeroU32
innewtype_index!
macro, change syntax #53315Rollup merge of rust-lang#53315 - nikomatsakis:newtype-index, r=Mark-…