open_vaf/
util.rs

1/*
2 * ******************************************************************************************
3 * Copyright (c) 2019 Pascal Kuthe. This file is part of the OpenVAF project.
4 * It is subject to the license terms in the LICENSE file found in the top-level directory
5 *  of this distribution and at  https://gitlab.com/DSPOM/OpenVAF/blob/master/LICENSE.
6 *  No part of OpenVAF, including this file, may be copied, modified, propagated, or
7 *  distributed except according to the terms contained in the LICENSE file.
8 * *****************************************************************************************
9 */
10
11use core::fmt::{Display, Formatter};
12macro_rules! unreachable_unchecked{
13    ($reason:expr) => {
14        if cfg!(debug_assertions){
15            $crate::std::unreachable!($reason);
16        }else{
17            //if you call this macro the unsafety is your problem.. You should have good reason for doing so
18            unsafe{$crate::std::hint::unreachable_unchecked()};
19        }
20    };
21}
22
23pub(crate) trait RefMut<T> {
24    fn ref_mut(&mut self) -> Option<&mut T>;
25}
26
27impl<'t, T> RefMut<T> for Option<&'t mut T> {
28    #[inline]
29    fn ref_mut(&mut self) -> Option<&mut T> {
30        self.as_mut().map(|x| &mut **x)
31    }
32}
33
34pub fn format_list<'lt, T: Display>(vec: &'lt [T], seperator: &'lt str) -> VecFormatter<'lt, T> {
35    VecFormatter(vec, seperator, " or ")
36}
37pub struct VecFormatter<'lt, T: Display>(pub &'lt [T], pub &'lt str, pub &'lt str);
38
39impl<'lt, T: Display> Display for VecFormatter<'lt, T> {
40    #[inline]
41    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
42        match self.0 {
43            [] => f.write_str(" "),
44            [x] => {
45                f.write_str(self.1)?;
46                x.fmt(f)?;
47                f.write_str(self.1)
48            }
49            [ref body @ .., second_last, last] => {
50                for x in body {
51                    f.write_str(self.1)?;
52                    x.fmt(f)?;
53                    f.write_str(self.1)?;
54                    f.write_str(", ")?;
55                }
56                f.write_str(self.1)?;
57                second_last.fmt(f)?;
58                f.write_str(self.1)?;
59                f.write_str(self.2)?;
60                f.write_str(self.1)?;
61                last.fmt(f)?;
62                f.write_str(self.1)
63            }
64        }
65    }
66}