use crate::{asm::TryFromBytes, bytecode::BytecodeMapped};
use std::sync::Arc;
pub trait OpAccess: Clone + Send + Sync {
type Op;
type Error: core::fmt::Debug + core::fmt::Display + Send;
fn op_access(&self, index: usize) -> Option<Result<Self::Op, Self::Error>>;
}
impl<Op> OpAccess for &[Op]
where
Op: Clone + Send + Sync,
{
type Op = Op;
type Error = core::convert::Infallible;
fn op_access(&self, index: usize) -> Option<Result<Self::Op, Self::Error>> {
self.get(index).cloned().map(Ok)
}
}
impl<Op, Bytes> OpAccess for &BytecodeMapped<Op, Bytes>
where
Op: TryFromBytes + Send + Sync,
Bytes: core::ops::Deref<Target = [u8]> + Send + Sync,
{
type Op = Op;
type Error = core::convert::Infallible;
fn op_access(&self, index: usize) -> Option<Result<Self::Op, Self::Error>> {
self.op(index).map(Ok)
}
}
impl<T> OpAccess for Arc<T>
where
T: OpAccess,
{
type Op = T::Op;
type Error = T::Error;
fn op_access(&self, index: usize) -> Option<Result<Self::Op, Self::Error>> {
(**self).op_access(index)
}
}