Skip to content

Commit 6b90391

Browse files
author
Artem Kryvokrysenko
committed
Add Alignment::new constructor function
Currently the only way to construct Aligned value is using `aligned::Aligned` function which returns instance of `Aligned` struct. This does not work well with type aliases. For example, consider this type alias: ``` /// Aligns value at cache line boundary (assuming 64 byte cache line size) type CacheLineAligned<T> = Aligned<A64, T>; ``` User still has to use `aligned::Aligned` function to create value which breaks abstraction provided by type alias: ``` let cache_aligned_value: CacheLineAligned<u32> = aligned::Aligned(42); ``` In this commit I am implementing a conventional `new` constructor for `struct Aligned` which works with type aliases: ``` let cache_aligned_value = CacheLineAligned::new(42_u32); ```
1 parent 10a449d commit 6b90391

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ where
155155
}
156156
}
157157

158+
impl<A, T> Aligned<A, T>
159+
where
160+
A: Alignment,
161+
{
162+
/// Changes the alignment of value to be at least A bytes
163+
pub const fn new(value: T) -> Self {
164+
Aligned {
165+
_alignment: [],
166+
value,
167+
}
168+
}
169+
}
170+
171+
158172
impl<A, T> Aligned<A, [T]>
159173
where
160174
A: Alignment,
@@ -525,6 +539,16 @@ fn sanity() {
525539
let _: &[u8] = y;
526540
}
527541

542+
#[test]
543+
fn test_type_alias_new() {
544+
type CacheLineAligned<T> = Aligned<A64, T>;
545+
546+
let aligned: Aligned<A64, _> = Aligned([0u8; 3]);
547+
let aligned_new = CacheLineAligned::new([0u8; 3]);
548+
549+
assert_eq!(aligned, aligned_new);
550+
}
551+
528552
#[test]
529553
fn test_range_to() {
530554
let a: &Aligned<A2, [u8]> = &Aligned::<A2, _>([0, 1, 2, 3]);

0 commit comments

Comments
 (0)