pub trait CustomConst:
Send
+ Sync
+ Debug
+ TryHash
+ CustomConstBoxClone
+ Any
+ Downcast
+ Serialize
+ Deserialize {
// Required methods
fn name(&self) -> ValueName;
fn extension_reqs(&self) -> ExtensionSet;
fn get_type(&self) -> Type;
// Provided methods
fn validate(&self) -> Result<(), CustomCheckFailure> { ... }
fn equal_consts(&self, _other: &dyn CustomConst) -> bool { ... }
}
Expand description
Extensible constant values.
We use typetag to provide an impl Serialize for dyn CustomConst
, and
similarly serde::Deserialize. When implementing this trait, include the
#[typetag::serde]
attribute to enable serialization.
Note that when serializing through the [dyn CustomConst
] a dictionary will
be serialized with two attributes, "c"
the tag and "v"
the
CustomConst
:
use serde::{Serialize,Deserialize};
use hugr::{
types::Type,ops::constant::{OpaqueValue, ValueName, CustomConst},
extension::ExtensionSet, std_extensions::arithmetic::int_types};
use serde_json::json;
#[derive(std::fmt::Debug, Clone, Hash, Serialize,Deserialize)]
struct CC(i64);
#[typetag::serde]
impl CustomConst for CC {
fn name(&self) -> ValueName { "CC".into() }
fn extension_reqs(&self) -> ExtensionSet { ExtensionSet::singleton(&int_types::EXTENSION_ID) }
fn get_type(&self) -> Type { int_types::INT_TYPES[5].clone() }
}
assert_eq!(serde_json::to_value(CC(2)).unwrap(), json!(2));
assert_eq!(serde_json::to_value(&CC(2) as &dyn CustomConst).unwrap(), json!({
"c": "CC",
"v": 2
}));
Required Methods§
sourcefn extension_reqs(&self) -> ExtensionSet
fn extension_reqs(&self) -> ExtensionSet
Provided Methods§
sourcefn validate(&self) -> Result<(), CustomCheckFailure>
fn validate(&self) -> Result<(), CustomCheckFailure>
Check the value.
sourcefn equal_consts(&self, _other: &dyn CustomConst) -> bool
fn equal_consts(&self, _other: &dyn CustomConst) -> bool
Compare two constants for equality, using downcasting and comparing the definitions.
If the type implements PartialEq
, use downcast_equal_consts
to compare the values.
Note that this does not require any equivalent of Eq: it is permissible to return
false
if in doubt, and in particular, there is no requirement for reflexivity
(i.e. x.equal_consts(x)
can be false
). However, we do expect both
symmetry (x.equal_consts(y) == y.equal_consts(x)
) and transitivity
(if x.equal_consts(y) && y.equal_consts(z)
then x.equal_consts(z)
).
Implementations§
source§impl dyn CustomConst
impl dyn CustomConst
sourcepub fn is<__T: CustomConst>(&self) -> bool
pub fn is<__T: CustomConst>(&self) -> bool
Returns true if the trait object wraps an object of type __T
.
sourcepub fn downcast<__T: CustomConst>(
self: Box<Self>,
) -> Result<Box<__T>, Box<Self>>
pub fn downcast<__T: CustomConst>( self: Box<Self>, ) -> Result<Box<__T>, Box<Self>>
Returns a boxed object from a boxed trait object if the underlying object is of type
__T
. Returns the original boxed trait if it isn’t.
sourcepub fn downcast_rc<__T: CustomConst>(
self: Rc<Self>,
) -> Result<Rc<__T>, Rc<Self>>
pub fn downcast_rc<__T: CustomConst>( self: Rc<Self>, ) -> Result<Rc<__T>, Rc<Self>>
Returns an Rc
-ed object from an Rc
-ed trait object if the underlying object is of
type __T
. Returns the original Rc
-ed trait if it isn’t.
sourcepub fn downcast_ref<__T: CustomConst>(&self) -> Option<&__T>
pub fn downcast_ref<__T: CustomConst>(&self) -> Option<&__T>
Returns a reference to the object within the trait object if it is of type __T
, or
None
if it isn’t.
sourcepub fn downcast_mut<__T: CustomConst>(&mut self) -> Option<&mut __T>
pub fn downcast_mut<__T: CustomConst>(&mut self) -> Option<&mut __T>
Returns a mutable reference to the object within the trait object if it is of type
__T
, or None
if it isn’t.
Trait Implementations§
source§impl Clone for Box<dyn CustomConst>
impl Clone for Box<dyn CustomConst>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more