Skip to content

Commit f874251

Browse files
committed
Optionally implement ConstDefault trait on types
Signed-off-by: Nathaniel McCallum <[email protected]>
1 parent ee880bd commit f874251

File tree

6 files changed

+29
-3
lines changed

6 files changed

+29
-3
lines changed

.github/workflows/test.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ on: [pull_request]
22
name: test
33
jobs:
44
test:
5-
name: ${{ matrix.toolchain }} (${{ matrix.profile.name }})
5+
name: ${{ matrix.toolchain }} ${{ matrix.profile.name }} ${{ matrix.features }}
66
runs-on: ubuntu-latest
77
steps:
88
- uses: actions/checkout@v2
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions-rs/cargo@v1
1414
with:
1515
command: test
16-
args: ${{ matrix.profile.flag }}
16+
args: ${{ matrix.profile.flag }} --features=${{ matrix.features }}
1717
strategy:
1818
fail-fast: false
1919
matrix:
@@ -22,6 +22,9 @@ jobs:
2222
- beta
2323
- stable
2424
- 1.41.0
25+
features:
26+
-
27+
- const-default
2528
profile:
2629
- name: debug
2730
- name: release

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ github = { repository = "enarx/primordial", workflow = "test" }
1919
maintenance = { status = "actively-developed" }
2020
is-it-maintained-issue-resolution = { repository = "enarx/primordial" }
2121
is-it-maintained-open-issues = { repository = "enarx/primordial" }
22+
23+
[dependencies]
24+
const-default = { version = "0.1", optional = true }

src/address.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ use core::ops::*;
2222
#[repr(transparent)]
2323
pub struct Address<T, U>(T, PhantomData<U>);
2424

25-
impl<T: Zero, U: Copy> Address<T, U> {
25+
#[cfg(feature = "const-default")]
26+
impl<T: Zero, U> const_default::ConstDefault for Address<T, U> {
27+
const DEFAULT: Self = Self::NULL;
28+
}
29+
30+
impl<T: Zero, U> Address<T, U> {
2631
/// The NULL address
2732
pub const NULL: Address<T, U> = Address(T::ZERO, PhantomData);
2833
}

src/offset.rs

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ use core::ops::*;
1818
#[repr(transparent)]
1919
pub struct Offset<T, U>(T, PhantomData<U>);
2020

21+
#[cfg(feature = "const-default")]
22+
impl<T: Zero, U> const_default::ConstDefault for Offset<T, U> {
23+
const DEFAULT: Self = Self(T::ZERO, PhantomData);
24+
}
25+
2126
impl<T, U> Offset<T, U> {
2227
/// Create an offset value from the number of items
2328
#[inline]

src/page.rs

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ use core::mem::{align_of, align_of_val, size_of, size_of_val};
99
#[repr(C, align(4096))]
1010
pub struct Page([[u64; 32]; 16]);
1111

12+
#[cfg(feature = "const-default")]
13+
impl const_default::ConstDefault for Page {
14+
const DEFAULT: Self = Self([[0; 32]; 16]);
15+
}
16+
1217
impl Default for Page {
1318
fn default() -> Self {
1419
Self([[0; 32]; 16])

src/register.rs

+5
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ implptr! {
271271
#[repr(transparent)]
272272
pub struct Register<T>(T);
273273

274+
#[cfg(feature = "const-default")]
275+
impl<T: super::Zero> const_default::ConstDefault for Register<T> {
276+
const DEFAULT: Self = Self(T::ZERO);
277+
}
278+
274279
impl<T> Register<T> {
275280
/// Converts a register value to a slice
276281
///

0 commit comments

Comments
 (0)