1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// This file is part of context-allocator. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/context-allocator/master/COPYRIGHT. No part of context-allocator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2019 The developers of context-allocator. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/context-allocator/master/COPYRIGHT.


/// Useful extensions.
pub trait NonZeroUsizeExt: Sized + Copy + Ord + Debug
{
	/// Next power of two.
	#[inline(always)]
	fn next_power_of_two(self) -> Self
	{
		Self::non_zero_unchecked(self.to_usize().next_power_of_two())
	}

	/// Round up.
	#[inline(always)]
	fn round_up_to_power_of_two(self, non_zero_power_of_two_alignment: NonZeroUsize) -> Self
	{
		let power_of_two = non_zero_power_of_two_alignment.get();
		let power_of_two_less_one = power_of_two - 1;

		debug_assert!(self.checked_add(power_of_two_less_one).is_some(), "non_zero_power_of_two_alignment is far too close to the maximum value of a pointer");

		Self::non_zero(self.add(power_of_two_less_one).to_usize() & !power_of_two_less_one)
	}

	/// Round down.
	#[inline(always)]
	fn round_down_to_power_of_two(self, power_of_two: NonZeroUsize) -> usize
	{
		let value = self.to_usize();
		let power_of_two_exponent = power_of_two.logarithm_base2();

		value & !((1 << power_of_two_exponent) - 1)
	}

	/// Divide.
	#[inline(always)]
	fn divide_power_of_two_by_power_of_two(self, divisor: NonZeroUsize) -> usize
	{
		debug_assert!(self.is_power_of_two(), "self `{:?}` is not a power of two", self);
		debug_assert!(divisor.is_power_of_two(), "divisor `{:?}` is not a power of two", divisor);

		self.to_usize() >> divisor.logarithm_base2()
	}

	/// Is power of two.
	#[inline(always)]
	fn is_power_of_two(self) -> bool
	{
		self.to_usize().is_power_of_two()
	}

	/// Logarithm base two.
	#[inline(always)]
	fn logarithm_base2(self) -> usize
	{
		self.to_usize().trailing_zeros() as usize
	}

	/// Decrement.
	#[inline(always)]
	fn decrement(self) -> usize
	{
		self.to_usize() - 1
	}

	/// Add.
	#[inline(always)]
	fn add(self, increment: usize) -> Self
	{
		Self::non_zero(self.to_usize() + increment)
	}

	/// Add.
	#[inline(always)]
	fn add_non_zero(self, increment: NonZeroUsize) -> Self
	{
		Self::non_zero(self.to_usize() + increment.get())
	}

	/// Add.
	#[inline(always)]
	fn checked_add(self, increment: usize) -> Option<Self>
	{
		self.to_usize().checked_add(increment).map(Self::non_zero)
	}

	/// Add.
	#[inline(always)]
	fn add_assign(&mut self, increment: usize)
	{
		*self = (*self).add(increment)
	}

	/// Double.
	#[inline(always)]
	fn doubled(self) -> NonZeroUsize
	{
		(self.to_usize() << 1).non_zero()
	}

	/// Difference.
	#[inline(always)]
	fn difference(self, other: Self) -> usize
	{
		debug_assert!(self >= other, "other `{:?}` is less than self `{:?}`", other, self);

		self.to_usize() - other.to_usize()
	}

	/// Difference.
	#[inline(always)]
	fn difference_non_zero(self, other: Self) -> NonZeroUsize
	{
		self.difference(other).non_zero()
	}

	/// Multiply.
	#[inline(always)]
	fn multiply(self, other: Self) -> NonZeroUsize
	{
		(self.to_usize() * other.to_usize()).non_zero()
	}

	/// Is odd.
	#[inline(always)]
	fn is_odd(self) -> bool
	{
		self.to_usize().is_odd()
	}

	/// Non zero.
	#[inline(always)]
	fn to_non_zero_u32(self) -> NonZeroU32
	{
		let usize = self.to_usize();
		debug_assert!(usize <= ::std::u32::MAX as usize, "exceeds `{}` ::std::u32::MAX `{}`", usize, ::std::u32::MAX);
		NonZeroU32::non_zero_unchecked(usize as u32)
	}

	/// Non zero.
	#[inline(always)]
	fn non_zero(value: usize) -> Self
	{
		debug_assert_ne!(value, 0, "value is zero");

		Self::non_zero_unchecked(value)
	}

	/// Non zero.
	fn non_zero_unchecked(value: usize) -> Self;

	#[doc(hidden)]
	fn to_usize(self) -> usize;
}

impl NonZeroUsizeExt for NonZeroUsize
{
	#[inline(always)]
	fn difference(self, other: Self) -> usize
	{
		debug_assert!(self >= other, "other `{:?}` is less than self `{:?}`", other, self);

		self.get() - other.get()
	}

	#[inline(always)]
	fn non_zero_unchecked(value: usize) -> Self
	{
		non_zero_usize(value)
	}

	#[inline(always)]
	fn to_usize(self) -> usize
	{
		self.get()
	}
}