1#![cfg_attr(not(feature = "std"), no_std)]
4
5#[cfg(not(feature = "std"))]
6extern crate alloc;
7
8use core::fmt;
9
10pub use ::smol_str;
13
14pub mod bigint;
15pub mod byte_array;
16pub mod casts;
17pub mod collection_arithmetics;
18pub mod deque;
19pub mod extract_matches;
20#[cfg(feature = "std")]
21pub mod graph_algos;
22#[cfg(feature = "std")]
23pub mod heap_size;
24pub mod iterators;
25#[cfg(feature = "tracing")]
26pub mod logging;
27pub mod ordered_hash_map;
28pub mod ordered_hash_set;
29#[cfg(feature = "std")]
30pub mod small_ordered_map;
31pub mod unordered_hash_map;
32pub mod unordered_hash_set;
33
34#[cfg(feature = "std")]
35pub use heap_size::HeapSize;
36
37pub trait OptionFrom<T>: Sized {
39 fn option_from(other: T) -> Option<Self>;
40}
41
42pub fn write_comma_separated<Iter: IntoIterator<Item = V>, V: core::fmt::Display>(
43 f: &mut fmt::Formatter<'_>,
44 values: Iter,
45) -> fmt::Result {
46 let mut iter = values.into_iter();
47 if let Some(value) = iter.next() {
48 write!(f, "{value}")?;
49 }
50 for value in iter {
51 write!(f, ", {value}")?;
52 }
53 Ok(())
54}
55
56pub trait OptionHelper {
58 fn on_none<F: FnOnce()>(self, f: F) -> Self;
59}
60impl<T> OptionHelper for Option<T> {
61 fn on_none<F: FnOnce()>(self, f: F) -> Self {
62 if self.is_none() {
63 f();
64 }
65 self
66 }
67}
68
69#[cfg(feature = "std")]
71pub trait CloneableDatabase: salsa::Database + Send {
72 fn dyn_clone(&self) -> Box<dyn CloneableDatabase>;
74}
75
76#[cfg(feature = "std")]
78impl Clone for Box<dyn CloneableDatabase> {
79 fn clone(&self) -> Self {
80 self.dyn_clone()
81 }
82}
83
84#[cfg(feature = "std")]
85pub trait Intern<'db, Target> {
86 fn intern(self, db: &'db dyn salsa::Database) -> Target;
87}
88
89#[macro_export]
99macro_rules! define_short_id {
100 ($short_id:ident, $long_id:path) => {
101 #[cairo_lang_proc_macros::interned(revisions = usize::MAX)]
103 pub struct $short_id<'db> {
104 #[returns(ref)]
105 pub long: $long_id,
106 }
107
108 impl<'db> std::fmt::Debug for $short_id<'db> {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110 write!(f, "{}({:x})", stringify!($short_id), self.as_intern_id().index())
111 }
112 }
113
114 impl<'db> cairo_lang_utils::Intern<'db, $short_id<'db>> for $long_id {
115 fn intern(self, db: &'db dyn salsa::Database) -> $short_id<'db> {
116 $short_id::new(db, self)
117 }
118 }
119
120 impl<'db> $short_id<'db> {
121 pub fn from_intern_id(intern_id: salsa::Id) -> Self {
122 use salsa::plumbing::FromId;
123 Self::from_id(intern_id)
124 }
125
126 pub fn as_intern_id(self) -> salsa::Id {
127 use salsa::plumbing::AsId;
128 self.as_id()
129 }
130 }
131
132 impl<'db> cairo_lang_debug::DebugWithDb<'db> for $short_id<'db> {
134 type Db = dyn salsa::Database;
135 fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &'db Self::Db) -> std::fmt::Result {
136 use core::fmt::Debug;
137
138 use cairo_lang_debug::helper::{Fallback, HelperDebug};
139
140 HelperDebug::<$long_id, dyn salsa::Database>::helper_debug(self.long(db), db).fmt(f)
141 }
142 }
143
144 impl<'db> cairo_lang_utils::HeapSize for $short_id<'db> {
147 fn heap_size(&self) -> usize {
148 0
149 }
150 }
151 };
152}
153
154#[must_use = "This function is only relevant to create a possible return."]
161pub fn require(condition: bool) -> Option<()> {
162 condition.then_some(())
163}