From 784e7f170b7f9a5656fa96f42434f096de843e8d Mon Sep 17 00:00:00 2001 From: Jay Kickliter Date: Mon, 22 Apr 2024 16:33:13 -0600 Subject: [PATCH] impl TryFrom for Cell --- src/cell.rs | 10 +++++++++- src/iteration.rs | 37 ++++++++++++++++++------------------- tests/tests.rs | 17 ++++++++--------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/cell.rs b/src/cell.rs index 0e18a8f..7d84baa 100755 --- a/src/cell.rs +++ b/src/cell.rs @@ -225,6 +225,14 @@ impl TryFrom for Cell { } } +impl TryFrom for Cell { + type Error = Error; + + fn try_from(raw: i64) -> Result { + Cell::from_raw(raw as u64) + } +} + /// A type for building up Cells in an iterative matter when /// tree-walking. pub(crate) struct CellStack(Option); @@ -351,7 +359,7 @@ mod tests { #[test] fn test_cell_to_parent() { - let cell = Cell::try_from(0x85283473fffffff).unwrap(); + let cell = Cell::from_raw(0x85283473fffffff).unwrap(); let parent = cell.to_parent(cell.res()).unwrap(); assert_eq!(cell, parent); let parent = cell.to_parent(4).unwrap(); diff --git a/src/iteration.rs b/src/iteration.rs index b04372f..8d7baaf 100644 --- a/src/iteration.rs +++ b/src/iteration.rs @@ -210,19 +210,18 @@ mod tests { geom::{ContainmentMode, PolyfillConfig, Polygon, ToCells}, CellIndex, Resolution, }; - use std::convert::TryFrom; #[test] fn test_visit() { - let parent = Cell::try_from(0x825997fffffffff).unwrap(); + let parent = Cell::from_raw(0x825997fffffffff).unwrap(); let children = [ - Cell::try_from(0x835990fffffffff).unwrap(), - Cell::try_from(0x835991fffffffff).unwrap(), - Cell::try_from(0x835992fffffffff).unwrap(), - Cell::try_from(0x835993fffffffff).unwrap(), - Cell::try_from(0x835994fffffffff).unwrap(), - Cell::try_from(0x835995fffffffff).unwrap(), - Cell::try_from(0x835996fffffffff).unwrap(), + Cell::from_raw(0x835990fffffffff).unwrap(), + Cell::from_raw(0x835991fffffffff).unwrap(), + Cell::from_raw(0x835992fffffffff).unwrap(), + Cell::from_raw(0x835993fffffffff).unwrap(), + Cell::from_raw(0x835994fffffffff).unwrap(), + Cell::from_raw(0x835995fffffffff).unwrap(), + Cell::from_raw(0x835996fffffffff).unwrap(), ]; let hexmap: HexTreeMap = children.iter().map(|cell| (cell, cell)).collect(); @@ -243,7 +242,7 @@ mod tests { let mut map = HexTreeMap::new(); for cell in COMPACT_US915_INDICES .iter() - .map(|&idx| Cell::try_from(idx).unwrap()) + .map(|&idx| Cell::from_raw(idx).unwrap()) { map.insert(cell, cell); } @@ -262,7 +261,7 @@ mod tests { let mut map = HexTreeMap::new(); for cell in COMPACT_US915_INDICES .iter() - .map(|&idx| Cell::try_from(idx).unwrap()) + .map(|&idx| Cell::from_raw(idx).unwrap()) { map.insert(cell, cell); } @@ -283,7 +282,7 @@ mod tests { let mut cell_value_pairs: Vec<(Cell, i32)> = Vec::new(); let mut count = 0; while let Ok(idx) = rdr.read_u64::() { - cell_value_pairs.push((Cell::try_from(idx).unwrap(), count)); + cell_value_pairs.push((Cell::from_raw(idx).unwrap(), count)); count += 1; } cell_value_pairs @@ -367,7 +366,7 @@ mod tests { eiffel_tower_cells.dedup(); eiffel_tower_cells .into_iter() - .map(|cell| Cell::try_from(u64::from(cell)).unwrap()) + .map(|cell| Cell::from_raw(u64::from(cell)).unwrap()) .collect::>() }; let mut hex_map: HexTreeMap = eiffel_tower_cells @@ -375,7 +374,7 @@ mod tests { .enumerate() .map(|(i, &cell)| (cell, i as i32)) .collect(); - let eiffel_tower_res1_parent = Cell::try_from(0x811fbffffffffff).unwrap(); + let eiffel_tower_res1_parent = Cell::from_raw(0x811fbffffffffff).unwrap(); let value_sum: i32 = hex_map .subtree_iter(eiffel_tower_res1_parent) .map(|(_cell, val)| val) @@ -383,8 +382,8 @@ mod tests { // Establish the sum of map values in the eiffel tower block. assert_eq!(value_sum, 22578); - let west_mask_res9 = Cell::try_from(0x891fb46741bffff).unwrap(); - let east_mask_res9 = Cell::try_from(0x891fb467413ffff).unwrap(); + let west_mask_res9 = Cell::from_raw(0x891fb46741bffff).unwrap(); + let east_mask_res9 = Cell::from_raw(0x891fb467413ffff).unwrap(); let west_value_sum: i32 = hex_map .subtree_iter(west_mask_res9) .map(|(_cell, val)| val) @@ -421,18 +420,18 @@ mod tests { // https://wolf-h3-viewer.glitch.me/?h3=863969a47ffffff let monaco_res6_cellidx = CellIndex::try_from(0x863969a47ffffff).unwrap(); - let monaco_res6_cell = Cell::try_from(u64::from(monaco_res6_cellidx)).unwrap(); + let monaco_res6_cell = Cell::from_raw(u64::from(monaco_res6_cellidx)).unwrap(); // https://wolf-h3-viewer.glitch.me/?h3=863969a6fffffff let not_monaco_res6_cellidx = CellIndex::try_from(0x863969a6fffffff).unwrap(); let monaco_res10_cells = monaco_res6_cellidx .children(Resolution::Ten) - .map(|ci| Cell::try_from(u64::from(ci)).unwrap()) + .map(|ci| Cell::from_raw(u64::from(ci)).unwrap()) .collect::>(); let not_monaco_res10_cells = not_monaco_res6_cellidx .children(Resolution::Ten) - .map(|ci| Cell::try_from(u64::from(ci)).unwrap()) + .map(|ci| Cell::from_raw(u64::from(ci)).unwrap()) .collect::>(); let monaco_hextree: HexTreeMap<(), NullCompactor> = monaco_res10_cells diff --git a/tests/tests.rs b/tests/tests.rs index 76493c2..583d9fc 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2,7 +2,6 @@ use geo::coord; use h3_lorawan_regions as regions; use h3ron::H3Cell; use hextree::{compaction::EqCompactor, Cell, HexTreeMap, HexTreeSet}; -use std::convert::TryFrom; /// Perform a linear search of `region` for `target` cell. fn naive_contains(region: &[Cell], target: Cell) -> bool { @@ -26,7 +25,7 @@ fn naive_contains(region: &[Cell], target: Cell) -> bool { fn from_indicies(indicies: &[u64]) -> (HexTreeSet, Vec) { let cells: Vec = indicies .iter() - .map(|&idx| Cell::try_from(idx).unwrap()) + .map(|&idx| Cell::from_raw(idx).unwrap()) .collect(); let set: HexTreeSet = cells.iter().collect(); (set, cells) @@ -40,9 +39,9 @@ fn all_up() { let tarpon_springs = H3Cell::from_coordinate(coord! {x: -82.753822, y: 28.15215}, 12).unwrap(); let gulf_of_mexico = H3Cell::from_coordinate(coord! {x: -83.101920, y: 28.128096}, 0).unwrap(); let paris = H3Cell::from_coordinate(coord! {x: 2.340340, y: 48.868680}, 12).unwrap(); - let tarpon_springs = Cell::try_from(*tarpon_springs).unwrap(); - let gulf_of_mexico = Cell::try_from(*gulf_of_mexico).unwrap(); - let paris = Cell::try_from(*paris).unwrap(); + let tarpon_springs = Cell::from_raw(*tarpon_springs).unwrap(); + let gulf_of_mexico = Cell::from_raw(*gulf_of_mexico).unwrap(); + let paris = Cell::from_raw(*paris).unwrap(); assert!(us915_tree.contains(tarpon_springs)); assert!(naive_contains(&us915_cells, tarpon_springs)); @@ -73,7 +72,7 @@ fn all_up() { } // https://wolf-h3-viewer.glitch.me/?h3=812a3ffffffffff - let northeast_res1 = Cell::try_from(0x812a3ffffffffff).unwrap(); + let northeast_res1 = Cell::from_raw(0x812a3ffffffffff).unwrap(); // Lets get rid of all raw cells not under northeast_res1. let expected_north_cells = { @@ -116,12 +115,12 @@ fn mono_map() { for (name, cells) in regions.iter() { for cell in cells.iter() { - monomap.insert(Cell::try_from(*cell).unwrap(), name); + monomap.insert(Cell::from_raw(*cell).unwrap(), name); } } for (name, cells) in regions.iter() { - assert!(cells.iter().map(|c| Cell::try_from(*c).unwrap()).all(|c| { + assert!(cells.iter().map(|c| Cell::from_raw(*c).unwrap()).all(|c| { if let Some((cell, val)) = monomap.get(c) { c.to_parent(cell.res()) == Some(cell) && val == &name } else { @@ -137,7 +136,7 @@ fn test_compaction() { let (mut us915_nocompact_tree, us915_nocompact_cells) = from_indicies(regions::nocompact::US915); let gulf_of_mexico = H3Cell::from_coordinate(coord! {x: -83.101920, y: 28.128096}, 0).unwrap(); - let gulf_of_mexico = Cell::try_from(*gulf_of_mexico).unwrap(); + let gulf_of_mexico = Cell::from_raw(*gulf_of_mexico).unwrap(); assert_eq!(us915_tree.len(), us915_nocompact_tree.len()); assert!(us915_tree == us915_nocompact_tree); assert!(us915_nocompact_tree.len() < us915_nocompact_cells.len());