commands: precalculate capacity and use iterators

This commit is contained in:
2025-11-21 14:44:17 +09:00
parent 7a966051d5
commit 4a1df97ad6
9 changed files with 24 additions and 24 deletions

View File

@@ -18,10 +18,10 @@ impl Command for ProtocolDisable {
}
// TODO: verify that the features are split by whitespace
let mut features = Vec::new();
for feature in parts {
features.push(feature.to_string());
}
let features = parts
.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>();
Ok((Request::ProtocolDisable(features), ""))
}

View File

@@ -18,10 +18,10 @@ impl Command for ProtocolEnable {
}
// TODO: verify that the features are split by whitespace
let mut features = Vec::new();
for feature in parts {
features.push(feature.to_string());
}
let features = parts
.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>();
Ok((Request::ProtocolEnable(features), ""))
}

View File

@@ -18,10 +18,10 @@ impl Command for TagTypesDisable {
}
// TODO: verify that the tag types are split by whitespace
let mut tag_types = Vec::new();
for tag_type in parts {
tag_types.push(tag_type.to_string());
}
let tag_types = parts
.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>();
Ok((Request::TagTypesDisable(tag_types), ""))
}

View File

@@ -18,10 +18,10 @@ impl Command for TagTypesEnable {
}
// TODO: verify that the tag types are split by whitespace
let mut tag_types = Vec::new();
for tag_type in parts {
tag_types.push(tag_type.to_string());
}
let tag_types = parts
.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>();
Ok((Request::TagTypesEnable(tag_types), ""))
}

View File

@@ -18,10 +18,10 @@ impl Command for TagTypesReset {
}
// TODO: verify that the tag types are split by whitespace
let mut tag_types = Vec::new();
for tag_type in parts {
tag_types.push(tag_type.to_string());
}
let tag_types = parts
.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>();
Ok((Request::TagTypesReset(tag_types), ""))
}

View File

@@ -20,7 +20,7 @@ impl Command for Commands {
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
let parts: Vec<_> = parts.into();
let mut result = Vec::new();
let mut result = Vec::with_capacity(parts.len());
for (key, value) in parts.into_iter() {
if key != "command" {
return Err(ResponseParserError::UnexpectedProperty(key));

View File

@@ -19,7 +19,7 @@ impl Command for NotCommands {
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
let parts: Vec<_> = parts.into();
let mut result = Vec::new();
let mut result = Vec::with_capacity(parts.len());
for (key, value) in parts.into_iter() {
if key != "command" {
return Err(ResponseParserError::UnexpectedProperty(key));

View File

@@ -19,7 +19,7 @@ impl Command for UrlHandlers {
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
let parts: Vec<_> = parts.into();
let mut url_handlers = Vec::new();
let mut url_handlers = Vec::with_capacity(parts.len());
for (key, value) in parts.into_iter() {
if key != "handler" {
return Err(ResponseParserError::UnexpectedProperty(key));

View File

@@ -71,7 +71,7 @@ impl Command for StickerFind {
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
let parts: Vec<_> = parts.into();
let mut stickers = Vec::new();
let mut stickers = Vec::with_capacity(parts.len() / 2);
for sticker_uri_pair in parts.chunks_exact(2) {
// TODO: don't assume that the order of the properties is fixed