1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! High-level traits to translate the low-level API to idiomatic Rust.

use atof::*;
use atoi::*;
use error::Error;
use ftoa::*;
use itoa::*;
use lib;

// FROM BYTES

/// Trait for numerical types that can be parsed from bytes.
pub trait FromBytes: Sized {
    /// Deserialize from byte slice.
    fn from_bytes(bytes: &[u8], base: u8) -> Self;

    /// Error-checking deserialize from byte slice.
    fn try_from_bytes(bytes: &[u8], base: u8) -> Result<Self, Error>;
}

macro_rules! from_bytes {
    ($t:ty, $bytes_cb:ident, $try_bytes_cb:ident) => (
        impl FromBytes for $t {
            #[inline(always)]
            fn from_bytes(bytes: &[u8], base: u8) -> $t
            {
                // We reverse the argument order, since the low-level API
                // always uses (base: u8, first: *const u8, last: *const u8)
                $bytes_cb(base, bytes)
            }

            #[inline(always)]
            fn try_from_bytes(bytes: &[u8], base: u8) -> Result<$t, Error>
            {
                // We reverse the argument order, since the low-level API
                // always uses (base: u8, first: *const u8, last: *const u8)
                $try_bytes_cb(base, bytes)
            }
        }
    )
}

from_bytes!(u8, atou8_bytes, try_atou8_bytes);
from_bytes!(u16, atou16_bytes, try_atou16_bytes);
from_bytes!(u32, atou32_bytes, try_atou32_bytes);
from_bytes!(u64, atou64_bytes, try_atou64_bytes);
from_bytes!(usize, atousize_bytes, try_atousize_bytes);
from_bytes!(i8, atoi8_bytes, try_atoi8_bytes);
from_bytes!(i16, atoi16_bytes, try_atoi16_bytes);
from_bytes!(i32, atoi32_bytes, try_atoi32_bytes);
from_bytes!(i64, atoi64_bytes, try_atoi64_bytes);
from_bytes!(isize, atoisize_bytes, try_atoisize_bytes);
from_bytes!(f32, atof32_bytes, try_atof32_bytes);
from_bytes!(f64, atof64_bytes, try_atof64_bytes);

// FROM BYTES LOSSY

/// Trait for floating-point types that can be parsed using lossy algorithms from bytes.
pub trait FromBytesLossy: FromBytes {
    /// Deserialize from byte slice.
    fn from_bytes_lossy(bytes: &[u8], base: u8) -> Self;

    /// Error-checking deserialize from byte slice.
    fn try_from_bytes_lossy(bytes: &[u8], base: u8) -> Result<Self, Error>;
}

macro_rules! from_bytes_lossy {
    ($t:ty, $bytes_cb:ident, $try_bytes_cb:ident) => (
        impl FromBytesLossy for $t {
            #[inline(always)]
            fn from_bytes_lossy(bytes: &[u8], base: u8) -> $t
            {
                // We reverse the argument order, since the low-level API
                // always uses (base: u8, first: *const u8, last: *const u8)
                $bytes_cb(base, bytes)
            }

            #[inline(always)]
            fn try_from_bytes_lossy(bytes: &[u8], base: u8) -> Result<$t, Error>
            {
                // We reverse the argument order, since the low-level API
                // always uses (base: u8, first: *const u8, last: *const u8)
                $try_bytes_cb(base, bytes)
            }
        }
    )
}

from_bytes_lossy!(f32, atof32_lossy_bytes, try_atof32_lossy_bytes);
from_bytes_lossy!(f64, atof64_lossy_bytes, try_atof64_lossy_bytes);

// TO BYTES

/// Trait for numerical types that can be serialized to bytes.
pub trait ToBytes: Sized {
    /// Serialize to string.
    fn to_bytes(&self, base: u8) -> lib::Vec<u8>;
}

macro_rules! to_bytes {
    ($t:ty, $string_cb:ident) => (
        impl ToBytes for $t {
            #[inline(always)]
            fn to_bytes(&self, base: u8) -> lib::Vec<u8>
            {
                $string_cb(*self, base)
            }
        }
    )
}

to_bytes!(u8, u8toa_bytes);
to_bytes!(u16, u16toa_bytes);
to_bytes!(u32, u32toa_bytes);
to_bytes!(u64, u64toa_bytes);
to_bytes!(usize, usizetoa_bytes);
to_bytes!(i8, i8toa_bytes);
to_bytes!(i16, i16toa_bytes);
to_bytes!(i32, i32toa_bytes);
to_bytes!(i64, i64toa_bytes);
to_bytes!(isize, isizetoa_bytes);
to_bytes!(f32, f32toa_bytes);
to_bytes!(f64, f64toa_bytes);

// TESTS
// -----

#[cfg(test)]
mod tests {
    use error::invalid_digit;
    use super::*;

    macro_rules! deserialize_int {
        ($($t:tt)*) => ($({
            assert_eq!($t::from_bytes(b"0", 10), 0);
            assert_eq!($t::try_from_bytes(b"0", 10), Ok(0));
            assert_eq!($t::try_from_bytes(b"", 10), Err(invalid_digit(0)));
            assert_eq!($t::try_from_bytes(b"1a", 10), Err(invalid_digit(1)));
        })*)
    }

    macro_rules! deserialize_float {
        ($($t:tt)*) => ($({
            assert_eq!($t::from_bytes(b"0.0", 10), 0.0);
            assert_eq!($t::from_bytes_lossy(b"0.0", 10), 0.0);
            assert_eq!($t::try_from_bytes(b"0.0", 10), Ok(0.0));
            assert_eq!($t::try_from_bytes(b"0.0a", 10), Err(invalid_digit(3)));
            assert_eq!($t::try_from_bytes_lossy(b"0.0", 10), Ok(0.0));
        })*)
    }

    #[test]
    fn from_bytes_test() {
        deserialize_int! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
        deserialize_float! { f32 f64 }
    }

    macro_rules! serialize_int {
        ($($t:tt)*) => ($({
            let x: $t = 0;
            assert_eq!(x.to_bytes(10), b"0".to_vec());
        })*)
    }

    macro_rules! serialize_float {
        ($($t:tt)*) => ($({
            let x: $t = 0.0;
            assert_eq!(x.to_bytes(10), b"0.0".to_vec());
        })*)
    }

    #[test]
    fn to_bytes_test() {
        serialize_int! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
        serialize_float! { f32 f64 }
    }
}