Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,48 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v5
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Run Clippy on generated test code - fail on warnings
run: cargo clippy -p packed_struct_tests --all-targets -- -D warnings
- name: Check feature combinations
run: |
cargo check -p packed_struct --no-default-features
cargo check -p packed_struct --no-default-features --features alloc
cargo check -p packed_struct --all-features
- name: Rust no_std tests
run: cd packed_struct_nostd_tests && cargo test --verbose


msrv:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v5
- uses: dtolnay/rust-toolchain@1.85
- name: Check on MSRV
run: cargo check --workspace --all-targets

build-cross:
runs-on: ubuntu-latest
needs: [build]

strategy:
fail-fast: false
matrix:
target: [aarch64-unknown-linux-gnu, arm-unknown-linux-gnueabihf, mips64-unknown-linux-gnuabi64, riscv64gc-unknown-linux-gnu ]
target: [aarch64-unknown-linux-gnu, arm-unknown-linux-gnueabihf, s390x-unknown-linux-gnu, riscv64gc-unknown-linux-gnu ]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v5
- uses: dtolnay/rust-toolchain@stable
- name: Install rust-embedded/cross
run: cargo install cross
- name: Cross-build
run: cross build --target ${{ matrix.target }}
- name: Cross-run tests
run: cross test --target ${{ matrix.target }}
run: cross test --target ${{ matrix.target }}
23 changes: 22 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
[workspace]
resolver = "3"
members = [
"packed_struct/",
"packed_struct_codegen/",
"packed_struct_tests/",
"packed_struct_examples/"
]

# Built separately: a workspace-wide build would unify packed_struct_codegen's
# `std` feature (enabled by packed_struct_tests) and emit std paths into this no_std crate.
exclude = [
"packed_struct_nostd_tests/"
]
]

[workspace.package]
version = "0.12.0"
authors = ["Rudi Benkovic <rudi.benkovic@gmail.com>"]
edition = "2024"
rust-version = "1.85"
license = "MIT OR Apache-2.0"
repository = "https://github.com/hashmismatch/packed_struct.rs"

[workspace.dependencies]
packed_struct = { path = "packed_struct/", version = "0.12.0", default-features = false }
packed_struct_codegen = { path = "packed_struct_codegen/", version = "0.12.0" }
syn = { version = "3.0.6", features = ["full"] }
quote = "1.0.47"
proc-macro2 = "1.0.107"
serde = { version = "1.0.229", default-features = false }
bitvec = { version = "1.1.1", default-features = false }
error-chain = "0.12.4"
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ provide safe packing, unpacking and runtime debugging formatters with per-field

# Crate-level feature flags
* `std`: use the Rust standard library. Default.
* `alloc`: use the `alloc` crate for `no_std` + `alloc` scenarios. Requires nightly Rust.
* `alloc`: use the `alloc` crate for `no_std` + `alloc` scenarios.
* `use_serde`: add serialization support to the built-in helper types.
* `byte_types_64`, `byte_types_256`: enlarge the size of the generated array, byte and bit width types.

Expand All @@ -35,13 +35,11 @@ provide safe packing, unpacking and runtime debugging formatters with per-field

```toml
[dependencies]
packed_struct = "0.10"
packed_struct = "0.12"
```
## Importing the library with the the most common traits and the derive macros

```rust
// This is only needed for pre Rust 2018
#[macro_use] extern crate packed_struct;
// Prelude import with the common imports
use packed_struct::prelude::*;
```
Expand Down
23 changes: 11 additions & 12 deletions packed_struct/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
[package]
name = "packed_struct"
description = "Binary-level structure packing and unpacking generator"
repository = "https://github.com/hashmismatch/packed_struct.rs"
homepage = "http://www.hashmismatch.net/libraries/packed-struct/"
version = "0.11.0"
rust-version = "1.51"
authors = ["Rudi Benkovic <rudi.benkovic@gmail.com>"]
license = "MIT OR Apache-2.0"
keywords = ["enum", "packing", "serialization"]
categories = ["encoding"]
readme = "../README.md"
edition = "2018"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
packed_struct_codegen = { path = "../packed_struct_codegen/", version = "0.11.0" }
serde = { version = "1.0", optional = true, default-features = false }
serde_derive = { version = "1.0", optional = true }
bitvec = { version = "1.0", default-features = false }
packed_struct_codegen.workspace = true
serde = { workspace = true, optional = true }
bitvec.workspace = true

[features]
default = ["std"]
std = ["serde/std", "packed_struct_codegen/std"]
std = ["serde?/std", "packed_struct_codegen/std"]
alloc = ["packed_struct_codegen/alloc"]
byte_types_64 = []
byte_types_256 = []
use_serde = ["serde", "serde_derive"]
use_serde = ["dep:serde", "serde/derive"]
5 changes: 4 additions & 1 deletion packed_struct/src/internal_prelude/no_std.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#![allow(unused_imports)]


pub use core::marker::PhantomData;
pub use core::iter;
pub use core::cell::RefCell;
pub use core::fmt;
pub use core::fmt::Formatter;
pub use core::fmt::{Debug, Display};
pub use core::fmt::Write as FmtWrite;
pub use core::fmt::Error as FmtError;
pub use core::ops::Range;
pub use core::num::Wrapping;
pub use core::cmp::*;
pub use core::mem;
pub use core::intrinsics::write_bytes;
pub use core::ptr::write_bytes;
pub use core::ops::Deref;
pub use core::slice;
pub use core::hash::{Hash, Hasher};
Expand Down
2 changes: 2 additions & 0 deletions packed_struct/src/internal_prelude/std.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_imports)]

pub use std::prelude::v1::*;
pub use std::cell::RefCell;
pub use std::rc::Rc;
Expand Down
21 changes: 2 additions & 19 deletions packed_struct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//!
//! # Crate-level feature flags
//! * `std`: use the Rust standard library. Default.
//! * `alloc`: use the `alloc` crate for `no_std` + `alloc` scenarios. Requires nightly Rust.
//! * `alloc`: use the `alloc` crate for `no_std` + `alloc` scenarios.
//! * `use_serde`: add serialization support to the built-in helper types.
//! * `byte_types_64`, `byte_types_256`: enlarge the size of the generated array, byte and bit width types.
//!
Expand All @@ -35,13 +35,11 @@
//!
//! ```toml
//! [dependencies]
//! packed_struct = "0.10"
//! packed_struct = "0.12"
//! ```
//! ## Importing the library with the the most common traits and the derive macros
//!
//! ```rust
//! // This is only needed for pre Rust 2018
//! #[macro_use] extern crate packed_struct;
//! // Prelude import with the common imports
//! use packed_struct::prelude::*;
//! # fn main() {
Expand Down Expand Up @@ -318,19 +316,10 @@

#![cfg_attr(not(feature = "std"), no_std)]

#![cfg_attr(feature="alloc", feature(alloc))]

extern crate packed_struct_codegen;

#[cfg(feature="alloc")]
#[macro_use]
extern crate alloc;

#[cfg(feature = "use_serde")]
extern crate serde;
#[cfg(feature = "use_serde")]
#[macro_use] extern crate serde_derive;

mod internal_prelude;

#[macro_use]
Expand All @@ -357,19 +346,13 @@ mod types_vec;

/// Implementations and wrappers for various packing types.
pub mod types {
pub use super::types_basic::*;

/// Types that specify the exact number of bits a packed integer should occupy.
pub mod bits {
pub use super::super::types_bits::*;
}

pub use super::types_num::*;
pub use super::types_array::*;
pub use super::types_reserved::*;
pub use super::types_generic::*;
#[cfg(any(feature="alloc", feature="std"))]
pub use super::types_vec::*;
}

pub use self::packing::*;
Expand Down
2 changes: 1 addition & 1 deletion packed_struct/src/packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub trait PackedStructSlice where Self: Sized {
}
}

#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "use_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
/// Packing errors that might occur during packing or unpacking
pub enum PackingError {
Expand Down
4 changes: 2 additions & 2 deletions packed_struct/src/types_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ impl<T, B, I> PackedStruct for LsbInteger<T, B, I>
let bits = BitSlice::<_, Msb0>::try_from_slice_mut(bytes_slice).map_err(|_| PackingError::BitsError)?;
let s = l - B::number_of_bits();
let (left, _) = bits.split_at_mut(l - leftover_bits);
left.shift_right(s);
left.shift_end(s);
}

Ok(bytes)
Expand All @@ -693,7 +693,7 @@ impl<T, B, I> PackedStruct for LsbInteger<T, B, I>
let bits = BitSlice::<_, Msb0>::try_from_slice_mut(bytes_slice).map_err(|_| PackingError::BitsError)?;
let s = l - B::number_of_bits();
let (left, _) = bits.split_at_mut(l - leftover_bits);
left.shift_left(s);
left.shift_start(s);

I::from_lsb_bytes(&src_bytes)?
} else {
Expand Down
6 changes: 3 additions & 3 deletions packed_struct/src/types_reserved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait ReservedBitValue {
fn get_reserved_bit_value_byte() -> u8;
}

#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "use_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Copy, Clone, PartialEq, Eq, Hash)]
pub struct BitOne;
impl ReservedBitValue for BitOne {
Expand All @@ -23,7 +23,7 @@ impl ReservedBitValue for BitOne {
}
}

#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "use_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Copy, Clone, PartialEq, Eq, Hash)]
pub struct BitZero;
impl ReservedBitValue for BitZero {
Expand All @@ -33,7 +33,7 @@ impl ReservedBitValue for BitZero {
}

/// Always packs into the associated bit value. Ignores the input when unpacking.
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "use_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ReservedBits<V, B> {
value: V,
Expand Down
17 changes: 9 additions & 8 deletions packed_struct_codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
[package]
name = "packed_struct_codegen"
description = "This crate implements the code generation for the packed_struct library."
repository = "https://github.com/hashmismatch/packed_struct.rs"
version = "0.11.0"
license = "MIT OR Apache-2.0"
authors = ["Rudi Benkovic <rudi.benkovic@gmail.com>"]
edition = "2018"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
quote = "1.0"
syn = { version = "2.0", features = ["full"] }
proc-macro2 = "1.0"
quote.workspace = true
syn.workspace = true
proc-macro2.workspace = true

[lib]
proc-macro = true
Expand Down
2 changes: 0 additions & 2 deletions packed_struct_codegen/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate syn;

#[cfg(feature="std")]
pub fn collections_prefix() -> syn::Type {
syn::parse_str("::std").unwrap()
Expand Down
7 changes: 0 additions & 7 deletions packed_struct_codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#![recursion_limit = "192"]
#![allow(clippy::redundant_clone)]

extern crate proc_macro;
extern crate proc_macro2;

extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use syn::{DeriveInput, parse_macro_input};

Expand Down
5 changes: 0 additions & 5 deletions packed_struct_codegen/src/pack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern crate quote;
extern crate syn;

use std::ops::*;
use crate::pack_parse::*;

Expand All @@ -17,7 +14,6 @@ pub enum FieldKind {
},
Array {
ident: syn::Ident,
size: usize,
elements: Vec<FieldRegular>
}
}
Expand Down Expand Up @@ -48,7 +44,6 @@ pub struct PackStruct<'a> {
pub fields: Vec<FieldKind>,
pub num_bytes: usize,
pub num_bits: usize,
pub data_struct: &'a syn::DataStruct,
pub derive_input: &'a syn::DeriveInput
}

Expand Down
Loading
Loading