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
#![allow(unsafe_code)]
use crate::ffi::ZStr;
use crate::imp::fd::{AsFd, AsRawFd};
#[cfg(feature = "std")]
use core::fmt;
use core::fmt::Write;
use itoa::{Buffer, Integer};
#[cfg(feature = "std")]
use std::ffi::{CStr, OsStr};
#[cfg(feature = "std")]
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(feature = "std")]
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::OsStrExt;
#[cfg(feature = "std")]
use std::path::Path;
#[derive(Clone)]
pub struct DecInt {
buf: [u8; 20 + 1],
len: usize,
}
impl DecInt {
#[inline]
pub fn new<Int: Integer>(i: Int) -> Self {
let mut me = DecIntWriter(Self {
buf: [0; 20 + 1],
len: 0,
});
let mut buf = Buffer::new();
me.write_str(buf.format(i)).unwrap();
me.0
}
#[inline]
pub fn from_fd<Fd: AsFd>(fd: Fd) -> Self {
Self::new(fd.as_fd().as_raw_fd())
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.buf[..self.len]
}
#[inline]
pub fn as_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
}
#[inline]
pub fn as_z_str(&self) -> &ZStr {
let bytes_with_nul = &self.buf[..=self.len];
debug_assert!(ZStr::from_bytes_with_nul(bytes_with_nul).is_ok());
unsafe { ZStr::from_bytes_with_nul_unchecked(bytes_with_nul) }
}
#[cfg(feature = "std")]
#[inline]
pub fn as_c_str(&self) -> &CStr {
let bytes_with_nul = &self.buf[..=self.len];
debug_assert!(CStr::from_bytes_with_nul(bytes_with_nul).is_ok());
unsafe { CStr::from_bytes_with_nul_unchecked(bytes_with_nul) }
}
}
struct DecIntWriter(DecInt);
impl core::fmt::Write for DecIntWriter {
#[inline]
fn write_str(&mut self, s: &str) -> core::fmt::Result {
match self.0.buf.get_mut(self.0.len..self.0.len + s.len()) {
Some(slice) => {
slice.copy_from_slice(s.as_bytes());
self.0.len += s.len();
Ok(())
}
None => Err(core::fmt::Error),
}
}
}
#[cfg(feature = "std")]
impl AsRef<Path> for DecInt {
#[inline]
fn as_ref(&self) -> &Path {
let as_os_str: &OsStr = OsStrExt::from_bytes(&self.buf[..self.len]);
Path::new(as_os_str)
}
}
#[cfg(feature = "std")]
impl fmt::Debug for DecInt {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(fmt)
}
}