// SPDX-License-Identifier: Apache-2.0
// Copyright (c) The pliron contributors
//! Utility traits such as [Named], [Verify] etc.
use crate::{context::Context, ident, identifier::Identifier, result::Result};
/// Check and ensure correctness.
pub trait Verify {
fn verify(&self, ctx: &Context) -> Result<()>;
}
/// Anything that has a name.
pub trait Named {
/// A (not necessarily unique) name.
fn given_name(&self, ctx: &Context) -> Option<Identifier>;
/// A Unique (within the context) ID.
fn id(&self, ctx: &Context) -> Identifier;
/// A unique name; concatenation of name and id.
fn unique_name(&self, ctx: &Context) -> Identifier {
match self.given_name(ctx) {
Some(given_name) => given_name + ident!("_") + self.id(ctx),
None => self.id(ctx),
}
}
}
/// For reference-counted containers, [share](Self::share) data by increasing the reference count.
/// This is equivalent in semantics to (i.e., [Rc::clone](alloc::rc::Rc::clone)),
/// but with a goal of having a less ambiguous name.
pub trait RcShare {
/// Share this object with someone else by increasing the reference count.
fn share(&self) -> Self;
}