Skip to content

Commit 9a56d36

Browse files
committed
refactor: added more borrowing
1 parent 46df4d8 commit 9a56d36

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

src/cells.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(crate) fn tile_to_cell(tile: Tile) -> Cell {
4949
}
5050

5151
/// Convert Quadbin cell into a tile
52-
pub(crate) fn cell_to_tile(cell: Cell) -> Tile {
52+
pub(crate) fn cell_to_tile(cell: &Cell) -> Tile {
5353
assert!(cell.is_valid(), "Quadbin cell index is not valid");
5454

5555
let cell64 = cell.get();
@@ -93,7 +93,7 @@ pub(crate) fn point_to_cell(lat: f64, lng: f64, res: u8) -> Cell {
9393
}
9494

9595
/// Convert cell into point
96-
pub(crate) fn cell_to_point(cell: Cell) -> (f64, f64) {
96+
pub(crate) fn cell_to_point(cell: &Cell) -> (f64, f64) {
9797
assert!(cell.is_valid(), "Quadbin cell index is not valid");
9898

9999
let tile = cell.to_tile();
@@ -104,7 +104,7 @@ pub(crate) fn cell_to_point(cell: Cell) -> (f64, f64) {
104104
}
105105

106106
/// Compute the parent cell for a specific resolution.
107-
pub(crate) fn cell_to_parent(cell: Cell, parent_res: u8) -> Cell {
107+
pub(crate) fn cell_to_parent(cell: &Cell, parent_res: u8) -> Cell {
108108
// Check resolution
109109
let resolution = cell.resolution();
110110
assert!(

src/test/hashing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn test_tile_hashing() {
1616

1717
for (tile, hash) in cases.iter() {
1818
// Tile to hash
19-
assert_eq!(Tile::to_hash(*tile), *hash);
19+
assert_eq!(Tile::to_hash(&tile), *hash);
2020
// Hash to tile
2121
assert_eq!(Tile::from_hash(*hash), *tile);
2222
}

src/test/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ fn test_tile_conversion() {
8585
#[test]
8686
fn test_tile_scalefactor() {
8787
assert_relative_eq!(
88-
tile_scalefactor(Tile::new(384, 368, 10)),
88+
tile_scalefactor(&Tile::new(384, 368, 10)),
8989
0.7075410884638627_f64,
9090
epsilon = ACC
9191
);
9292
assert_relative_eq!(
93-
tile_scalefactor(Tile::new(384, 368, 26)),
93+
tile_scalefactor(&Tile::new(384, 368, 26)),
9494
0.08626970361752928_f64,
9595
epsilon = ACC
9696
);
9797
assert_relative_eq!(
98-
tile_scalefactor(Tile::new(100, 100, 10)),
98+
tile_scalefactor(&Tile::new(100, 100, 10)),
9999
0.15910754230624527_f64,
100100
epsilon = ACC
101101
);

src/types.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ impl Tile {
3737
///
3838
/// See also [Tile::to_longitude].
3939
///
40-
pub fn to_latitude(self, offset: f64) -> f64 {
40+
pub fn to_latitude(&self, offset: f64) -> f64 {
4141
tile_to_latitude(self, offset)
4242
}
4343

4444
/// Return tile's longitude.
4545
///
4646
/// See also [Tile::to_latitude].
4747
///
48-
pub fn to_longitude(self, offset: f64) -> f64 {
48+
pub fn to_longitude(&self, offset: f64) -> f64 {
4949
tile_to_longitude(self, offset)
5050
}
5151

5252
/// Get tile's siblings.
5353
// TODO:
5454
// Add examples. See how to properly document direction
55-
pub fn get_sibling(self, direction: u8) -> Option<Self> {
55+
pub fn get_sibling(&self, direction: u8) -> Option<Self> {
5656
tile_sibling(self, direction)
5757
}
5858

5959
/// Compute a hash from the tile.
60-
pub fn to_hash(self) -> u64 {
60+
pub fn to_hash(&self) -> u64 {
6161
to_tile_hash(self)
6262
}
6363

@@ -142,7 +142,7 @@ impl Cell {
142142
/// let parent = qb_cell.parent(2_u8);
143143
/// assert_eq!(parent, quadbin::Cell::new(5200813144682790911))
144144
/// ```
145-
pub fn parent(self, parent_res: u8) -> Cell {
145+
pub fn parent(&self, parent_res: u8) -> Cell {
146146
cell_to_parent(self, parent_res)
147147
}
148148

@@ -182,7 +182,7 @@ impl Cell {
182182
///
183183
/// Returns a tuple with latitude and longitude in degrees.
184184
///
185-
pub fn to_point(self) -> (f64, f64) {
185+
pub fn to_point(&self) -> (f64, f64) {
186186
cell_to_point(self)
187187
}
188188

@@ -199,7 +199,7 @@ impl Cell {
199199
}
200200

201201
/// Convert a Quadbin cell into a tile.
202-
pub(crate) fn to_tile(self) -> Tile {
202+
pub(crate) fn to_tile(&self) -> Tile {
203203
cell_to_tile(self)
204204
}
205205
}

src/utils.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(crate) fn point_to_tile(lat: f64, lng: f64, res: u8) -> Tile {
4949
}
5050

5151
/// Compute the latitude for a tile with an offset.
52-
pub(crate) fn tile_to_latitude(tile: Tile, offset: f64) -> f64 {
52+
pub(crate) fn tile_to_latitude(tile: &Tile, offset: f64) -> f64 {
5353
// Check if offset is between 0 and 1
5454
assert!(
5555
(0.0..=1.0).contains(&offset),
@@ -66,7 +66,7 @@ pub(crate) fn tile_to_latitude(tile: Tile, offset: f64) -> f64 {
6666
}
6767

6868
/// Compute the longitude for a tile with an offset.
69-
pub(crate) fn tile_to_longitude(tile: Tile, offset: f64) -> f64 {
69+
pub(crate) fn tile_to_longitude(tile: &Tile, offset: f64) -> f64 {
7070
// Check if offset is between 0 and 1
7171
assert!(
7272
(0.0..=1.0).contains(&offset),
@@ -82,7 +82,7 @@ pub(crate) fn tile_to_longitude(tile: Tile, offset: f64) -> f64 {
8282
}
8383

8484
/// Inverse of the scale factor at the tile center.
85-
pub(crate) fn tile_scalefactor(tile: Tile) -> f64 {
85+
pub(crate) fn tile_scalefactor(tile: &Tile) -> f64 {
8686
// Get Tile coords
8787
let y = tile.y as f64;
8888
let z2 = (1 << tile.z) as f64;
@@ -116,7 +116,7 @@ pub(crate) fn tile_area(tile: &Tile) -> f64 {
116116
let z_factor = |y_val: f64| -> f64 {
117117
// Create a new tile with the same x and z but different y
118118
let temp_tile = Tile::new(*x, y_val as u32, z as u8);
119-
tile_scalefactor(temp_tile).powf(2.0)
119+
tile_scalefactor(&temp_tile).powf(2.0)
120120
};
121121

122122
area *= z_factor(y) / z_factor(center_y);
@@ -126,7 +126,7 @@ pub(crate) fn tile_area(tile: &Tile) -> f64 {
126126
}
127127

128128
/// Compute the sibling (neighbour) tile in a specific direction.
129-
pub(crate) fn tile_sibling(tile: Tile, direction: u8) -> Option<Tile> {
129+
pub(crate) fn tile_sibling(tile: &Tile, direction: u8) -> Option<Tile> {
130130
// Early return for a low level == no neighbors
131131
// TODO: Think about what should one return instead of None
132132
if tile.z == 0_u8 {
@@ -180,7 +180,7 @@ pub(crate) fn tile_sibling(tile: Tile, direction: u8) -> Option<Tile> {
180180
}
181181

182182
/// Compute a hash from the tile.
183-
pub(crate) fn to_tile_hash(tile: Tile) -> u64 {
183+
pub(crate) fn to_tile_hash(tile: &Tile) -> u64 {
184184
let x = tile.x as u64;
185185
let y = tile.y as u64;
186186
let z = tile.z as u64;

0 commit comments

Comments
 (0)