Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for customizable endian (big/little/native) #90

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions impl/src/bitfield/analyse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{
use crate::errors::CombineError;
use core::convert::TryFrom;
use quote::quote;
use std::collections::HashMap;
use std::{collections::HashMap, convert::TryInto};
use syn::{
self,
parse::Result,
Expand Down Expand Up @@ -201,7 +201,24 @@ impl BitfieldStruct {
))
}
}
} else if attr.path.is_ident("skip") {
} else if attr.path.is_ident("endian") {
let path = &attr.path;
let args = &attr.tokens;
let name_value: syn::MetaNameValue =
syn::parse2::<_>(quote! { #path #args })?;
let span = name_value.span();
match name_value.lit {
syn::Lit::Str(lit_str) => {
config.endian(lit_str.value().try_into()?, span)?;
}
_ => {
return Err(format_err!(
span,
"encountered invalid value type for #[endian = Endian]"
))
}
}
}else if attr.path.is_ident("skip") {
let path = &attr.path;
let args = &attr.tokens;
let meta: syn::Meta = syn::parse2::<_>(quote! { #path #args })?;
Expand Down
61 changes: 58 additions & 3 deletions impl/src/bitfield/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use super::field_config::FieldConfig;
use crate::errors::CombineError;
use core::any::TypeId;
use proc_macro2::Span;
use std::collections::{
hash_map::Entry,
HashMap,
use std::{
collections::{
hash_map::Entry,
HashMap,
},
convert::TryFrom, fmt,
};
use syn::parse::Result;

Expand All @@ -16,6 +19,7 @@ pub struct Config {
pub bytes: Option<ConfigValue<usize>>,
pub bits: Option<ConfigValue<usize>>,
pub filled: Option<ConfigValue<bool>>,
pub endian: Option<ConfigValue<Endian>>,
pub repr: Option<ConfigValue<ReprKind>>,
pub derive_debug: Option<ConfigValue<()>>,
pub derive_specifier: Option<ConfigValue<()>>,
Expand Down Expand Up @@ -57,6 +61,42 @@ impl core::fmt::Debug for ReprKind {
}
}

/// Types of endiannes for a `#[bitfield]` struct.
#[derive(Debug, Copy, Clone)]
pub enum Endian {
Little,
Big,
Native,
}

impl TryFrom<String> for Endian {
type Error = ::syn::Error;

fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
match value.as_str() {
"big" => Ok(Endian::Big),
"little" => Ok(Endian::Little),
"native" => Ok(Endian::Native),
invalid => {
Err(format_err!(
invalid,
"encountered invalid value argument for #[bitfield] `endian` parameter",
))
}
}
}
}

impl fmt::Display for Endian {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", match self {
Endian::Big => "big",
Endian::Little => "little",
Endian::Native => "native"
})
}
}

/// A configuration value and its originating span.
#[derive(Clone)]
pub struct ConfigValue<T> {
Expand Down Expand Up @@ -234,6 +274,21 @@ impl Config {
Ok(())
}

/// Sets the `endian: str` #[bitfield] parameter to the given value.
///
/// # Errors
///
/// If the specifier has already been set.
pub fn endian(&mut self, value: Endian, span: Span) -> Result<()> {
match &self.endian {
Some(previous) => {
return Err(Self::raise_duplicate_error("endian", span, previous))
}
None => self.endian = Some(ConfigValue::new(value, span)),
}
Ok(())
}

/// Registers the `#[repr(uN)]` attribute for the #[bitfield] macro.
///
/// # Errors
Expand Down
Loading