Skip to content

Commit 000f86f

Browse files
committed
initial implementation
Signed-off-by: Martin Kröning <[email protected]>
1 parent 248f653 commit 000f86f

File tree

9 files changed

+1806
-1
lines changed

9 files changed

+1806
-1
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.gitignore

.github/workflows/ci.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
merge_group:
7+
8+
env:
9+
RUSTFLAGS: -Dwarnings
10+
RUSTDOCFLAGS: -Dwarnings
11+
12+
jobs:
13+
clippy:
14+
name: Clippy
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: dtolnay/rust-toolchain@stable
19+
- run: cargo clippy --all-features
20+
21+
doc:
22+
name: Doc
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: dtolnay/rust-toolchain@stable
27+
- run: cargo doc --all-features
28+
29+
fmt:
30+
name: Format
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v4
34+
- uses: dtolnay/rust-toolchain@nightly
35+
with:
36+
components: rustfmt
37+
- run: cargo fmt --all --check
38+
39+
test:
40+
name: Test
41+
runs-on: ubuntu-latest
42+
steps:
43+
- uses: actions/checkout@v4
44+
- uses: dtolnay/rust-toolchain@stable
45+
- run: cargo test --all-features
46+
47+
test-s390x:
48+
name: Test S390x
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: docker/setup-qemu-action@v3
52+
with:
53+
platforms: linux/s390x
54+
- uses: docker/setup-buildx-action@v3
55+
with:
56+
platforms: linux/s390x
57+
- uses: docker/build-push-action@v5
58+
with:
59+
platforms: linux/s390x

Cargo.lock

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
[package]
22
name = "endian-num"
33
version = "0.0.0"
4+
authors = ["Martin Kröning <[email protected]>"]
45
edition = "2021"
5-
description = "Reserved."
6+
description = "Byte-order-aware numeric types."
7+
repository = "https://github.com/rust-osdev/endian-num"
68
license = "MIT OR Apache-2.0"
9+
keywords = ["byte", "endian", "big-endian", "little-endian", "binary"]
10+
categories = ["encoding", "rust-patterns", "no-std::no-alloc"]
11+
12+
[package.metadata.docs.rs]
13+
all-features = true
14+
rustdoc-args = ["--cfg", "docsrs"]
715

816
[dependencies]
17+
bitflags = { version = "2", optional = true }
18+
bytemuck = { version = "1", optional = true }
19+
bytemuck_derive = { version = "1", optional = true }
20+
zerocopy = { version = "0.7", optional = true, default-features = false }
21+
zerocopy-derive = { version = "0.7", optional = true }
22+
23+
[features]
24+
bitflags = ["dep:bitflags"]
25+
bytemuck = ["dep:bytemuck", "dep:bytemuck_derive"]
26+
linux-types = []
27+
zerocopy = ["dep:zerocopy", "dep:zerocopy-derive"]

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM rust:latest
2+
ENV CARGO_INCREMENTAL=0 \
3+
CARGO_TERM_COLOR=always
4+
5+
WORKDIR /root/endian-num
6+
COPY . .
7+
RUN set -ex; \
8+
cargo test --all-features; \
9+
cargo clean

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# endian-num
2+
3+
[![Crates.io](https://img.shields.io/crates/v/endian-num)](https://crates.io/crates/endian-num)
4+
[![docs.rs](https://img.shields.io/docsrs/endian-num)](https://docs.rs/endian-num)
5+
[![CI](https://github.com/rust-osdev/endian-num/actions/workflows/ci.yml/badge.svg)](https://github.com/rust-osdev/endian-num/actions/workflows/ci.yml)
6+
7+
This crate provides the [`Be`] (big-endian) and [`Le`] (little-endian) byte-order-aware numeric types.
8+
9+
[`Be`]: https://docs.rs/endian-num/latest/endian_num/struct.Be.html
10+
[`Le`]: https://docs.rs/endian-num/latest/endian_num/struct.Le.html
11+
12+
The core API looks _roughly_ like this (correspondingly for `Be`):
13+
14+
```rust
15+
#[repr(transparent)]
16+
pub struct<T> Le(pub T);
17+
18+
impl Le<T: Integer> {
19+
pub const fn from_ne(n: T) -> Self;
20+
pub const fn from_be(n: Be<T>) -> Self;
21+
22+
pub const fn to_ne(self) -> T;
23+
pub const fn to_be(self) -> Be<T>;
24+
25+
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()];
26+
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()];
27+
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()];
28+
29+
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self;
30+
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self;
31+
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self;
32+
}
33+
```
34+
35+
The types also implement appropriate traits from [`core::cmp`], [`core::convert`], [`core::fmt`], and [`core::ops`] and provide additional helper methods for computations.
36+
37+
For API documentation, see the [docs].
38+
39+
[docs]: https://docs.rs/endian-num
40+
[`core::cmp`]: https://doc.rust-lang.org/stable/core/cmp/index.html
41+
[`core::convert`]: https://doc.rust-lang.org/stable/core/convert/index.html
42+
[`core::fmt`]: https://doc.rust-lang.org/stable/core/fmt/index.html
43+
[`core::ops`]: https://doc.rust-lang.org/stable/core/ops/index.html
44+
45+
46+
## License
47+
48+
Licensed under either of
49+
50+
* Apache License, Version 2.0
51+
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
52+
* MIT license
53+
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
54+
55+
at your option.
56+
57+
### Contribution
58+
59+
Unless you explicitly state otherwise, any contribution intentionally submitted
60+
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
61+
dual licensed as above, without any additional terms or conditions.

rustfmt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
group_imports = "StdExternalCrate"
2+
imports_granularity = "Module"

src/internal_macros.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//! Based on <https://github.com/rust-lang/rust/blob/1.78.0/library/core/src/internal_macros.rs>.
2+
3+
// implements the unary operator "op &T"
4+
// based on "op T" where T is expected to be `Copy`able
5+
macro_rules! forward_ref_unop {
6+
(impl $Trait:ident, $method:ident for $T:ty) => {
7+
impl $Trait for &$T {
8+
type Output = <$T as $Trait>::Output;
9+
10+
#[inline]
11+
#[track_caller]
12+
fn $method(self) -> <$T as $Trait>::Output {
13+
$Trait::$method(*self)
14+
}
15+
}
16+
};
17+
}
18+
19+
// implements binary operators "&T op Rhs", "T op &Rhs", "&T op &Rhs"
20+
// based on "T op Rhs" where T and Rhs are expected to be `Copy`able
21+
macro_rules! forward_ref_binop {
22+
(impl $Trait:ident<$Rhs:ty>, $method:ident for $T:ty) => {
23+
impl<'a> $Trait<$Rhs> for &'a $T {
24+
type Output = <$T as $Trait<$Rhs>>::Output;
25+
26+
#[inline]
27+
#[track_caller]
28+
fn $method(self, rhs: $Rhs) -> <$T as $Trait<$Rhs>>::Output {
29+
$Trait::$method(*self, rhs)
30+
}
31+
}
32+
33+
impl $Trait<&$Rhs> for $T {
34+
type Output = <$T as $Trait<$Rhs>>::Output;
35+
36+
#[inline]
37+
#[track_caller]
38+
fn $method(self, rhs: &$Rhs) -> <$T as $Trait<$Rhs>>::Output {
39+
$Trait::$method(self, *rhs)
40+
}
41+
}
42+
43+
impl $Trait<&$Rhs> for &$T {
44+
type Output = <$T as $Trait<$Rhs>>::Output;
45+
46+
#[inline]
47+
#[track_caller]
48+
fn $method(self, rhs: &$Rhs) -> <$T as $Trait<$Rhs>>::Output {
49+
$Trait::$method(*self, *rhs)
50+
}
51+
}
52+
};
53+
}
54+
55+
// implements "T op= &Rhs", based on "T op= Rhs"
56+
// where Rhs is expected to be `Copy`able
57+
macro_rules! forward_ref_op_assign {
58+
(impl $Trait:ident<$Rhs:ty>, $method:ident for $T:ty) => {
59+
impl $Trait<&$Rhs> for $T {
60+
#[inline]
61+
#[track_caller]
62+
fn $method(&mut self, rhs: &$Rhs) {
63+
$Trait::$method(self, *rhs);
64+
}
65+
}
66+
};
67+
}

0 commit comments

Comments
 (0)