use std::borrow::Borrow;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use crate::atom::AtomTable;
use crate::ets::EtsError;
use crate::ets::copy::{OwnedTerm, copy_term_to_ets};
use crate::term::Term;
use crate::term::hash::EtsKey;
use super::TermKey;
pub(crate) struct OwnedEtsKey {
_owned: OwnedTerm,
key: EtsKey,
}
impl OwnedEtsKey {
pub(crate) fn copy_of(key: Term) -> Result<Self, EtsError> {
let owned = copy_term_to_ets(key)?;
let key = EtsKey::new(owned.root());
Ok(Self { _owned: owned, key })
}
}
impl PartialEq for OwnedEtsKey {
fn eq(&self, other: &Self) -> bool {
self.key == other.key
}
}
impl Eq for OwnedEtsKey {}
impl Hash for OwnedEtsKey {
fn hash<H: Hasher>(&self, state: &mut H) {
self.key.hash(state);
}
}
impl Borrow<EtsKey> for OwnedEtsKey {
fn borrow(&self) -> &EtsKey {
&self.key
}
}
pub(crate) struct OwnedTermKey {
_owned: OwnedTerm,
key: TermKey,
}
impl OwnedTermKey {
pub(crate) fn copy_of(key: Term, atom_table: Arc<AtomTable>) -> Result<Self, EtsError> {
let owned = copy_term_to_ets(key)?;
let key = TermKey::with_atom_table(owned.root(), atom_table);
Ok(Self { _owned: owned, key })
}
}
impl PartialEq for OwnedTermKey {
fn eq(&self, other: &Self) -> bool {
self.key == other.key
}
}
impl Eq for OwnedTermKey {}
impl PartialOrd for OwnedTermKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for OwnedTermKey {
fn cmp(&self, other: &Self) -> Ordering {
self.key.cmp(&other.key)
}
}
impl Borrow<TermKey> for OwnedTermKey {
fn borrow(&self) -> &TermKey {
&self.key
}
}