p2p: refactor: do not call as_ref() before calling into()
What changed, and why it matters
This is a small internal code cleanup in the Bitcoin peer-to-peer networking code. It removes an unnecessary memory copy when converting text into a command string used in network messages. There is no security bug being fixed here.
No security action required. Treat as a normal code-quality/refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors CommandString::try_from_stringly to take ownership of its input S: AsRef<str> + Into<String> rather than a reference &S. Previously, callers passed &s, which caused .into() to allocate a new String even when the input was already an owned String or Box<str>. By passing s directly, the Into<String> bound can consume the owned value without reallocation. The change is purely an optimization/refactoring; no validation logic, error handling, or public behavior changes.
Changed components
p2p/src/message.rsCommandString::try_from_stringlyTryFrom<String> for CommandStringTryFrom<Box<str>> for CommandStringTryFrom<&str> for CommandStringFromStr for CommandStringInspect captured patch +7 / −9
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index 8dc34c88..1882c80e 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -66,14 +66,12 @@ impl CommandString {
///
/// - If `s` is more than 12 characters in length.
/// - If `s` has non-ascii characters.
- fn try_from_stringly<S: AsRef<str> + Into<String>>(s: &S) -> Result<Self, CommandStringError> {
- let s = s.as_ref();
-
- if !s.is_ascii() || s.len() > Self::MAX_LEN {
+ fn try_from_stringly<S: AsRef<str> + Into<String>>(s: S) -> Result<Self, CommandStringError> {
+ if !s.as_ref().is_ascii() || s.as_ref().len() > Self::MAX_LEN {
Err(CommandStringError(s.into()))
} else {
let mut buf = [0; Self::MAX_LEN];
- buf[..s.len()].copy_from_slice(s.as_bytes());
+ buf[..s.as_ref().len()].copy_from_slice(s.as_ref().as_bytes());
Ok(Self(buf))
}
}
@@ -82,25 +80,25 @@ impl CommandString {
impl TryFrom<String> for CommandString {
type Error = CommandStringError;
- fn try_from(s: String) -> Result<Self, Self::Error> { Self::try_from_stringly(&s) }
+ fn try_from(s: String) -> Result<Self, Self::Error> { Self::try_from_stringly(s) }
}
impl TryFrom<Box<str>> for CommandString {
type Error = CommandStringError;
- fn try_from(s: Box<str>) -> Result<Self, Self::Error> { Self::try_from_stringly(&s) }
+ fn try_from(s: Box<str>) -> Result<Self, Self::Error> { Self::try_from_stringly(s) }
}
impl<'a> TryFrom<&'a str> for CommandString {
type Error = CommandStringError;
- fn try_from(s: &'a str) -> Result<Self, Self::Error> { Self::try_from_stringly(&s) }
+ fn try_from(s: &'a str) -> Result<Self, Self::Error> { Self::try_from_stringly(s) }
}
impl core::str::FromStr for CommandString {
type Err = CommandStringError;
- fn from_str(s: &str) -> Result<Self, Self::Err> { Self::try_from_stringly(&s) }
+ fn from_str(s: &str) -> Result<Self, Self::Err> { Self::try_from_stringly(s) }
}
impl AsRef<str> for CommandString {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.