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(u32),
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) } }
161#[rustfmt::skip]
162impl From<f64> for Value<'_> { fn from(v: f64) -> Self { Self::Double(v) } }
163#[rustfmt::skip]
164impl From<String> for Value<'_> { fn from(v: String) -> Self { Self::Str(v.into()) } }
165#[rustfmt::skip]
166impl<'s> From<&'s str> for Value<'s> { fn from(v: &'s str) -> Self { Self::Str(v.into()) } }
167
168#[macro_export]
169macro_rules! unwrap_or {
170    ($o:expr, $else:expr) => {
171        if let Some(v) = $o {
172            v
173        } else {
174            $else
175        }
176    };
177}
178
179#[macro_export]
180macro_rules! unwrap_ok_or {
181    ($o:expr, $err:pat => $($else:tt)+) => {
182        match $o {
183            Ok(v) => v,
184            $err => $($else)+,
185        }
186    }
187}
188
189////////////////////////////////////////////////////////////////////////////////
190// DisplayAsHexBytes
191////////////////////////////////////////////////////////////////////////////////
192
193/// A wrapper for displaying byte slices as hexadecimal byte slice literals.
194/// ```no_run
195/// # use tarantool::util::DisplayAsHexBytes;
196/// let s = format!("{}", DisplayAsHexBytes(&[1, 2, 3]));
197/// assert_eq!(s, r#"b"\x01\x02\x03""#);
198/// ```
199pub struct DisplayAsHexBytes<'a>(pub &'a [u8]);
200
201impl std::fmt::Display for DisplayAsHexBytes<'_> {
202    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
203        write!(f, "b\"")?;
204        for byte in self.0 {
205            if matches!(byte, b' '..=b'~') {
206                if matches!(byte, b'\\' | b'"') {
207                    write!(f, "\\")?;
208                }
209                write!(f, "{}", *byte as char)?;
210            } else {
211                write!(f, "\\x{byte:02x}")?;
212            }
213        }
214        write!(f, "\"")?;
215        Ok(())
216    }
217}
218
219////////////////////////////////////////////////////////////////////////////////
220// DisplayAsMPValue
221////////////////////////////////////////////////////////////////////////////////
222
223pub(crate) struct DebugAsMPValue<'a>(pub &'a [u8]);
224
225impl std::fmt::Debug for DebugAsMPValue<'_> {
226    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
227        let mut read = self.0;
228        match rmp_serde::from_read::<_, rmpv::Value>(&mut read) {
229            Ok(v) => write!(f, "{v:?}"),
230            Err(_) => write!(f, "{:?}", self.0),
231        }
232    }
233}
234
235////////////////////////////////////////////////////////////////////////////////
236// str_eq
237////////////////////////////////////////////////////////////////////////////////
238
239/// Compares strings for equality.
240///
241/// Works at compile time unlike [`std::cmp::Eq`].
242pub const fn str_eq(lhs: &str, rhs: &str) -> bool {
243    let lhs = lhs.as_bytes();
244    let rhs = rhs.as_bytes();
245    if lhs.len() != rhs.len() {
246        return false;
247    }
248    let mut i = 0;
249    loop {
250        if i == lhs.len() {
251            return true;
252        }
253        if lhs[i] != rhs[i] {
254            return false;
255        }
256        i += 1;
257    }
258}
259
260////////////////////////////////////////////////////////////////////////////////
261// to_cstring
262////////////////////////////////////////////////////////////////////////////////
263
264/// Convert `s` to a `CString` replacing any nul-bytes with `'�'` symbols.
265///
266/// Use this function when you need to unconditionally convert a rust string to
267/// a c string without failing for any reason (other then out-of-memory), for
268/// example when converting error messages.
269#[inline(always)]
270pub fn to_cstring_lossy(s: &str) -> CString {
271    into_cstring_lossy(s.into())
272}
273
274////////////////////////////////////////////////////////////////////////////////
275// test
276////////////////////////////////////////////////////////////////////////////////
277
278#[cfg(test)]
279mod test {
280    use super::*;
281    use pretty_assertions::assert_eq;
282
283    #[test]
284    #[allow(clippy::needless_range_loop)]
285    fn display_as_hex_bytes() {
286        let mut buf = [0_u8; 256];
287        for i in 0..256 {
288            buf[i] = i as _;
289        }
290
291        let s = format!("{}", DisplayAsHexBytes(&buf));
292        // Copy paste this into rust (or python) source to make sure it compiles.
293        assert_eq!(s, r###"
294b"\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"
295        "###.trim());
296    }
297
298    #[rustfmt::skip]
299    #[test]
300    fn check_to_cstring_lossy() {
301        let message = String::from("hell\0 w\0rld\0");
302        assert!(message.as_bytes().contains(&0));
303        assert_eq!(to_cstring_lossy(&message).as_ref(), crate::c_str!("hell� w�rld�"));
304
305        assert_eq!(into_cstring_lossy(message).as_ref(), crate::c_str!("hell� w�rld�"));
306    }
307}