Skip to content

Commit 0359266

Browse files
authored
Make alignment public but un-implementable to allow for downstream crates to be generic over alignment (#17)
1 parent ca719ae commit 0359266

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

src/lib.rs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,26 @@ use core::{
3838

3939
use as_slice::{AsMutSlice, AsSlice};
4040

41-
mod sealed;
41+
/// A marker trait for an alignment value.
42+
pub trait Alignment: Copy + sealed::Sealed {}
43+
44+
impl Alignment for A2 {}
45+
impl Alignment for A4 {}
46+
impl Alignment for A8 {}
47+
impl Alignment for A16 {}
48+
impl Alignment for A32 {}
49+
impl Alignment for A64 {}
50+
51+
mod sealed {
52+
pub trait Sealed {}
53+
54+
impl Sealed for super::A2 {}
55+
impl Sealed for super::A4 {}
56+
impl Sealed for super::A8 {}
57+
impl Sealed for super::A16 {}
58+
impl Sealed for super::A32 {}
59+
impl Sealed for super::A64 {}
60+
}
4261

4362
/// 2-byte alignment
4463
#[derive(Clone, Copy)]
@@ -91,7 +110,7 @@ pub const fn Aligned<A, T>(value: T) -> Aligned<A, T> {
91110

92111
impl<A, T> ops::Deref for Aligned<A, T>
93112
where
94-
A: sealed::Alignment,
113+
A: Alignment,
95114
T: ?Sized,
96115
{
97116
type Target = T;
@@ -103,7 +122,7 @@ where
103122

104123
impl<A, T> ops::DerefMut for Aligned<A, T>
105124
where
106-
A: sealed::Alignment,
125+
A: Alignment,
107126
T: ?Sized,
108127
{
109128
fn deref_mut(&mut self) -> &mut T {
@@ -113,7 +132,7 @@ where
113132

114133
impl<A, T> ops::Index<ops::RangeTo<usize>> for Aligned<A, [T]>
115134
where
116-
A: sealed::Alignment,
135+
A: Alignment,
117136
{
118137
type Output = Aligned<A, [T]>;
119138

@@ -124,7 +143,7 @@ where
124143

125144
impl<A, T> AsSlice for Aligned<A, T>
126145
where
127-
A: sealed::Alignment,
146+
A: Alignment,
128147
T: AsSlice,
129148
{
130149
type Element = T::Element;
@@ -136,7 +155,7 @@ where
136155

137156
impl<A, T> AsMutSlice for Aligned<A, T>
138157
where
139-
A: sealed::Alignment,
158+
A: Alignment,
140159
T: AsMutSlice,
141160
{
142161
fn as_mut_slice(&mut self) -> &mut [T::Element] {
@@ -146,7 +165,7 @@ where
146165

147166
impl<A, T> Clone for Aligned<A, T>
148167
where
149-
A: sealed::Alignment,
168+
A: Alignment,
150169
T: Clone,
151170
{
152171
fn clone(&self) -> Self {
@@ -159,14 +178,14 @@ where
159178

160179
impl<A, T> Copy for Aligned<A, T>
161180
where
162-
A: sealed::Alignment,
181+
A: Alignment,
163182
T: Copy,
164183
{
165184
}
166185

167186
impl<A, T> Default for Aligned<A, T>
168187
where
169-
A: sealed::Alignment,
188+
A: Alignment,
170189
T: Default,
171190
{
172191
fn default() -> Self {
@@ -179,7 +198,7 @@ where
179198

180199
impl<A, T> Debug for Aligned<A, T>
181200
where
182-
A: sealed::Alignment,
201+
A: Alignment,
183202
T: Debug,
184203
{
185204
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
@@ -189,7 +208,7 @@ where
189208

190209
impl<A, T> Display for Aligned<A, T>
191210
where
192-
A: sealed::Alignment,
211+
A: Alignment,
193212
T: Display,
194213
{
195214
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
@@ -199,7 +218,7 @@ where
199218

200219
impl<A, T> PartialEq for Aligned<A, T>
201220
where
202-
A: sealed::Alignment,
221+
A: Alignment,
203222
T: PartialEq,
204223
{
205224
fn eq(&self, other: &Self) -> bool {
@@ -209,14 +228,14 @@ where
209228

210229
impl<A, T> Eq for Aligned<A, T>
211230
where
212-
A: sealed::Alignment,
231+
A: Alignment,
213232
T: Eq,
214233
{
215234
}
216235

217236
impl<A, T> Hash for Aligned<A, T>
218237
where
219-
A: sealed::Alignment,
238+
A: Alignment,
220239
T: Hash,
221240
{
222241
fn hash<H: Hasher>(&self, state: &mut H) {
@@ -226,7 +245,7 @@ where
226245

227246
impl<A, T> Ord for Aligned<A, T>
228247
where
229-
A: sealed::Alignment,
248+
A: Alignment,
230249
T: Ord,
231250
{
232251
fn cmp(&self, other: &Self) -> Ordering {
@@ -236,7 +255,7 @@ where
236255

237256
impl<A, T> PartialOrd for Aligned<A, T>
238257
where
239-
A: sealed::Alignment,
258+
A: Alignment,
240259
T: PartialOrd,
241260
{
242261
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {

src/sealed.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)