What do you think about my code?

#1

here is a crate I wrote for streaming over HTTP for a project that I didn’t want to deal with WebSockets.
What do you think about the code should I change anything to improve the performance or readability (besides the documentation)

#2

Alright then, looking through your code and repo:

  • No readme.md
  • No description for the crates.io page
  • Odd //}}}s and //{{{s dotted through your code in stream.rs
  • new_client() can be simplified:
pub fn new_client(&self, s: TcpStream) {
    self.clients.write().unwrap().push(s);
}
  • Just out of curiosity, why would you take &self and &mut self if you will usually be dealing with Arc<Self>? You could use an arbitrary self type like you already do in Streamer::start, like for example:
pub fn new_client(self: &Arc<Self>, s: TcpStream) {
    self.clients.write().unwrap().push(s);
}
  • I’m not too sure about this one, but on lines 81-84 I don’t think you need to have the let _ = statements, but I’ve heard the contrary from time to time.
  • If you really want to get the best out of your code, try to minimize the time you own a write lock on the RwLock, take a read lock for as long as possible, and then drop it and wait on write.
    I’m not well versed with tcp (Or really network protocols in general) so this is just a general look at your code.
#3

Rust API Guidelines C-CALLER-CONTROL

1 Like