Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap the inner array for LocalCostMatrix with Box #532

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/local/cost_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::{linear_index_to_xy, xy_to_linear_index, Position, RoomXY};
#[serde(transparent)]
pub struct LocalCostMatrix {
#[serde(with = "serde_impls")]
bits: [u8; ROOM_AREA],
bits: Box<[u8; ROOM_AREA]>,
}

impl Default for LocalCostMatrix {
Expand All @@ -40,7 +40,7 @@ impl LocalCostMatrix {
/// assert_eq!(lcm.get(pos), 0);
/// ```
#[inline]
pub const fn new() -> Self {
pub fn new() -> Self {
LocalCostMatrix::new_with_value(0)
}

Expand All @@ -56,9 +56,9 @@ impl LocalCostMatrix {
/// assert_eq!(lcm.get(pos), u8::MAX);
/// ```
#[inline]
pub const fn new_with_value(value: u8) -> Self {
pub fn new_with_value(value: u8) -> Self {
LocalCostMatrix {
bits: [value; ROOM_AREA],
bits: Box::new([value; ROOM_AREA]),
}
}

Expand Down Expand Up @@ -102,13 +102,13 @@ impl From<LocalCostMatrix> for Vec<u8> {
/// `idx = ((x * ROOM_SIZE) + y)`.
#[inline]
fn from(lcm: LocalCostMatrix) -> Vec<u8> {
lcm.bits.into()
(*lcm.bits).into()
}
}

impl From<&LocalCostMatrix> for Vec<u8> {
fn from(lcm: &LocalCostMatrix) -> Vec<u8> {
lcm.bits.into()
(*lcm.bits).into()
}
}

Expand All @@ -117,7 +117,9 @@ impl From<&CostMatrix> for LocalCostMatrix {
let mut bits = [0; ROOM_AREA];
js_matrix.get_bits().copy_to(&mut bits);

LocalCostMatrix { bits }
LocalCostMatrix {
bits: Box::new(bits),
}
}
}

Expand Down Expand Up @@ -177,7 +179,7 @@ mod serde_impls {
bits[..].serialize(serializer)
}

pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<[u8; ROOM_AREA], D::Error>
pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<Box<[u8; ROOM_AREA]>, D::Error>
where
D: Deserializer<'de>,
{
Expand All @@ -191,6 +193,6 @@ mod serde_impls {
}

// SAFETY: If the length wasn't right, we would have hit the check above
Ok(bits_slice.try_into().unwrap())
Ok(Box::new(bits_slice.try_into().unwrap()))
}
}
Loading