From 27a950f268fe564918b13f00327c9f429b27e677 Mon Sep 17 00:00:00 2001 From: Daniel Prilik Date: Mon, 7 Sep 2020 14:33:33 -0400 Subject: [PATCH] minor tweaks, notes, and docs just a few things that I noticed while rummaging around in the codebase. they don't really merit individial commits... --- src/connection/mod.rs | 5 +++++ src/protocol/commands/_qSupported.rs | 2 +- src/protocol/console_output.rs | 8 ++++---- src/protocol/response_writer.rs | 15 ++++++++++----- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/connection/mod.rs b/src/connection/mod.rs index 491388b8..36d70263 100644 --- a/src/connection/mod.rs +++ b/src/connection/mod.rs @@ -1,6 +1,11 @@ mod impls; /// A trait to perform bytewise I/O over a serial transport layer. +/// +/// When the `std` feature is enabled, this trait is automatically implemented +/// for [`TcpStream`](https://doc.rust-lang.org/std/net/struct.TcpStream.html) +/// and [`UnixStream`](https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html) +/// (on unix systems). pub trait Connection { /// Transport-specific error type. type Error; diff --git a/src/protocol/commands/_qSupported.rs b/src/protocol/commands/_qSupported.rs index 6f04c359..55b885dc 100644 --- a/src/protocol/commands/_qSupported.rs +++ b/src/protocol/commands/_qSupported.rs @@ -32,7 +32,7 @@ impl<'a> Features<'a> { b'+' => FeatureSupported::Yes, b'-' => FeatureSupported::No, b'?' => FeatureSupported::Maybe, - _ => unreachable!(), + _ => return Err("invalid feature flag (must be +, -, or ?)"), }, }), Some(_) => { diff --git a/src/protocol/console_output.rs b/src/protocol/console_output.rs index f831d9ef..73157bcd 100644 --- a/src/protocol/console_output.rs +++ b/src/protocol/console_output.rs @@ -75,7 +75,7 @@ impl Drop for ConsoleOutput<'_> { macro_rules! output { ($console_output:expr, $($args:tt)*) => {{ use std::fmt::Write; - writeln!($console_output, $($args)*).unwrap(); + let _ = writeln!($console_output, $($args)*); }}; } @@ -85,14 +85,14 @@ macro_rules! output { #[macro_export] macro_rules! outputln { ($console_output:expr) => {{ - use std::fmt::Write; + use core::fmt::Write; let _ = writeln!($console_output); }}; ($console_output:expr,) => { outputln!($console_output) }; ($console_output:expr, $($args:tt)*) => {{ - use std::fmt::Write; - writeln!($console_output, $($args)*).unwrap(); + use core::fmt::Write; + let _ = writeln!($console_output, $($args)*); }}; } diff --git a/src/protocol/response_writer.rs b/src/protocol/response_writer.rs index 05d530cf..dff0cc5f 100644 --- a/src/protocol/response_writer.rs +++ b/src/protocol/response_writer.rs @@ -2,6 +2,9 @@ use crate::internal::BeBytes; use crate::protocol::{IdKind, ThreadId}; use crate::Connection; +#[cfg(feature = "alloc")] +use alloc::string::String; + /// Newtype around a Connection error. Having a newtype allows implementing a /// `From> for crate::Error`, which greatly /// simplifies some of the error handling in the main gdbstub. @@ -14,8 +17,10 @@ pub struct ResponseWriter<'a, C: Connection + 'a> { inner: &'a mut C, started: bool, checksum: u8, - // buffer outgoing message, for logging purposes - #[cfg(feature = "std")] + // buffer outgoing message + // TODO: add `write_all` method to Connection, and allow user to optionally pass outgoing + // packet buffer? This could improve performance (instead of writing a single byte at a time) + #[cfg(feature = "alloc")] msg: String, } @@ -26,7 +31,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { inner, started: false, checksum: 0, - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] msg: String::new(), } } @@ -36,7 +41,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { // don't include '#' in checksum calculation let checksum = self.checksum; - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] trace!("--> ${}#{:02x?}", self.msg, checksum); self.write(b'#')?; @@ -52,7 +57,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { /// Write a single byte. pub fn write(&mut self, byte: u8) -> Result<(), Error> { - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] self.msg.push(byte as char); if !self.started {