#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
extern crate alloc;
use core::fmt;
pub use ::smol_str;
pub mod bigint;
pub mod byte_array;
pub mod casts;
pub mod collection_arithmetics;
pub mod deque;
pub mod extract_matches;
#[cfg(feature = "std")]
pub mod graph_algos;
#[cfg(feature = "std")]
pub mod heap_size;
pub mod iterators;
#[cfg(feature = "tracing")]
pub mod logging;
pub mod ordered_hash_map;
pub mod ordered_hash_set;
#[cfg(feature = "std")]
pub mod small_ordered_map;
pub mod unordered_hash_map;
pub mod unordered_hash_set;
#[cfg(feature = "std")]
pub use heap_size::HeapSize;
pub trait OptionFrom<T>: Sized {
fn option_from(other: T) -> Option<Self>;
}
pub fn write_comma_separated<Iter: IntoIterator<Item = V>, V: core::fmt::Display>(
f: &mut fmt::Formatter<'_>,
values: Iter,
) -> fmt::Result {
let mut iter = values.into_iter();
if let Some(value) = iter.next() {
write!(f, "{value}")?;
}
for value in iter {
write!(f, ", {value}")?;
}
Ok(())
}
pub trait OptionHelper {
fn on_none<F: FnOnce()>(self, f: F) -> Self;
}
impl<T> OptionHelper for Option<T> {
fn on_none<F: FnOnce()>(self, f: F) -> Self {
if self.is_none() {
f();
}
self
}
}
#[cfg(feature = "std")]
pub trait CloneableDatabase: salsa::Database + Send {
fn dyn_clone(&self) -> Box<dyn CloneableDatabase>;
}
#[cfg(feature = "std")]
impl Clone for Box<dyn CloneableDatabase> {
fn clone(&self) -> Self {
self.dyn_clone()
}
}
#[cfg(feature = "std")]
pub trait Intern<'db, Target> {
fn intern(self, db: &'db dyn salsa::Database) -> Target;
}
#[macro_export]
macro_rules! define_short_id {
($short_id:ident, $long_id:path) => {
#[cairo_lang_proc_macros::interned(revisions = usize::MAX)]
pub struct $short_id<'db> {
#[returns(ref)]
pub long: $long_id,
}
impl<'db> std::fmt::Debug for $short_id<'db> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({:x})", stringify!($short_id), self.as_intern_id().index())
}
}
impl<'db> cairo_lang_utils::Intern<'db, $short_id<'db>> for $long_id {
fn intern(self, db: &'db dyn salsa::Database) -> $short_id<'db> {
$short_id::new(db, self)
}
}
impl<'db> $short_id<'db> {
pub fn from_intern_id(intern_id: salsa::Id) -> Self {
use salsa::plumbing::FromId;
Self::from_id(intern_id)
}
pub fn as_intern_id(self) -> salsa::Id {
use salsa::plumbing::AsId;
self.as_id()
}
}
impl<'db> cairo_lang_debug::DebugWithDb<'db> for $short_id<'db> {
type Db = dyn salsa::Database;
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &'db Self::Db) -> std::fmt::Result {
use core::fmt::Debug;
use cairo_lang_debug::helper::{Fallback, HelperDebug};
HelperDebug::<$long_id, dyn salsa::Database>::helper_debug(self.long(db), db).fmt(f)
}
}
impl<'db> cairo_lang_utils::HeapSize for $short_id<'db> {
fn heap_size(&self) -> usize {
0
}
}
};
}
#[must_use = "This function is only relevant to create a possible return."]
pub fn require(condition: bool) -> Option<()> {
condition.then_some(())
}