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

Convert to bytes via associated type #1

Open
wants to merge 4 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
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
[package]
name = "endian-type"
version = "0.1.2"
authors = ["Lolirofle <lolipopple@hotmail.com>"]
description = "Type safe wrappers for types with a defined byte order"
version = "0.1.3"
authors = ["Lolirofle <lolipopple@hotmail.com>","Jeffrey Burdges <burdges@gnunet.org>"]

description = "Type safe wrappers for byte order with conversion from/to byte arrays."
#documentation = "..."

homepage = "https://github.com/Lolirofle/endian-type"
repository = "https://github.com/Lolirofle/endian-type.git"

#readme = "README.md"

keywords = ["endian","byteorder"]

license = "MIT"

[dependencies]


153 changes: 107 additions & 46 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
use std::{mem,slice};
use std::convert::{From,Into};
use std::ops::{BitAnd,BitOr,BitXor};

extern crate core;

// use core::intrinsics::transmute;
use core::{mem,slice};
// use core::mem::size_of;
use core::convert::{From,Into};
use core::ops::{BitAnd,BitOr,BitXor};

///Type with a specified byte order
pub trait Endian<T>{}
pub trait Endian<T>{
type Bytes;
}

macro_rules! EndianBytes{
($e:ident,$t:ty) => {
<$e<$t> as Endian<$t>>::Bytes
}
}

macro_rules! impl_EndianBytes{
($e:ident,$t:ty,$size:expr) => {
impl Endian<$t> for $e<$t> {
type Bytes = [u8; $size];
}

impl Into<EndianBytes!($e,$t)> for $e<$t> {
#[inline]
fn into(self) -> EndianBytes!($e,$t) {
unsafe { mem::transmute::<$t,EndianBytes!($e,$t)>(self.0) }
}
}
// Cannot impl From<$e<$t>> for EndianBytes!($e,$t) { .. }
// because "only traits defined in the current crate can be
// implemented for a type parameter" like ::Bytes.

impl From<EndianBytes!($e,$t)> for $e<$t> {
#[inline]
fn from(v: EndianBytes!($e,$t)) -> Self {
$e( unsafe { mem::transmute::<EndianBytes!($e,$t),$t>(v) } )
}
}
// Provides Impl Into<$e<$t>> for EndianBytes!($e,$t) { .. }
// but maybe one should impl manually for inline
}
}

macro_rules! impl_EndianNoBytes{
($e:ident,$t:ty) => {
impl Endian<$t> for $e<$t> {
type Bytes = ();
}
}
}

macro_rules! impl_Endian{
($e:ident) => {
impl_EndianBytes!($e,u16,2);
impl_EndianBytes!($e,u32,4);
impl_EndianBytes!($e,u64,8);
impl_EndianNoBytes!($e,usize);
impl_EndianBytes!($e,i16,2);
impl_EndianBytes!($e,i32,4);
impl_EndianBytes!($e,i64,8);
impl_EndianNoBytes!($e,isize);
// impl_EndianBytes!($e,f32,4);
// impl_EndianBytes!($e,f64,8);
}
}

macro_rules! impl_EndianOps{
( for $e:ident) => {
impl<T> BitAnd for $e<T>
where T: BitAnd
Expand Down Expand Up @@ -64,39 +127,49 @@ macro_rules! impl_Endian{
}
}



///Big endian byte order
///
///Most significant byte first
#[derive(Copy,Clone,Debug,Eq,PartialEq,Hash,Ord,PartialOrd)]
pub struct BigEndian<T>(T);
impl<T> Endian<T> for BigEndian<T>{}
macro_rules! impl_for_BigEndian{
( $t:ident ) => {
impl Into<$t> for BigEndian<$t>{
macro_rules! impl_for_Endian{
( $t:ident, $me:ident, $from_e:ident, $to_e:ident, $op:ident ) => {
impl Into<$t> for $me<$t>{
#[inline]
fn into(self) -> $t{
$t::from_be(self.0)
$t::$from_e(self.0)
}
}
}
// Seemingly supurfluous, except maybe for the inline

impl From<$t> for BigEndian<$t>{
impl From<$t> for $me<$t>{
#[inline]
fn from(data: $t) -> Self{
BigEndian(data.to_be())
$me(data.$to_e())
}
}

impl From<LittleEndian<$t>> for BigEndian<$t>{
impl From<$op<$t>> for $me<$t>{
#[inline]
fn from(data: LittleEndian<$t>) -> Self{
BigEndian(data.0.swap_bytes())
fn from(data: $op<$t>) -> Self{
$me(data.0.swap_bytes())
}
}
}
}
impl_Endian!(for BigEndian);


///Big endian byte order
///
///Most significant byte first
#[derive(Copy,Clone,Debug,Eq,PartialEq,Hash,Ord,PartialOrd)]
pub struct BigEndian<T>(T);
impl_EndianOps!(for BigEndian);

// impl<T> Endian<T> for BigEndian<T>{}
impl_Endian!(BigEndian);

macro_rules! impl_for_BigEndian{
( $t:ident ) => {
impl_for_Endian!($t,BigEndian,from_be,to_be,LittleEndian);
}
}

impl_for_BigEndian!(u16);
impl_for_BigEndian!(u32);
impl_for_BigEndian!(u64);
Expand All @@ -105,40 +178,26 @@ impl_for_BigEndian!(i16);
impl_for_BigEndian!(i32);
impl_for_BigEndian!(i64);
impl_for_BigEndian!(isize);

// impl_for_BigEndian!(f32);
// impl_for_BigEndian!(f64);


///Little endian byte order
///
///Least significant byte first
#[derive(Copy,Clone,Debug,Eq,PartialEq,Hash,Ord,PartialOrd)]
pub struct LittleEndian<T>(T);
impl<T> Endian<T> for LittleEndian<T>{}
macro_rules! impl_for_LittleEndian{
( $t:ident ) => {
impl Into<$t> for LittleEndian<$t>{
#[inline]
fn into(self) -> $t{
$t::from_le(self.0)
}
}
impl_EndianOps!(for LittleEndian);

impl From<$t> for LittleEndian<$t>{
#[inline]
fn from(data: $t) -> Self{
LittleEndian(data.to_le())
}
}
// impl<T> Endian<T> for LittleEndian<T>{}
impl_Endian!(LittleEndian);

impl From<BigEndian<$t>> for LittleEndian<$t>{
#[inline]
fn from(data: BigEndian<$t>) -> Self{
LittleEndian(data.0.swap_bytes())
}
}
macro_rules! impl_for_LittleEndian{
( $t:ident ) => {
impl_for_Endian!($t,LittleEndian,from_le,to_le,BigEndian);
}
}
impl_Endian!(for LittleEndian);

impl_for_LittleEndian!(u16);
impl_for_LittleEndian!(u32);
impl_for_LittleEndian!(u64);
Expand All @@ -147,6 +206,8 @@ impl_for_LittleEndian!(i16);
impl_for_LittleEndian!(i32);
impl_for_LittleEndian!(i64);
impl_for_LittleEndian!(isize);
// impl_for_LittleEndian!(f32);
// impl_for_LittleEndian!(f64);


///Network byte order as defined by IETF RFC1700 [http://tools.ietf.org/html/rfc1700]
Expand Down