Skip to content

Commit 12a1885

Browse files
committed
types: refactor precomputed data to directly store finalize types
This eliminates an `unwrap`, cleans up a bit of code in named_node.rs, and moves every instance of `Type::from` into types/mod.rs, where it will be easy to modify or remove in a later commit. Also removes From<Arc<Final>> for Type, a somewhat-weird From impl which was introduced in #218. It is now superceded by Type::complete. In general I would like to remove From impls from Type because they have an inflexible API and because they have somewhat nonobvious behavior.
1 parent dcba2f4 commit 12a1885

File tree

5 files changed

+22
-29
lines changed

5 files changed

+22
-29
lines changed

src/human_encoding/named_node.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,13 @@ impl<J: Jet> NamedConstructNode<J> {
413413
if self.for_main {
414414
// For `main`, only apply type ascriptions *after* inference has completely
415415
// determined the type.
416-
let source_bound =
417-
types::Bound::Complete(Arc::clone(&commit_data.arrow().source));
418-
let source_ty = types::Type::from(source_bound);
416+
let source_ty = types::Type::complete(Arc::clone(&commit_data.arrow().source));
419417
for ty in data.node.cached_data().user_source_types.as_ref() {
420418
if let Err(e) = source_ty.unify(ty, "binding source type annotation") {
421419
self.errors.add(data.node.position(), e);
422420
}
423421
}
424-
let target_bound =
425-
types::Bound::Complete(Arc::clone(&commit_data.arrow().target));
426-
let target_ty = types::Type::from(target_bound);
422+
let target_ty = types::Type::complete(Arc::clone(&commit_data.arrow().target));
427423
for ty in data.node.cached_data().user_target_types.as_ref() {
428424
if let Err(e) = target_ty.unify(ty, "binding target type annotation") {
429425
self.errors.add(data.node.position(), e);

src/human_encoding/parse/ast.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,7 @@ fn grammar<J: Jet + 'static>() -> Grammar<Ast<J>> {
633633
Error::BadWordLength { bit_length },
634634
));
635635
}
636-
let ty = types::Type::two_two_n(bit_length.trailing_zeros() as usize)
637-
.final_data()
638-
.unwrap();
636+
let ty = types::Final::two_two_n(bit_length.trailing_zeros() as usize);
639637
// unwrap ok here since literally every sequence of bits is a valid
640638
// value for the given type
641639
let value = iter.read_value(&ty).unwrap();

src/types/final_data.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//!
1313
1414
use crate::dag::{Dag, DagLike, NoSharing};
15-
use crate::types::{Bound, Type};
1615
use crate::Tmr;
1716

1817
use std::sync::Arc;
@@ -163,7 +162,7 @@ impl Final {
163162
///
164163
/// The type is precomputed and fast to access.
165164
pub fn two_two_n(n: usize) -> Arc<Self> {
166-
super::precomputed::nth_power_of_2(n).final_data().unwrap()
165+
super::precomputed::nth_power_of_2(n)
167166
}
168167

169168
/// Create the sum of the given `left` and `right` types.
@@ -227,12 +226,6 @@ impl Final {
227226
}
228227
}
229228

230-
impl From<Arc<Final>> for Type {
231-
fn from(value: Arc<Final>) -> Self {
232-
Type::from(Bound::Complete(value))
233-
}
234-
}
235-
236229
#[cfg(test)]
237230
mod tests {
238231
use super::*;

src/types/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl Type {
402402
///
403403
/// The type is precomputed and fast to access.
404404
pub fn two_two_n(n: usize) -> Self {
405-
precomputed::nth_power_of_2(n)
405+
Self::complete(precomputed::nth_power_of_2(n))
406406
}
407407

408408
/// Create the sum of the given `left` and `right` types.
@@ -415,6 +415,11 @@ impl Type {
415415
Type::from(Bound::product(left, right))
416416
}
417417

418+
/// Create a complete type.
419+
pub fn complete(final_data: Arc<Final>) -> Self {
420+
Type::from(Bound::Complete(final_data))
421+
}
422+
418423
/// Clones the `Type`.
419424
///
420425
/// This is the same as just calling `.clone()` but has a different name to

src/types/precomputed.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,34 @@
1414
1515
use crate::Tmr;
1616

17-
use super::Type;
17+
use super::Final;
1818

1919
use std::cell::RefCell;
2020
use std::convert::TryInto;
21+
use std::sync::Arc;
2122

2223
// Directly use the size of the precomputed TMR table to make sure they're in sync.
2324
const N_POWERS: usize = Tmr::POWERS_OF_TWO.len();
2425

2526
thread_local! {
26-
static POWERS_OF_TWO: RefCell<Option<[Type; N_POWERS]>> = RefCell::new(None);
27+
static POWERS_OF_TWO: RefCell<Option<[Arc<Final>; N_POWERS]>> = RefCell::new(None);
2728
}
2829

29-
fn initialize(write: &mut Option<[Type; N_POWERS]>) {
30-
let one = Type::unit();
30+
fn initialize(write: &mut Option<[Arc<Final>; N_POWERS]>) {
31+
let one = Final::unit();
3132
let mut powers = Vec::with_capacity(N_POWERS);
3233

3334
// Two^(2^0) = Two = (One + One)
34-
let mut power = Type::sum(one.shallow_clone(), one);
35-
powers.push(power.shallow_clone());
35+
let mut power = Final::sum(Arc::clone(&one), one);
36+
powers.push(Arc::clone(&power));
3637

3738
// Two^(2^(i + 1)) = (Two^(2^i) * Two^(2^i))
3839
for _ in 1..N_POWERS {
39-
power = Type::product(power.shallow_clone(), power);
40-
powers.push(power.shallow_clone());
40+
power = Final::product(Arc::clone(&power), power);
41+
powers.push(Arc::clone(&power));
4142
}
4243

43-
let powers: [Type; N_POWERS] = powers.try_into().unwrap();
44+
let powers: [Arc<Final>; N_POWERS] = powers.try_into().unwrap();
4445
*write = Some(powers);
4546
}
4647

@@ -49,12 +50,12 @@ fn initialize(write: &mut Option<[Type; N_POWERS]>) {
4950
/// # Panics
5051
///
5152
/// Panics if you request a number `n` greater than or equal to [`Tmr::POWERS_OF_TWO`].
52-
pub fn nth_power_of_2(n: usize) -> Type {
53+
pub fn nth_power_of_2(n: usize) -> Arc<Final> {
5354
POWERS_OF_TWO.with(|arr| {
5455
if arr.borrow().is_none() {
5556
initialize(&mut arr.borrow_mut());
5657
}
5758
debug_assert!(arr.borrow().is_some());
58-
arr.borrow().as_ref().unwrap()[n].shallow_clone()
59+
Arc::clone(&arr.borrow().as_ref().unwrap()[n])
5960
})
6061
}

0 commit comments

Comments
 (0)