diff --git a/src/human_encoding/named_node.rs b/src/human_encoding/named_node.rs index a4d208a6..f282d6f0 100644 --- a/src/human_encoding/named_node.rs +++ b/src/human_encoding/named_node.rs @@ -413,17 +413,13 @@ impl NamedConstructNode { if self.for_main { // For `main`, only apply type ascriptions *after* inference has completely // determined the type. - let source_bound = - types::Bound::Complete(Arc::clone(&commit_data.arrow().source)); - let source_ty = types::Type::from(source_bound); + let source_ty = types::Type::complete(Arc::clone(&commit_data.arrow().source)); for ty in data.node.cached_data().user_source_types.as_ref() { if let Err(e) = source_ty.unify(ty, "binding source type annotation") { self.errors.add(data.node.position(), e); } } - let target_bound = - types::Bound::Complete(Arc::clone(&commit_data.arrow().target)); - let target_ty = types::Type::from(target_bound); + let target_ty = types::Type::complete(Arc::clone(&commit_data.arrow().target)); for ty in data.node.cached_data().user_target_types.as_ref() { if let Err(e) = target_ty.unify(ty, "binding target type annotation") { self.errors.add(data.node.position(), e); diff --git a/src/human_encoding/parse/ast.rs b/src/human_encoding/parse/ast.rs index 241b606b..f58e9d4e 100644 --- a/src/human_encoding/parse/ast.rs +++ b/src/human_encoding/parse/ast.rs @@ -633,9 +633,7 @@ fn grammar() -> Grammar> { Error::BadWordLength { bit_length }, )); } - let ty = types::Type::two_two_n(bit_length.trailing_zeros() as usize) - .final_data() - .unwrap(); + let ty = types::Final::two_two_n(bit_length.trailing_zeros() as usize); // unwrap ok here since literally every sequence of bits is a valid // value for the given type let value = iter.read_value(&ty).unwrap(); diff --git a/src/types/final_data.rs b/src/types/final_data.rs index 1bfc06e9..8858edd3 100644 --- a/src/types/final_data.rs +++ b/src/types/final_data.rs @@ -12,7 +12,6 @@ //! use crate::dag::{Dag, DagLike, NoSharing}; -use crate::types::{Bound, Type}; use crate::Tmr; use std::sync::Arc; @@ -163,7 +162,7 @@ impl Final { /// /// The type is precomputed and fast to access. pub fn two_two_n(n: usize) -> Arc { - super::precomputed::nth_power_of_2(n).final_data().unwrap() + super::precomputed::nth_power_of_2(n) } /// Create the sum of the given `left` and `right` types. @@ -227,12 +226,6 @@ impl Final { } } -impl From> for Type { - fn from(value: Arc) -> Self { - Type::from(Bound::Complete(value)) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/types/mod.rs b/src/types/mod.rs index 3aad8f91..9461008e 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -402,7 +402,7 @@ impl Type { /// /// The type is precomputed and fast to access. pub fn two_two_n(n: usize) -> Self { - precomputed::nth_power_of_2(n) + Self::complete(precomputed::nth_power_of_2(n)) } /// Create the sum of the given `left` and `right` types. @@ -415,6 +415,11 @@ impl Type { Type::from(Bound::product(left, right)) } + /// Create a complete type. + pub fn complete(final_data: Arc) -> Self { + Type::from(Bound::Complete(final_data)) + } + /// Clones the `Type`. /// /// This is the same as just calling `.clone()` but has a different name to diff --git a/src/types/precomputed.rs b/src/types/precomputed.rs index 86d6a683..fb67ae32 100644 --- a/src/types/precomputed.rs +++ b/src/types/precomputed.rs @@ -14,33 +14,34 @@ use crate::Tmr; -use super::Type; +use super::Final; use std::cell::RefCell; use std::convert::TryInto; +use std::sync::Arc; // Directly use the size of the precomputed TMR table to make sure they're in sync. const N_POWERS: usize = Tmr::POWERS_OF_TWO.len(); thread_local! { - static POWERS_OF_TWO: RefCell> = RefCell::new(None); + static POWERS_OF_TWO: RefCell; N_POWERS]>> = RefCell::new(None); } -fn initialize(write: &mut Option<[Type; N_POWERS]>) { - let one = Type::unit(); +fn initialize(write: &mut Option<[Arc; N_POWERS]>) { + let one = Final::unit(); let mut powers = Vec::with_capacity(N_POWERS); // Two^(2^0) = Two = (One + One) - let mut power = Type::sum(one.shallow_clone(), one); - powers.push(power.shallow_clone()); + let mut power = Final::sum(Arc::clone(&one), one); + powers.push(Arc::clone(&power)); // Two^(2^(i + 1)) = (Two^(2^i) * Two^(2^i)) for _ in 1..N_POWERS { - power = Type::product(power.shallow_clone(), power); - powers.push(power.shallow_clone()); + power = Final::product(Arc::clone(&power), power); + powers.push(Arc::clone(&power)); } - let powers: [Type; N_POWERS] = powers.try_into().unwrap(); + let powers: [Arc; N_POWERS] = powers.try_into().unwrap(); *write = Some(powers); } @@ -49,12 +50,12 @@ fn initialize(write: &mut Option<[Type; N_POWERS]>) { /// # Panics /// /// Panics if you request a number `n` greater than or equal to [`Tmr::POWERS_OF_TWO`]. -pub fn nth_power_of_2(n: usize) -> Type { +pub fn nth_power_of_2(n: usize) -> Arc { POWERS_OF_TWO.with(|arr| { if arr.borrow().is_none() { initialize(&mut arr.borrow_mut()); } debug_assert!(arr.borrow().is_some()); - arr.borrow().as_ref().unwrap()[n].shallow_clone() + Arc::clone(&arr.borrow().as_ref().unwrap()[n]) }) }