Use Vec<u8>s for sending data between threads

This commit is contained in:
Michael Bradley 2025-05-25 17:12:05 -04:00
parent dd57bc30e1
commit ba7737671e
Signed by: MichaelBradley
SSH key fingerprint: SHA256:o/aaeYtRubILK7OYYjYP12DmU7BsPUhKji1AgaQ+ge4
5 changed files with 47 additions and 16 deletions

View file

@ -1,10 +1,13 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use std::{
array::TryFromSliceError,
hash::{DefaultHasher, Hash, Hasher},
};
use bevy::prelude::*;
use rand::random;
/// Value with which to initialize the PRNG
#[derive(Clone, Resource)]
#[derive(Resource, Clone, Copy)]
pub struct Seed(u64);
impl Seed {
@ -26,20 +29,40 @@ impl From<String> for Seed {
}
impl From<Seed> for [u8; 8] {
/// Convert to a u8 array for ingestion by random number generator
fn from(value: Seed) -> Self {
value.0.to_le_bytes()
}
}
impl From<[u8; 8]> for Seed {
fn from(value: [u8; 8]) -> Self {
u64::from_le_bytes(value).into()
}
}
impl From<Seed> for u64 {
fn from(value: Seed) -> Self {
value.0
}
}
impl From<u64> for Seed {
fn from(value: u64) -> Self {
Seed(value)
}
}
impl From<Seed> for u64 {
impl From<Seed> for Vec<u8> {
fn from(value: Seed) -> Self {
value.0
value.0.to_le_bytes().to_vec()
}
}
impl TryFrom<Vec<u8>> for Seed {
type Error = TryFromSliceError;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
let bytes: [u8; 8] = value.as_slice().try_into()?;
Ok(bytes.into())
}
}