tarantool/
util.rs

1use crate::error::Error;
2use serde::{Deserialize, Serialize};
3use std::borrow::Cow;
4use std::ffi::CString;
5
6/// Exists to optimize code burden with access to mutable statics
7/// and avoid annoying warnings by Clippy about in-place dereference.
8/// It is pretty much a `addr_of*`/`&raw` wrapper without warnings,
9/// so it is developer responsibility to use it correctly, but it
10/// is not marked as unsafe in an expansion to prevent silly mistakes
11/// and to have an opportunity for reviewers to blame someone easily.
12///
13/// Expects `mut` or `const` as a second parameter, to state whether
14/// you want mutable (exclusive) or constant (shared) reference to
15/// the provided mutable static variable as a first parameter.
16///
17/// NOTE: this macro is not that useful if you need a shared reference
18/// to an immutable static, as it is considered safe by default.
19#[macro_export]
20macro_rules! static_ref {
21    (const $var:ident) => {
22        &*&raw const $var
23    };
24    (mut $var:ident) => {
25        &mut *&raw mut $var
26    };
27}
28
29pub trait IntoClones<Tuple>: Clone {
30    fn into_clones(self) -> Tuple;
31}
32
33macro_rules! impl_into_clones {
34    // [@clones(self) T (...)] => [(... self,)]
35    [@clones($self:ident) $h:ident ($($code:tt)*)] => { ($($code)* $self,) };
36    // [@clones(self) T T ... T (...)] => [@clones(self) T ... T (... self.clone(),)]
37    [@clones($self:ident) $h:ident $($t:ident)+ ($($code:tt)*)] => {
38        impl_into_clones![
39            @clones($self) $($t)+ ($($code)* $self.clone(),)
40        ]
41    };
42    {$h:ident $($t:ident)*} => {
43        impl<$h: Clone> IntoClones<($h $(, $t)*,)> for $h {
44            fn into_clones(self) -> ($h $(, $t)*,) {
45                // [@clones(self) T T ... T ()]
46                impl_into_clones![@clones(self) $h $($t)* ()]
47            }
48        }
49        impl_into_clones!{$($t)*}
50    };
51    () => {};
52}
53
54impl_into_clones! {T T T T T T T T T T T}
55
56#[macro_export]
57macro_rules! tuple_from_box_api {
58    ($f:path [ $($args:expr),* , @out ]) => {
59        {
60            let mut result = ::std::mem::MaybeUninit::uninit();
61            #[allow(unused_unsafe)]
62            #[allow(clippy::macro_metavars_in_unsafe)]
63            unsafe {
64                if $f($($args),*, result.as_mut_ptr()) < 0 {
65                    return Err($crate::error::TarantoolError::last().into());
66                }
67                Ok($crate::tuple::Tuple::try_from_ptr(result.assume_init()))
68            }
69        }
70    }
71}
72
73#[macro_export]
74macro_rules! expr_count {
75    () => { 0 };
76    ($head:expr $(, $tail:expr)*) => { 1 + $crate::expr_count!($($tail),*) }
77}
78
79/// Return an array reference to the first `N` items in the slice.
80/// If the slice is not at least N in length, this will return None.
81/// Equivalent to similar slice (as primitive) method (`slice::first_chunk`).
82#[inline]
83pub const fn slice_first_chunk<const N: usize, T>(slice: &[T]) -> Option<&[T; N]> {
84    if slice.len() < N {
85        None
86    } else {
87        // SAFETY: We explicitly check for the correct number
88        // of elements, and do not let the reference outlive the slice.
89        Some(unsafe { &*(slice.as_ptr().cast::<[T; N]>()) })
90    }
91}
92
93#[inline]
94pub fn rmp_to_vec<T>(val: &T) -> Result<Vec<u8>, Error>
95where
96    T: Serialize + ?Sized,
97{
98    Ok(rmp_serde::to_vec(val)?)
99}
100
101#[derive(Clone, Debug, Serialize, Deserialize, tlua::Push, tlua::LuaRead, PartialEq, Eq, Hash)]
102#[serde(untagged)]
103pub enum NumOrStr {
104    Num(u32),
105    // TODO(gmoshkin): this should be a `&str` instead, but
106    // `#[derive(tlua::Push)]` doesn't support generic parameters yet
107    Str(String),
108}
109
110impl Default for NumOrStr {
111    fn default() -> Self {
112        Self::Num(0)
113    }
114}
115
116impl From<u32> for NumOrStr {
117    #[inline(always)]
118    fn from(n: u32) -> Self {
119        Self::Num(n)
120    }
121}
122
123impl From<String> for NumOrStr {
124    #[inline(always)]
125    fn from(s: String) -> Self {
126        Self::Str(s)
127    }
128}
129
130impl From<NumOrStr> for String {
131    #[inline(always)]
132    fn from(s: NumOrStr) -> Self {
133        match s {
134            NumOrStr::Str(s) => s,
135            NumOrStr::Num(n) => n.to_string(),
136        }
137    }
138}
139
140impl<'a> From<&'a str> for NumOrStr {
141    #[inline(always)]
142    fn from(s: &'a str) -> Self {
143        Self::Str(s.into())
144    }
145}
146
147#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
148#[serde(untagged)]
149pub enum Value<'a> {
150    Num(u32),
151    Double(f64),
152    Str(Cow<'a, str>),
153    Bool(bool),
154}
155
156impl std::hash::Hash for Value<'_> {
157    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
158        match self {
159            Self::Num(v) => v.hash(state),
160            Self::Double(v) => v.to_bits().hash(state),
161            Self::Str(v) => v.hash(state),
162            Self::Bool(v) => v.hash(state),
163        }
164    }
165}
166
167impl Eq for Value<'_> {}
168
169#[rustfmt::skip]
170impl From<bool> for Value<'_> { fn from(v: bool) -> Self { Self::Bool(v) } }
171#[rustfmt::skip]
172impl From<u32> for Value<'_> { fn from(v: u32) -> Self { Self::Num(v) } }
173#[rustfmt::skip]
174impl From<f64> for Value<'_> { fn from(v: f64) -> Self { Self::Double(v) } }
175#[rustfmt::skip]
176impl From<String> for Value<'_> { fn from(v: String) -> Self { Self::Str(v.into()) } }
177#[rustfmt::skip]
178impl<'s> From<&'s str> for Value<'s> { fn from(v: &'s str) -> Self { Self::Str(v.into()) } }
179
180#[macro_export]
181macro_rules! unwrap_or {
182    ($o:expr, $else:expr) => {
183        if let Some(v) = $o {
184            v
185        } else {
186            $else
187        }
188    };
189}
190
191#[macro_export]
192macro_rules! unwrap_ok_or {
193    ($o:expr, $err:pat => $($else:tt)+) => {
194        match $o {
195            Ok(v) => v,
196            $err => $($else)+,
197        }
198    }
199}
200
201////////////////////////////////////////////////////////////////////////////////
202// DisplayAsHexBytes
203////////////////////////////////////////////////////////////////////////////////
204
205/// A wrapper for displaying byte slices as hexadecimal byte slice literals.
206/// ```no_run
207/// # use tarantool::util::DisplayAsHexBytes;
208/// let s = format!("{}", DisplayAsHexBytes(&[1, 2, 3]));
209/// assert_eq!(s, r#"b"\x01\x02\x03""#);
210/// ```
211pub struct DisplayAsHexBytes<'a>(pub &'a [u8]);
212
213impl std::fmt::Display for DisplayAsHexBytes<'_> {
214    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
215        write!(f, "b\"")?;
216        for byte in self.0 {
217            if matches!(byte, b' '..=b'~') {
218                if matches!(byte, b'\\' | b'"') {
219                    write!(f, "\\")?;
220                }
221                write!(f, "{}", *byte as char)?;
222            } else {
223                write!(f, "\\x{byte:02x}")?;
224            }
225        }
226        write!(f, "\"")?;
227        Ok(())
228    }
229}
230
231////////////////////////////////////////////////////////////////////////////////
232// DisplayAsMPValue
233////////////////////////////////////////////////////////////////////////////////
234
235pub(crate) struct DebugAsMPValue<'a>(pub &'a [u8]);
236
237impl std::fmt::Debug for DebugAsMPValue<'_> {
238    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
239        let mut read = self.0;
240        match rmp_serde::from_read::<_, rmpv::Value>(&mut read) {
241            Ok(v) => write!(f, "{:?}", v),
242            Err(_) => write!(f, "{:?}", self.0),
243        }
244    }
245}
246
247////////////////////////////////////////////////////////////////////////////////
248// str_eq
249////////////////////////////////////////////////////////////////////////////////
250
251/// Compares strings for equality.
252///
253/// Works at compile time unlike [`std::cmp::Eq`].
254pub const fn str_eq(lhs: &str, rhs: &str) -> bool {
255    let lhs = lhs.as_bytes();
256    let rhs = rhs.as_bytes();
257    if lhs.len() != rhs.len() {
258        return false;
259    }
260    let mut i = 0;
261    loop {
262        if i == lhs.len() {
263            return true;
264        }
265        if lhs[i] != rhs[i] {
266            return false;
267        }
268        i += 1;
269    }
270}
271
272////////////////////////////////////////////////////////////////////////////////
273// to_cstring
274////////////////////////////////////////////////////////////////////////////////
275
276/// Convert `s` to a `CString` replacing any nul-bytes with `'�'` symbols.
277///
278/// Use this function when you need to unconditionally convert a rust string to
279/// a c string without failing for any reason (other then out-of-memory), for
280/// example when converting error messages.
281#[inline(always)]
282pub fn to_cstring_lossy(s: &str) -> CString {
283    into_cstring_lossy(s.into())
284}
285
286/// Convert `s` into a `CString` replacing any nul-bytes with `'�'` symbols.
287///
288/// Use this function when you need to unconditionally convert a rust string to
289/// a c string without failing for any reason (other then out-of-memory), for
290/// example when converting error messages.
291#[inline]
292pub fn into_cstring_lossy(s: String) -> CString {
293    match CString::new(s) {
294        Ok(cstring) => cstring,
295        Err(e) => {
296            // Safety: the already Vec was a String a moment earlier
297            let s = unsafe { String::from_utf8_unchecked(e.into_vec()) };
298            // The same character String::from_utf8_lossy uses to replace non-utf8 bytes
299            let s = s.replace('\0', "�");
300            // Safety: s no longer contains any nul bytes.
301            unsafe { CString::from_vec_unchecked(s.into()) }
302        }
303    }
304}
305
306////////////////////////////////////////////////////////////////////////////////
307// test
308////////////////////////////////////////////////////////////////////////////////
309
310#[cfg(test)]
311mod test {
312    use super::*;
313    use pretty_assertions::assert_eq;
314
315    #[test]
316    #[cfg(miri)]
317    fn check_slice_first_chunk() {
318        let data = &[1u8, 2, 3, 4];
319        assert_eq!(slice_first_chunk::<2, _>(data), Some(&[1, 2]));
320        assert_eq!(slice_first_chunk::<5, _>(data), None);
321    }
322
323    #[test]
324    #[allow(clippy::needless_range_loop)]
325    fn display_as_hex_bytes() {
326        let mut buf = [0_u8; 256];
327        for i in 0..256 {
328            buf[i] = i as _;
329        }
330
331        let s = format!("{}", DisplayAsHexBytes(&buf));
332        // Copy paste this into rust (or python) source to make sure it compiles.
333        assert_eq!(s, r###"
334b"\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"
335        "###.trim());
336    }
337
338    #[rustfmt::skip]
339    #[test]
340    fn check_to_cstring_lossy() {
341        let message = String::from("hell\0 w\0rld\0");
342        assert!(message.as_bytes().contains(&0));
343        assert_eq!(to_cstring_lossy(&message).as_ref(), crate::c_str!("hell� w�rld�"));
344
345        assert_eq!(into_cstring_lossy(message).as_ref(), crate::c_str!("hell� w�rld�"));
346    }
347}