Skip to main content

formally_support/
sarc.rs

1//
2// ::formally - the open-source formal methods toolchain
3//
4// Copyright (c) 2025 Nicola Gigante
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23//
24
25use std::{
26    fmt::{Debug, Formatter},
27    hash::{Hash, Hasher},
28    ops::Deref,
29    sync::Arc,
30};
31
32/// Either a static reference or an [Arc].
33///
34/// [SArc] is useful when declaring types that can refer to static data as well as data initialized
35/// at runtime. [Arc::new()] is not usable in `const` contexts, in which case [SArc::Static] can be
36/// used, while using [SArc::Arc] for all other uses.
37pub enum SArc<T: 'static + ?Sized> {
38    Static(&'static T),
39    Arc(Arc<T>),
40}
41
42impl<T: 'static + ?Sized> Deref for SArc<T> {
43    type Target = T;
44
45    fn deref(&self) -> &Self::Target {
46        match self {
47            SArc::Static(st) => st,
48            SArc::Arc(arc) => arc,
49        }
50    }
51}
52
53
54impl<T: 'static + ?Sized + Hash> Hash for SArc<T> {
55    fn hash<H: Hasher>(&self, state: &mut H) {
56        self.deref().hash(state)
57    }
58}
59
60impl<T: 'static + ?Sized + PartialEq> PartialEq for SArc<T> {
61    fn eq(&self, other: &Self) -> bool {
62        *self.deref() == *other.deref()
63    }
64}
65
66impl<T: 'static + ?Sized + Eq> Eq for SArc<T> {}
67
68impl<T: 'static + ?Sized> From<Box<T>> for SArc<T> {
69    fn from(value: Box<T>) -> Self {
70        SArc::Arc(Arc::from(value))
71    }
72}
73
74impl<T: 'static + Default> Default for SArc<T> {
75    fn default() -> Self {
76        SArc::Arc(Arc::default())
77    }
78}
79
80impl<T: 'static> Default for SArc<[T]> {
81    fn default() -> Self {
82        SArc::Arc(Arc::default())
83    }
84}
85
86impl<T: 'static + ?Sized> Clone for SArc<T> {
87    fn clone(&self) -> Self {
88        match self {
89            SArc::Static(st) => SArc::Static(st),
90            SArc::Arc(arc) => SArc::Arc(arc.clone()),
91        }
92    }
93}
94
95impl<T: Debug> Debug for SArc<T> {
96    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
97        write!(f, "{:?}", **self)
98    }
99}
100
101impl<T: 'static> SArc<T> {
102    pub fn new(value: T) -> SArc<T> {
103        SArc::Arc(Arc::new(value))
104    }
105}
106