From 04ce5366c8b450d9e764bb216daea116f4798452 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 3 Jul 2022 18:02:00 -0700 Subject: [PATCH] Add a null state to some IDs --- src/client.rs | 19 ++++++++++++------- src/entity.rs | 4 +++- src/slotmap.rs | 16 ++++++++++++++++ src/world.rs | 6 +++++- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/client.rs b/src/client.rs index 1bfd312..722f546 100644 --- a/src/client.rs +++ b/src/client.rs @@ -86,8 +86,13 @@ impl Clients { } } +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] pub struct ClientId(Key); +impl ClientId { + pub const NULL: Self = Self(Key::NULL); +} + /// Represents a client connected to the server after logging in. pub struct Client { /// Setting this to `None` disconnects the client. @@ -98,8 +103,8 @@ pub struct Client { uuid: Uuid, username: String, textures: Option, - new_world: Option, - old_world: Option, + new_world: WorldId, + old_world: WorldId, on_ground: bool, new_position: Vec3, old_position: Vec3, @@ -157,8 +162,8 @@ impl Client { uuid: ncd.uuid, username: ncd.username, textures: ncd.textures, - new_world: None, - old_world: None, + new_world: WorldId::default(), + old_world: WorldId::default(), on_ground: false, new_position: Vec3::default(), old_position: Vec3::default(), @@ -203,12 +208,12 @@ impl Client { self.textures.as_ref() } - pub fn world(&self) -> Option { + pub fn world(&self) -> WorldId { self.new_world } pub fn set_world(&mut self, world: WorldId) { - self.new_world = Some(world); + self.new_world = world; } /// Sends a system message to the player. @@ -573,7 +578,7 @@ impl Client { return; } - let world = match self.new_world.and_then(|id| worlds.get(id)) { + let world = match worlds.get(self.new_world) { Some(world) => world, None => { log::warn!( diff --git a/src/entity.rs b/src/entity.rs index 2841dce..82b89c1 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -164,10 +164,12 @@ impl Entities { } } -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] pub struct EntityId(Key); impl EntityId { + pub const NULL: Self = Self(Key::NULL); + pub(crate) fn to_network_id(self) -> i32 { self.0.version().get() as i32 } diff --git a/src/slotmap.rs b/src/slotmap.rs index e5384bb..35f861f 100644 --- a/src/slotmap.rs +++ b/src/slotmap.rs @@ -23,6 +23,22 @@ pub struct Key { version: NonZeroU32, } +impl Key { + pub const NULL: Self = Self { + index: u32::MAX, + version: match NonZeroU32::new(u32::MAX) { + Some(n) => n, + None => unreachable!(), + }, + }; +} + +impl Default for Key { + fn default() -> Self { + Self::NULL + } +} + impl Key { pub fn new(index: u32, version: NonZeroU32) -> Self { Self { index, version } diff --git a/src/world.rs b/src/world.rs index fa09b23..958684f 100644 --- a/src/world.rs +++ b/src/world.rs @@ -11,9 +11,13 @@ pub struct Worlds { server: SharedServer, } -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] pub struct WorldId(Key); +impl WorldId { + pub const NULL: Self = Self(Key::NULL); +} + impl Worlds { pub(crate) fn new(server: SharedServer) -> Self { Self {