use crate::{
context::{Context, Shared},
value::ValueId,
};
pub struct BaseRef<Ctx, Id: Copy> {
pub id: Id,
pub(in crate::value) ctx: Ctx,
}
impl<Ctx, Id: Copy> BaseRef<Ctx, Id> {
pub fn new(ctx: Ctx, id: Id) -> Self {
BaseRef { id, ctx }
}
}
impl<Ctx, Id: Copy + Into<ValueId>> BaseRef<Ctx, Id> {
pub fn id(&self) -> ValueId {
self.id.into()
}
}
impl<'str, 'ctx, Id: Copy> BaseRef<&'ctx Context<'str>, Id> {
pub fn from_id(ctx: &'ctx Context<'str>, id: Id) -> Self {
BaseRef { id, ctx }
}
}
impl<'a, 'str, Id: Copy> BaseRef<&'a Shared<'str>, Id> {
pub fn from_id(src: impl AsShared<'a, 'str>, id: Id) -> Self {
BaseRef {
id,
ctx: src.as_shared(),
}
}
}
impl<'str, 'ctx, Id: Copy> BaseRef<&'ctx mut Context<'str>, Id> {
pub fn from_id(ctx: &'ctx mut Context<'str>, id: Id) -> Self {
BaseRef { id, ctx }
}
}
impl<'str, 'ctx, Id: Copy> From<BaseRef<&'ctx mut Context<'str>, Id>>
for BaseRef<&'ctx Context<'str>, Id>
{
fn from(r: BaseRef<&'ctx mut Context<'str>, Id>) -> Self {
BaseRef {
id: r.id,
ctx: r.ctx,
}
}
}
pub trait WithCtx<'s, 'ctx: 's, 'str: 'ctx> {
fn ctx(&'s self) -> &'ctx Context<'str>;
}
pub trait AsShared<'a, 'str> {
#[allow(clippy::wrong_self_convention)]
fn as_shared(self) -> &'a Shared<'str>;
}
impl<'a, 'str> AsShared<'a, 'str> for &'a Shared<'str> {
fn as_shared(self) -> &'a Shared<'str> {
self
}
}
impl<'a, 'str> AsShared<'a, 'str> for &'a Context<'str> {
fn as_shared(self) -> &'a Shared<'str> {
&self.shared
}
}
pub trait WithShared<'s, 'sh: 's, 'str: 'sh> {
fn shared(&'s self) -> &'sh Shared<'str>;
}
pub trait WithCtxMut<'s, 'str: 's>: WithCtx<'s, 's, 'str> {
fn ctx_mut(&'s mut self) -> &'s mut Context<'str>;
}