From 9e2e66b01cc75ee63e22d57bde88859877871850 Mon Sep 17 00:00:00 2001 From: QuackitsQuinn Date: Fri, 17 May 2024 08:35:53 -0500 Subject: [PATCH] Small tweaks - Some code clean up - Prepare for publishing --- Cargo.lock | 2 +- Cargo.toml | 14 +++++++++++--- examples/hello_world.rs | 2 +- lazuli_core/src/client/client.rs | 24 ++++++++++++++++++++++-- lazuli_core/src/client/connector.rs | 18 +++++++++++++++++- lazuli_core/src/client/server.rs | 1 + 6 files changed, 53 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b20b5d..b430b3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,7 +19,7 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "lazuli" -version = "0.1.1" +version = "0.1.0" dependencies = [ "lazuli_core", "lazuli_derive", diff --git a/Cargo.toml b/Cargo.toml index d91eae1..5e099a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,17 +1,25 @@ [package] name = "lazuli" -version = "0.1.1" +version = "0.1.0" edition = "2021" +publish = true + [workspace] members = ["lazuli_core", "lazuli_derive"] [workspace.package] version = "0.1.0" +authors = ["quackitsquinn"] +license = "GPL-3.0" +readme = "README.md" +repository = "https://github.com/quackitsquinn/lazuli" +description = "A socket library for consistent, quick, and easy data transfer" +keywords = ["socket", "networking", "data transfer", "tcp", "lazuli"] [dependencies] -lazuli_core = { path = "lazuli_core" } -lazuli_derive = { path = "lazuli_derive" } +lazuli_core = { path = "lazuli_core", version = "0.1.0"} +lazuli_derive = { path = "lazuli_derive", version = "0.1.0"} diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 973af3d..d64cf1c 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -7,7 +7,7 @@ const ADDRESS: SocketAddr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST pub fn main() { // Initialize the server and client. The server will accept a connection from the client. let mut server = Server::new(ADDRESS).unwrap(); - let mut client = Client::new(ADDRESS).unwrap(); + let mut client = Client::connect(ADDRESS).unwrap(); // Accept the connection from the client. This returns a Client object that can be used to communicate with the client. // The client object is wrapped in an Arc> to allow for thread-safe access. diff --git a/lazuli_core/src/client/client.rs b/lazuli_core/src/client/client.rs index 369e93e..ec1a9d7 100644 --- a/lazuli_core/src/client/client.rs +++ b/lazuli_core/src/client/client.rs @@ -102,7 +102,7 @@ impl Client { T: Sendable + 'static, { // SAFETY: This is safe because the stream is connected to a StreamConnector, which is guaranteed to be valid. - let stream: Stream = unsafe { Stream::new() }; + let stream: Stream = Stream::new(); let info = StreamConnector::new(&stream); self.streams .lock() @@ -141,6 +141,26 @@ impl Client { } } +impl Debug for Client { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Client") + .field("peer_addr", &self.peer_addr()) + .field("is_connected", &self.is_connected()) + .field( + "streams", + // This line pasta converts the array of StreamConnector into an array of &str + &self + .streams + .lock() + .unwrap() + .values() + .map(|x| x.type_name()) + .collect::>(), + ) + .finish() + } +} + #[cfg(test)] mod tests { use std::vec; @@ -249,7 +269,7 @@ mod tests { #[test] fn test_stream_data_struct() { - let mut stream: Stream = unsafe { Stream::new() }; + let mut stream: Stream = Stream::new(); let mut data = StreamConnector::new(&stream); unsafe { data.push_raw(TestStruct { a: 30, b: 40 }.send().into()) diff --git a/lazuli_core/src/client/connector.rs b/lazuli_core/src/client/connector.rs index be21961..1c4765b 100644 --- a/lazuli_core/src/client/connector.rs +++ b/lazuli_core/src/client/connector.rs @@ -1,13 +1,14 @@ //! Contains the StreamConnector struct, which allows for the pushing of data into a Stream. use std::{ + fmt::Debug, io::Read, mem::{self, ManuallyDrop}, }; use log::trace; -use crate::{stream::Stream, ArcMutex, Result, PacketHeader, Sendable, UnknownType}; +use crate::{stream::Stream, ArcMutex, PacketHeader, Result, Sendable, UnknownType}; /// A single byte type that is used to store the raw data. #[repr(transparent)] @@ -22,6 +23,7 @@ pub struct StreamConnector { size: usize, grew: ArcMutex, conversion_fn: fn(&mut dyn Read) -> Result>, + type_name: &'static str, } impl StreamConnector { @@ -33,6 +35,7 @@ impl StreamConnector { size: mem::size_of::(), grew: stream.get_grow_by(), conversion_fn: T::as_conversion_fn(), + type_name: std::any::type_name::(), } } /// Pushes data to the stream. @@ -88,6 +91,19 @@ impl StreamConnector { unsafe { self.push_raw(converted)? }; Ok(()) } + /// Returns the type name of the stream. This is mainly used for the debug implementation. + pub fn type_name(&self) -> &'static str { + self.type_name + } +} + +impl Debug for StreamConnector { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("StreamConnector") + .field("type_name", &self.type_name) + .field("size", &self.size) + .finish() + } } /// Everything in StreamConnector is behind a mutex, besides the size. The size should never change. diff --git a/lazuli_core/src/client/server.rs b/lazuli_core/src/client/server.rs index 0fbb590..aa00fb7 100644 --- a/lazuli_core/src/client/server.rs +++ b/lazuli_core/src/client/server.rs @@ -104,6 +104,7 @@ mod test { let mut client2 = make_server_client_pair(&mut server); let mut str_stream_1 = client1.0.stream::(); let mut str_stream_2 = client2.0.stream::(); + println!("{:#?}", client1.0); server.broadcast(&"Hello, world!".to_owned())?; client1.0.recv()?; client2.0.recv()?;