1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-07-28 12:17:45 +00:00

feat: remove unnecessary allocations in VersionComponent

This commit is contained in:
RGBCube 2025-05-08 22:40:25 +03:00 committed by bloxx12
parent c73c1ab330
commit 066652cee3

View file

@ -19,13 +19,19 @@ use std::string::ToString;
#[derive(Eq, PartialEq, Debug)] #[derive(Eq, PartialEq, Debug)]
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
enum VersionComponent { enum VersionComponent<'a> {
Number(u64), Number(u64),
Text(String), Text(&'a str),
} }
impl std::cmp::Ord for VersionComponent { impl PartialOrd for VersionComponent<'_> {
fn cmp(&self, other: &Self) -> Ordering { fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl cmp::Ord for VersionComponent<'_> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
use VersionComponent::{ use VersionComponent::{
Number, Number,
Text, Text,
@ -34,7 +40,7 @@ impl std::cmp::Ord for VersionComponent {
match (self, other) { match (self, other) {
(Number(x), Number(y)) => x.cmp(y), (Number(x), Number(y)) => x.cmp(y),
(Text(x), Text(y)) => { (Text(x), Text(y)) => {
match (x.as_str(), y.as_str()) { match (*x, *y) {
("pre", _) => cmp::Ordering::Less, ("pre", _) => cmp::Ordering::Less,
(_, "pre") => cmp::Ordering::Greater, (_, "pre") => cmp::Ordering::Greater,
_ => x.cmp(y), _ => x.cmp(y),
@ -50,8 +56,8 @@ impl std::cmp::Ord for VersionComponent {
#[derive(Deref, DerefMut, From)] #[derive(Deref, DerefMut, From)]
struct VersionComponentIter<'a>(&'a str); struct VersionComponentIter<'a>(&'a str);
impl Iterator for VersionComponentIter<'_> { impl<'a> Iterator for VersionComponentIter<'a> {
type Item = VersionComponent; type Item = VersionComponent<'a>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
// Skip all '-' and '.'. // Skip all '-' and '.'.
@ -71,7 +77,7 @@ impl Iterator for VersionComponentIter<'_> {
.map(char::len_utf8) .map(char::len_utf8)
.sum(); .sum();
let component = self[..component_len].to_owned(); let component = &self[..component_len];
**self = &self[component_len..]; **self = &self[component_len..];
assert!(!component.is_empty()); assert!(!component.is_empty());