Skip to main content

tarantool/
util.rs

1use crate::error::Error;
2use serde::{Deserialize, Serialize};
3use std::borrow::Cow;
4use std::ffi::CString;
5
6pub use tlua::util::into_cstring_lossy;
7
8/// Exists to optimize code burden with access to mutable statics
9/// and avoid annoying warnings by Clippy about in-place dereference.
10/// It is pretty much a `addr_of*`/`&raw` wrapper without warnings,
11/// so it is developer responsibility to use it correctly, but it
12/// is not marked as unsafe in an expansion to prevent silly mistakes
13/// and to have an opportunity for reviewers to blame someone easily.
14///
15/// Expects `mut` or `const` as a second parameter, to state whether
16/// you want mutable (exclusive) or constant (shared) reference to
17/// the provided mutable static variable as a first parameter.
18///
19/// NOTE: this macro is not that useful if you need a shared reference
20/// to an immutable static, as it is considered safe by default.
21#[macro_export]
22macro_rules! static_ref {
23    (const $var:ident) => {
24        &*&raw const $var
25    };
26    (mut $var:ident) => {
27        &mut *&raw mut $var
28    };
29}
30
31pub trait IntoClones<Tuple>: Clone {
32    fn into_clones(self) -> Tuple;
33}
34
35macro_rules! impl_into_clones {
36    // [@clones(self) T (...)] => [(... self,)]
37    [@clones($self:ident) $h:ident ($($code:tt)*)] => { ($($code)* $self,) };
38    // [@clones(self) T T ... T (...)] => [@clones(self) T ... T (... self.clone(),)]
39    [@clones($self:ident) $h:ident $($t:ident)+ ($($code:tt)*)] => {
40        impl_into_clones![
41            @clones($self) $($t)+ ($($code)* $self.clone(),)
42        ]
43    };
44    {$h:ident $($t:ident)*} => {
45        impl<$h: Clone> IntoClones<($h $(, $t)*,)> for $h {
46            fn into_clones(self) -> ($h $(, $t)*,) {
47                // [@clones(self) T T ... T ()]
48                impl_into_clones![@clones(self) $h $($t)* ()]
49            }
50        }
51        impl_into_clones!{$($t)*}
52    };
53    () => {};
54}
55
56impl_into_clones! {T T T T T T T T T T T}
57
58#[macro_export]
59macro_rules! tuple_from_box_api {
60    ($f:path [ $($args:expr),* , @out ]) => {
61        {
62            let mut result = ::std::mem::MaybeUninit::uninit();
63            #[allow(unused_unsafe)]
64            #[allow(clippy::macro_metavars_in_unsafe)]
65            unsafe {
66                if $f($($args),*, result.as_mut_ptr()) < 0 {
67                    return Err($crate::error::TarantoolError::last().into());
68                }
69                Ok($crate::tuple::Tuple::try_from_ptr(result.assume_init()))
70            }
71        }
72    }
73}
74
75#[macro_export]
76macro_rules! expr_count {
77    () => { 0 };
78    ($head:expr $(, $tail:expr)*) => { 1 + $crate::expr_count!($($tail),*) }
79}
80
81#[inline]
82pub fn rmp_to_vec<T>(val: &T) -> Result<Vec<u8>, Error>
83where
84    T: Serialize + ?Sized,
85{
86    Ok(rmp_serde::to_vec(val)?)
87}
88
89#[derive(Clone, Debug, Serialize, Deserialize, tlua::Push, tlua::LuaRead, PartialEq, Eq, Hash)]
90#[serde(untagged)]
91pub enum NumOrStr {
92    Num(u32),
93    // TODO(gmoshkin): this should be a `&str` instead, but
94    // `#[derive(tlua::Push)]` doesn't support generic parameters yet
95    Str(String),
96}
97
98impl Default for NumOrStr {
99    fn default() -> Self {
100        Self::Num(0)
101    }
102}
103
104impl From<u32> for NumOrStr {
105    #[inline(always)]
106    fn from(n: u32) -> Self {
107        Self::Num(n)
108    }
109}
110
111impl From<String> for NumOrStr {
112    #[inline(always)]
113    fn from(s: String) -> Self {
114        Self::Str(s)
115    }
116}
117
118impl From<NumOrStr> for String {
119    #[inline(always)]
120    fn from(s: NumOrStr) -> Self {
121        match s {
122            NumOrStr::Str(s) => s,
123            NumOrStr::Num(n) => n.to_string(),
124        }
125    }
126}
127
128impl<'a> From<&'a str> for NumOrStr {
129    #[inline(always)]
130    fn from(s: &'a str) -> Self {
131        Self::Str(s.into())
132    }
133}
134
135#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
136#[serde(untagged)]
137pub enum Value<'a> {
138    Num(i64),
139    Double(f64),
140    Str(Cow<'a, str>),
141    Bool(bool),
142}
143
144impl std::hash::Hash for Value<'_> {
145    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
146        match self {
147            Self::Num(v) => v.hash(state),
148            Self::Double(v) => v.to_bits().hash(state),
149            Self::Str(v) => v.hash(state),
150            Self::Bool(v) => v.hash(state),
151        }
152    }
153}
154
155impl Eq for Value<'_> {}
156
157#[rustfmt::skip]
158impl From<bool> for Value<'_> { fn from(v: bool) -> Self { Self::Bool(v) } }
159#[rustfmt::skip]
160impl From<u32> for Value<'_> { fn from(v: u32) -> Self { Self::Num(v as i64) } }
161#[rustfmt::skip]
162impl From<i32> for Value<'_> { fn from(v: i32) -> Self { Self::Num(v as i64) } }
163#[rustfmt::skip]
164impl From<i64> for Value<'_> { fn from(v: i64) -> Self { Self::Num(v) } }
165#[rustfmt::skip]
166impl From<f64> for Value<'_> { fn from(v: f64) -> Self { Self::Double(v) } }
167#[rustfmt::skip]
168impl From<String> for Value<'_> { fn from(v: String) -> Self { Self::Str(v.into()) } }
169#[rustfmt::skip]
170impl<'s> From<&'s str> for Value<'s> { fn from(v: &'s str) -> Self { Self::Str(v.into()) } }
171
172#[macro_export]
173macro_rules! unwrap_or {
174    ($o:expr, $else:expr) => {
175        if let Some(v) = $o {
176            v
177        } else {
178            $else
179        }
180    };
181}
182
183#[macro_export]
184macro_rules! unwrap_ok_or {
185    ($o:expr, $err:pat => $($else:tt)+) => {
186        match $o {
187            Ok(v) => v,
188            $err => $($else)+,
189        }
190    }
191}
192
193////////////////////////////////////////////////////////////////////////////////
194// DisplayAsHexBytes
195////////////////////////////////////////////////////////////////////////////////
196
197/// A wrapper for displaying byte slices as hexadecimal byte slice literals.
198/// ```no_run
199/// # use tarantool::util::DisplayAsHexBytes;
200/// let s = format!("{}", DisplayAsHexBytes(&[1, 2, 3]));
201/// assert_eq!(s, r#"b"\x01\x02\x03""#);
202/// ```
203pub struct DisplayAsHexBytes<'a>(pub &'a [u8]);
204
205impl std::fmt::Display for DisplayAsHexBytes<'_> {
206    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
207        write!(f, "b\"")?;
208        for byte in self.0 {
209            if matches!(byte, b' '..=b'~') {
210                if matches!(byte, b'\\' | b'"') {
211                    write!(f, "\\")?;
212                }
213                write!(f, "{}", *byte as char)?;
214            } else {
215                write!(f, "\\x{byte:02x}")?;
216            }
217        }
218        write!(f, "\"")?;
219        Ok(())
220    }
221}
222
223////////////////////////////////////////////////////////////////////////////////
224// DisplayAsMPValue
225////////////////////////////////////////////////////////////////////////////////
226
227pub(crate) struct DebugAsMPValue<'a>(pub &'a [u8]);
228
229impl std::fmt::Debug for DebugAsMPValue<'_> {
230    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
231        let mut read = self.0;
232        match rmp_serde::from_read::<_, rmpv::Value>(&mut read) {
233            Ok(v) => write!(f, "{v:?}"),
234            Err(_) => write!(f, "{:?}", self.0),
235        }
236    }
237}
238
239////////////////////////////////////////////////////////////////////////////////
240// str_eq
241////////////////////////////////////////////////////////////////////////////////
242
243/// Compares strings for equality.
244///
245/// Works at compile time unlike [`std::cmp::Eq`].
246pub const fn str_eq(lhs: &str, rhs: &str) -> bool {
247    let lhs = lhs.as_bytes();
248    let rhs = rhs.as_bytes();
249    if lhs.len() != rhs.len() {
250        return false;
251    }
252    let mut i = 0;
253    loop {
254        if i == lhs.len() {
255            return true;
256        }
257        if lhs[i] != rhs[i] {
258            return false;
259        }
260        i += 1;
261    }
262}
263
264////////////////////////////////////////////////////////////////////////////////
265// to_cstring
266////////////////////////////////////////////////////////////////////////////////
267
268/// Convert `s` to a `CString` replacing any nul-bytes with `'�'` symbols.
269///
270/// Use this function when you need to unconditionally convert a rust string to
271/// a c string without failing for any reason (other then out-of-memory), for
272/// example when converting error messages.
273#[inline(always)]
274pub fn to_cstring_lossy(s: &str) -> CString {
275    into_cstring_lossy(s.into())
276}
277
278////////////////////////////////////////////////////////////////////////////////
279// test
280////////////////////////////////////////////////////////////////////////////////
281
282#[cfg(test)]
283mod test {
284    use super::*;
285    use pretty_assertions::assert_eq;
286
287    #[test]
288    #[allow(clippy::needless_range_loop)]
289    fn display_as_hex_bytes() {
290        let mut buf = [0_u8; 256];
291        for i in 0..256 {
292            buf[i] = i as _;
293        }
294
295        let s = format!("{}", DisplayAsHexBytes(&buf));
296        // Copy paste this into rust (or python) source to make sure it compiles.
297        assert_eq!(s, r###"
298b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
299        "###.trim());
300    }
301
302    #[rustfmt::skip]
303    #[test]
304    fn check_to_cstring_lossy() {
305        let message = String::from("hell\0 w\0rld\0");
306        assert!(message.as_bytes().contains(&0));
307        assert_eq!(to_cstring_lossy(&message).as_ref(), crate::c_str!("hell� w�rld�"));
308
309        assert_eq!(into_cstring_lossy(message).as_ref(), crate::c_str!("hell� w�rld�"));
310    }
311}