astd/
lib.rs

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
//! This crate provides core definitions that need to be shared between the
//! various wrapper utilities.
#![deny(missing_docs)]
#![deny(unused_results)]
#![deny(rust_2018_idioms)]

mod macros;
mod source_location;

pub use source_location::*;

pub mod convert;
pub mod ffi;
pub mod meta;
pub mod ptr;

/// A module for providing extension utility functions to [`u16`] types.
pub mod u16 {
  /// A small function for forming a `u16` from a string at compile-time.
  ///
  /// # Panics
  ///
  /// This function will panic if the input string is not a valid decimal
  /// (radix10) string.
  pub const fn from_str_radix10(s: &str) -> u16 {
    let mut bytes = s.as_bytes();
    let mut val: u16 = 0;
    while let [byte, rest @ ..] = bytes {
      assert!(b'0' <= *byte && *byte <= b'9', "invalid digit");
      val = val * 10 + (*byte - b'0') as u16;
      bytes = rest;
    }
    val
  }
}

/// Computes the null-terminated string length of a C-string.
///
/// This function exists since the current FFI helpers are not `const` functions,
/// and this enables checking for the value at compile-time.
///
/// This is an implementation-detail of [`c_str!`], which simply delegates the
/// call to [`_null_terminated_length_b`].
///
/// # Parameters
///
/// * `s` - the string slice
#[doc(hidden)]
pub const fn _null_terminated_length(s: &str) -> usize {
  _null_terminated_length_b(s.as_bytes(), 0, 0)
}

/// Computes the null-terminated string length of a C-string.
///
/// This counts each independent non-null byte, and will stop executing when it
/// exceeds
#[doc(hidden)]
pub const fn _null_terminated_length_b(bytes: &[u8], index: usize, count: usize) -> usize {
  if bytes[index] == 0 {
    count
  } else {
    _null_terminated_length_b(bytes, index + 1, count + 1)
  }
}

#[doc(inline)]
pub use uuid::Uuid;

unsafe impl convert::Transparent for Uuid {
  type Wrapped = uuid::Bytes;
}