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
//! `tinystr-raw` exports functions to convert byte strings to raw TinyStr data.
//!
//! Not intended for public consumption; use `tinystr` instead.

mod error;
mod helpers;

pub use error::Error;

use std::num::{NonZeroU128, NonZeroU32, NonZeroU64};

#[inline(always)]
pub fn try_u32_from_bytes(bytes: &[u8]) -> Result<NonZeroU32, Error> {
    unsafe {
        match bytes.len() {
            1 => helpers::make_u32_bytes(bytes, 1, 0x80),
            2 => helpers::make_u32_bytes(bytes, 2, 0x8080),
            3 => helpers::make_u32_bytes(bytes, 3, 0x0080_8080),
            4 => helpers::make_u32_bytes(bytes, 4, 0x8080_8080),
            _ => Err(Error::InvalidSize),
        }
    }
}

#[test]
fn test_u32_from_bytes() {
    assert_eq!(
        NonZeroU32::new(if cfg!(target_endian = "little") {
            0x6262_6161
        } else {
            0x6161_6262
        })
        .unwrap(),
        try_u32_from_bytes(b"aabb").unwrap()
    );
}

#[inline(always)]
pub fn try_u64_from_bytes(bytes: &[u8]) -> Result<NonZeroU64, Error> {
    let len = bytes.len();
    if !(1..=8).contains(&len) {
        return Err(Error::InvalidSize);
    }
    let mask = 0x8080_8080_8080_8080_u64 >> (8 * (8 - len));
    unsafe { helpers::make_u64_bytes(bytes, len, mask) }
}

#[test]
fn test_u64_from_bytes() {
    assert_eq!(
        NonZeroU64::new(if cfg!(target_endian = "little") {
            0x6262_6262_6161_6161
        } else {
            0x6161_6161_6262_6262
        })
        .unwrap(),
        try_u64_from_bytes(b"aaaabbbb").unwrap()
    );
}

#[inline(always)]
pub fn try_u128_from_bytes(bytes: &[u8]) -> Result<NonZeroU128, Error> {
    let len = bytes.len();
    if !(1..=16).contains(&len) {
        return Err(Error::InvalidSize);
    }
    let mask = 0x8080_8080_8080_8080_8080_8080_8080_8080_u128 >> (8 * (16 - len));
    unsafe { helpers::make_u128_bytes(bytes, len, mask) }
}

#[test]
fn test_u128_from_bytes() {
    assert_eq!(
        NonZeroU128::new(if cfg!(target_endian = "little") {
            0x6262_6262_6262_6262_6161_6161_6161_6161
        } else {
            0x6161_6161_6161_6161_6262_6262_6262_6262
        })
        .unwrap(),
        try_u128_from_bytes(b"aaaaaaaabbbbbbbb").unwrap()
    );
}