From a7ed6776bdbe347a5d7dbc5c2209f3fe28971eca Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 14 May 2024 11:05:10 +0100 Subject: [PATCH] refactor!: rename `crate::ops::constant::ExtensionValue` => `OpaqueValue` The name `ExtensionValue` clashes 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` --- hugr/src/ops/constant.rs | 24 ++++++++++++------------ hugr/src/ops/constant/custom.rs | 16 ++++++++-------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/hugr/src/ops/constant.rs b/hugr/src/ops/constant.rs index 1f91b6935..e873bf2d3 100644 --- a/hugr/src/ops/constant.rs +++ b/hugr/src/ops/constant.rs @@ -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. @@ -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`](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`](CustomConst). The [ExtensionValue] is +/// [CustomSerialized] into a [`Box`](CustomConst). The [OpaqueValue] is /// then produced from the [`Box`](CustomConst). /// /// In the case where the internal serialised value of a `CustomSerialized` @@ -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; @@ -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, @@ -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, } -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) } } @@ -209,13 +209,13 @@ impl ExtensionValue { } } -impl From for ExtensionValue { +impl From 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()) } @@ -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), } } diff --git a/hugr/src/ops/constant/custom.rs b/hugr/src/ops/constant/custom.rs index ae7bd57e5..58b5de4bf 100644 --- a/hugr/src/ops/constant/custom.rs +++ b/hugr/src/ops/constant/custom.rs @@ -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; /// @@ -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}; @@ -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: CC, @@ -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, @@ -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() ); }