Skip to content

Commit

Permalink
Small tweaks
Browse files Browse the repository at this point in the history
- Some code clean up
- Prepare for publishing
  • Loading branch information
quackitsquinn committed May 17, 2024
1 parent ea1effc commit 9e2e66b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"}

2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<Client>> to allow for thread-safe access.
Expand Down
24 changes: 22 additions & 2 deletions lazuli_core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = unsafe { Stream::new() };
let stream: Stream<T> = Stream::new();
let info = StreamConnector::new(&stream);
self.streams
.lock()
Expand Down Expand Up @@ -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::<Vec<&str>>(),
)
.finish()
}
}

#[cfg(test)]
mod tests {
use std::vec;
Expand Down Expand Up @@ -249,7 +269,7 @@ mod tests {

#[test]
fn test_stream_data_struct() {
let mut stream: Stream<TestStruct> = unsafe { Stream::new() };
let mut stream: Stream<TestStruct> = Stream::new();
let mut data = StreamConnector::new(&stream);
unsafe {
data.push_raw(TestStruct { a: 30, b: 40 }.send().into())
Expand Down
18 changes: 17 additions & 1 deletion lazuli_core/src/client/connector.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -22,6 +23,7 @@ pub struct StreamConnector {
size: usize,
grew: ArcMutex<usize>,
conversion_fn: fn(&mut dyn Read) -> Result<Box<[u8]>>,
type_name: &'static str,
}

impl StreamConnector {
Expand All @@ -33,6 +35,7 @@ impl StreamConnector {
size: mem::size_of::<T>(),
grew: stream.get_grow_by(),
conversion_fn: T::as_conversion_fn(),
type_name: std::any::type_name::<T>(),
}
}
/// Pushes data to the stream.
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lazuli_core/src/client/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ mod test {
let mut client2 = make_server_client_pair(&mut server);
let mut str_stream_1 = client1.0.stream::<String>();
let mut str_stream_2 = client2.0.stream::<String>();
println!("{:#?}", client1.0);
server.broadcast(&"Hello, world!".to_owned())?;
client1.0.recv()?;
client2.0.recv()?;
Expand Down

0 comments on commit 9e2e66b

Please sign in to comment.