hugr_core::ops::constant

Trait CustomConst

source
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§

source

fn name(&self) -> ValueName

An identifier for the constant.

source

fn extension_reqs(&self) -> ExtensionSet

The extension(s) defining the custom constant (a set to allow, say, a List of USize)

source

fn get_type(&self) -> Type

Report the type.

Provided Methods§

source

fn validate(&self) -> Result<(), CustomCheckFailure>

Check the value.

source

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

source

pub fn is<__T: CustomConst>(&self) -> bool

Returns true if the trait object wraps an object of type __T.

source

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.

source

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.

source

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.

source

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>

source§

fn clone(&self) -> Box<dyn CustomConst>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl PartialEq<dyn CustomConst> for ConstExternalSymbol

source§

fn eq(&self, other: &dyn CustomConst) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for dyn CustomConst

source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'typetag> Serialize for dyn CustomConst + 'typetag

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'typetag> Serialize for dyn CustomConst + Send + 'typetag

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'typetag> Serialize for dyn CustomConst + Send + Sync + 'typetag

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'typetag> Serialize for dyn CustomConst + Sync + 'typetag

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Implementors§