use std::sync::Arc;
pub type CpuFn = fn(input: &[u8], output: &mut Vec<u8>);
#[derive(Debug, Clone)]
pub struct BackendId(Arc<str>);
impl BackendId {
#[must_use]
pub fn new(name: impl Into<Arc<str>>) -> Self {
Self(name.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<&str> for BackendId {
fn from(name: &str) -> Self {
Self(Arc::from(name))
}
}
impl From<String> for BackendId {
fn from(name: String) -> Self {
Self(Arc::from(name))
}
}
impl core::fmt::Display for BackendId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.0)
}
}
impl PartialEq for BackendId {
fn eq(&self, other: &Self) -> bool {
self.0.as_ref() == other.0.as_ref()
}
}
impl Eq for BackendId {}
impl core::hash::Hash for BackendId {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.0.as_ref().hash(state);
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Backend {
id: BackendId,
name: Option<Arc<str>>,
}
impl Backend {
#[must_use]
pub fn new(id: impl Into<BackendId>) -> Self {
Self {
id: id.into(),
name: None,
}
}
#[must_use]
pub fn named(id: impl Into<BackendId>, name: impl Into<Arc<str>>) -> Self {
Self {
id: id.into(),
name: Some(name.into()),
}
}
#[must_use]
pub fn id(&self) -> &str {
self.id.as_str()
}
#[must_use]
pub fn name(&self) -> &str {
self.name.as_deref().unwrap_or_else(|| self.id())
}
}
impl From<BackendId> for Backend {
fn from(id: BackendId) -> Self {
Self::new(id)
}
}
impl From<&str> for Backend {
fn from(id: &str) -> Self {
Self::new(BackendId::from(id))
}
}
impl From<&Backend> for BackendId {
fn from(backend: &Backend) -> Self {
backend.id.clone()
}
}
use crate::op_contract::OperationContract;
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct IntrinsicDescriptor {
name: &'static str,
hardware: &'static str,
cpu_fn: CpuFn,
contract: Option<OperationContract>,
}
impl IntrinsicDescriptor {
#[must_use]
pub const fn new(name: &'static str, hardware: &'static str, cpu_fn: CpuFn) -> Self {
Self {
name,
hardware,
cpu_fn,
contract: None,
}
}
#[must_use]
pub const fn with_contract(
name: &'static str,
hardware: &'static str,
cpu_fn: CpuFn,
contract: OperationContract,
) -> Self {
Self {
name,
hardware,
cpu_fn,
contract: Some(contract),
}
}
#[must_use]
pub const fn name(&self) -> &'static str {
self.name
}
#[must_use]
pub const fn hardware(&self) -> &'static str {
self.hardware
}
#[must_use]
pub const fn cpu_fn(&self) -> CpuFn {
self.cpu_fn
}
#[must_use]
pub const fn contract(&self) -> Option<&OperationContract> {
self.contract.as_ref()
}
}