Skip to content

Commit

Permalink
refactor!: rename crate::ops::constant::ExtensionValue => `OpaqueVa…
Browse files Browse the repository at this point in the history
…lue` (#1036)

Fixes #989.

The name `ExtensionValue` clashed with
`crate::extension::ExtensionValue`

Note that we have chosen not to rename the corresponding element of the
serialization schema.

BREAKING CHANGE: rename `crate::ops::constant::ExtensionValue` =>
`OpaqueValue`
  • Loading branch information
doug-q authored May 14, 2024
1 parent 81c4465 commit be9ac48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
24 changes: 12 additions & 12 deletions hugr/src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub enum Value {
Extension {
#[serde(flatten)]
/// The custom constant value.
e: ExtensionValue,
e: OpaqueValue,
},
/// A higher-order function value.
// TODO use a root parametrised hugr, e.g. Hugr<DFG>.
Expand Down Expand Up @@ -140,13 +140,13 @@ pub enum Value {
/// During serialization we first serialize the internal [`dyn` CustomConst](CustomConst)
/// into a [serde_yaml::Value]. We then create a [CustomSerialized] wrapping
/// that value. That [CustomSerialized] is then serialized in place of the
/// [ExtensionValue].
/// [OpaqueValue].
///
/// During deserialization, first we deserialize a [CustomSerialized]. We
/// attempt to deserialize the internal [serde_yaml::Value] using the [`Box<dyn
/// CustomConst>`](CustomConst) impl. This will fail if the appropriate `impl CustomConst`
/// is not linked into the running program, in which case we coerce the
/// [CustomSerialized] into a [`Box<dyn CustomConst>`](CustomConst). The [ExtensionValue] is
/// [CustomSerialized] into a [`Box<dyn CustomConst>`](CustomConst). The [OpaqueValue] is
/// then produced from the [`Box<dyn [CustomConst]>`](CustomConst).
///
/// In the case where the internal serialised value of a `CustomSerialized`
Expand All @@ -156,7 +156,7 @@ pub enum Value {
/// ```rust
/// use serde::{Serialize,Deserialize};
/// use hugr::{
/// types::Type,ops::constant::{ExtensionValue, ValueName, CustomConst, CustomSerialized},
/// types::Type,ops::constant::{OpaqueValue, ValueName, CustomConst, CustomSerialized},
/// extension::{ExtensionSet, prelude::{USIZE_T, ConstUsize}},
/// std_extensions::arithmetic::int_types};
/// use serde_json::json;
Expand All @@ -166,11 +166,11 @@ pub enum Value {
/// "typ": USIZE_T,
/// "value": {'c': "ConstUsize", 'v': 1}
/// });
/// let ev = ExtensionValue::new(ConstUsize::new(1));
/// let ev = OpaqueValue::new(ConstUsize::new(1));
/// assert_eq!(&serde_json::to_value(&ev).unwrap(), &expected_json);
/// assert_eq!(ev, serde_json::from_value(expected_json).unwrap());
///
/// let ev = ExtensionValue::new(CustomSerialized::new(USIZE_T.clone(), serde_yaml::Value::Null, ExtensionSet::default()));
/// let ev = OpaqueValue::new(CustomSerialized::new(USIZE_T.clone(), serde_yaml::Value::Null, ExtensionSet::default()));
/// let expected_json = json!({
/// "extensions": [],
/// "typ": USIZE_T,
Expand All @@ -181,13 +181,13 @@ pub enum Value {
/// assert_eq!(ev, serde_json::from_value(expected_json).unwrap());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtensionValue {
pub struct OpaqueValue {
#[serde(flatten, with = "self::custom::serde_extension_value")]
v: Box<dyn CustomConst>,
}

impl ExtensionValue {
/// Create a new [`ExtensionValue`] from any [`CustomConst`].
impl OpaqueValue {
/// Create a new [`OpaqueValue`] from any [`CustomConst`].
pub fn new(cc: impl CustomConst) -> Self {
Self { v: Box::new(cc) }
}
Expand All @@ -209,13 +209,13 @@ impl ExtensionValue {
}
}

impl<CC: CustomConst> From<CC> for ExtensionValue {
impl<CC: CustomConst> From<CC> for OpaqueValue {
fn from(x: CC) -> Self {
Self::new(x)
}
}

impl PartialEq for ExtensionValue {
impl PartialEq for OpaqueValue {
fn eq(&self, other: &Self) -> bool {
self.value().equal_consts(other.value())
}
Expand Down Expand Up @@ -361,7 +361,7 @@ impl Value {
/// Returns a tuple constant of constant values.
pub fn extension(custom_const: impl CustomConst) -> Self {
Self::Extension {
e: ExtensionValue::new(custom_const),
e: OpaqueValue::new(custom_const),
}
}

Expand Down
16 changes: 8 additions & 8 deletions hugr/src/ops/constant/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use super::ValueName;
/// ```rust
/// use serde::{Serialize,Deserialize};
/// use hugr::{
/// types::Type,ops::constant::{ExtensionValue, ValueName, CustomConst},
/// types::Type,ops::constant::{OpaqueValue, ValueName, CustomConst},
/// extension::ExtensionSet, std_extensions::arithmetic::int_types};
/// use serde_json::json;
///
Expand Down Expand Up @@ -264,7 +264,7 @@ impl CustomConst for CustomSerialized {
}
}

/// This module is used by the serde annotations on `super::ExtensionValue`
/// This module is used by the serde annotations on `super::OpaqueValue`
pub(super) mod serde_extension_value {
use serde::{Deserializer, Serializer};

Expand Down Expand Up @@ -323,7 +323,7 @@ mod test {
std_extensions::collections::ListValue,
};

use super::{super::ExtensionValue, CustomConst, CustomConstBoxClone, CustomSerialized};
use super::{super::OpaqueValue, CustomConst, CustomConstBoxClone, CustomSerialized};

struct SerializeCustomConstExample<CC: CustomConst + serde::Serialize + 'static> {
cc: CC,
Expand Down Expand Up @@ -407,8 +407,8 @@ mod test {
.unwrap()
);

// check ExtensionValue serializes/deserializes as a CustomSerialized
let ev: ExtensionValue = example.cc.clone().into();
// check OpaqueValue serializes/deserializes as a CustomSerialized
let ev: OpaqueValue = example.cc.clone().into();
let ev_val = serde_yaml::to_value(&ev).unwrap();
assert_eq!(
&ev_val,
Expand Down Expand Up @@ -465,10 +465,10 @@ mod test {
assert_eq!(&inner.clone_box(), &cs.clone().into_custom_const_box());
assert_eq!(&inner, &cs.clone().try_into_custom_const().unwrap());

let ev: ExtensionValue = cs.clone().into();
// A serialisation round-trip results in an ExtensionValue with the value of inner
let ev: OpaqueValue = cs.clone().into();
// A serialisation round-trip results in an OpaqueValue with the value of inner
assert_eq!(
ExtensionValue::new(inner),
OpaqueValue::new(inner),
serde_yaml::from_value(serde_yaml::to_value(&ev).unwrap()).unwrap()
);
}
Expand Down

0 comments on commit be9ac48

Please sign in to comment.