Skip to content

Commit 77eadeb

Browse files
committed
Use an ArrayVec for a function that can only return a 0-2 lengthed array
1 parent c0ee51f commit 77eadeb

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::collections::VecDeque;
1010
use std::fmt::{self, Display, Write};
1111
use std::iter;
1212

13+
use arrayvec::ArrayVec;
1314
use rustc_data_structures::fx::FxIndexMap;
1415
use rustc_lexer::{Cursor, FrontmatterAllowed, LiteralKind, TokenKind};
1516
use rustc_span::edition::Edition;
@@ -818,7 +819,7 @@ impl<'src> Classifier<'src> {
818819
}
819820

820821
/// Concatenate colons and idents as one when possible.
821-
fn get_full_ident_path(&mut self) -> Vec<(TokenKind, usize, usize)> {
822+
fn get_full_ident_path(&mut self) -> ArrayVec<(TokenKind, usize, usize), 2> {
822823
let start = self.byte_pos as usize;
823824
let mut pos = start;
824825
let mut has_ident = false;
@@ -832,13 +833,16 @@ impl<'src> Classifier<'src> {
832833
// Ident path can start with "::" but if we already have content in the ident path,
833834
// the "::" is mandatory.
834835
if has_ident && nb == 0 {
835-
return vec![(TokenKind::Ident, start, pos)];
836+
return ArrayVec::from_iter([(TokenKind::Ident, start, pos)]);
836837
} else if nb != 0 && nb != 2 {
837-
if has_ident {
838-
return vec![(TokenKind::Ident, start, pos), (TokenKind::Colon, pos, pos + nb)];
838+
return if has_ident {
839+
ArrayVec::from_iter([
840+
(TokenKind::Ident, start, pos),
841+
(TokenKind::Colon, pos, pos + nb),
842+
])
839843
} else {
840-
return vec![(TokenKind::Colon, start, pos + nb)];
841-
}
844+
ArrayVec::from_iter([(TokenKind::Colon, start, pos + nb)])
845+
};
842846
}
843847

844848
if let Some((None, text)) = self.tokens.peek().map(|(token, text)| {
@@ -854,15 +858,21 @@ impl<'src> Classifier<'src> {
854858
pos += text.len() + nb;
855859
has_ident = true;
856860
self.tokens.next();
857-
} else if nb > 0 && has_ident {
858-
return vec![(TokenKind::Ident, start, pos), (TokenKind::Colon, pos, pos + nb)];
861+
continue;
862+
}
863+
864+
return if nb > 0 && has_ident {
865+
ArrayVec::from_iter([
866+
(TokenKind::Ident, start, pos),
867+
(TokenKind::Colon, pos, pos + nb),
868+
])
859869
} else if nb > 0 {
860-
return vec![(TokenKind::Colon, start, start + nb)];
870+
ArrayVec::from_iter([(TokenKind::Colon, start, start + nb)])
861871
} else if has_ident {
862-
return vec![(TokenKind::Ident, start, pos)];
872+
ArrayVec::from_iter([(TokenKind::Ident, start, pos)])
863873
} else {
864-
return Vec::new();
865-
}
874+
ArrayVec::new()
875+
};
866876
}
867877
}
868878

@@ -903,8 +913,7 @@ impl<'src> Classifier<'src> {
903913
if self
904914
.tokens
905915
.peek()
906-
.map(|t| matches!(t.0, TokenKind::Colon | TokenKind::Ident))
907-
.unwrap_or(false)
916+
.is_some_and(|(kind, _)| matches!(kind, TokenKind::Colon | TokenKind::Ident))
908917
{
909918
let tokens = self.get_full_ident_path();
910919
for (token, start, end) in &tokens {

0 commit comments

Comments
 (0)