From 42ff747141b13e712e77c6328730c959580a7148 Mon Sep 17 00:00:00 2001 From: Markus Ineichen Date: Sun, 14 Sep 2025 04:03:01 +0200 Subject: [PATCH] Mark various constructors const --- color/lib.rs | 10 +++++----- src/parser.rs | 22 +++++++++++----------- src/rules_and_declarations.rs | 4 ++-- src/serializer.rs | 2 +- src/tokenizer.rs | 4 ++-- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/color/lib.rs b/color/lib.rs index 5b116c23..88a64956 100644 --- a/color/lib.rs +++ b/color/lib.rs @@ -572,7 +572,7 @@ pub struct Hsl { impl Hsl { /// Construct a new HSL color from it's components. - pub fn new( + pub const fn new( hue: Option, saturation: Option, lightness: Option, @@ -619,7 +619,7 @@ pub struct Hwb { impl Hwb { /// Construct a new HWB color from it's components. - pub fn new( + pub const fn new( hue: Option, whiteness: Option, blackness: Option, @@ -685,7 +685,7 @@ macro_rules! impl_lab_like { ($cls:ident, $fname:literal) => { impl $cls { /// Construct a new Lab color format with lightness, a, b and alpha components. - pub fn new( + pub const fn new( lightness: Option, a: Option, b: Option, @@ -757,7 +757,7 @@ macro_rules! impl_lch_like { ($cls:ident, $fname:literal) => { impl $cls { /// Construct a new color with lightness, chroma and hue components. - pub fn new( + pub const fn new( lightness: Option, chroma: Option, hue: Option, @@ -814,7 +814,7 @@ pub struct ColorFunction { impl ColorFunction { /// Construct a new color function definition with the given color space and /// color components. - pub fn new( + pub const fn new( color_space: PredefinedColorSpace, c1: Option, c2: Option, diff --git a/src/parser.rs b/src/parser.rs index 82ec4b00..a892695d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -116,13 +116,13 @@ impl<'i, T> From> for ParseError<'i, T> { impl SourceLocation { /// Create a new BasicParseError at this location for an unexpected token #[inline] - pub fn new_basic_unexpected_token_error(self, token: Token<'_>) -> BasicParseError<'_> { + pub const fn new_basic_unexpected_token_error(self, token: Token<'_>) -> BasicParseError<'_> { self.new_basic_error(BasicParseErrorKind::UnexpectedToken(token)) } /// Create a new BasicParseError at this location #[inline] - pub fn new_basic_error(self, kind: BasicParseErrorKind<'_>) -> BasicParseError<'_> { + pub const fn new_basic_error(self, kind: BasicParseErrorKind<'_>) -> BasicParseError<'_> { BasicParseError { kind, location: self, @@ -131,13 +131,13 @@ impl SourceLocation { /// Create a new ParseError at this location for an unexpected token #[inline] - pub fn new_unexpected_token_error(self, token: Token<'_>) -> ParseError<'_, E> { + pub const fn new_unexpected_token_error(self, token: Token<'_>) -> ParseError<'_, E> { self.new_error(BasicParseErrorKind::UnexpectedToken(token)) } /// Create a new basic ParseError at the current location #[inline] - pub fn new_error(self, kind: BasicParseErrorKind<'_>) -> ParseError<'_, E> { + pub const fn new_error(self, kind: BasicParseErrorKind<'_>) -> ParseError<'_, E> { ParseError { kind: ParseErrorKind::Basic(kind), location: self, @@ -240,7 +240,7 @@ struct CachedToken<'i> { impl<'i> ParserInput<'i> { /// Create a new input for a parser. - pub fn new(input: &'i str) -> ParserInput<'i> { + pub const fn new(input: &'i str) -> ParserInput<'i> { ParserInput { tokenizer: Tokenizer::new(input), cached_token: None, @@ -383,7 +383,7 @@ macro_rules! expect { impl<'i: 't, 't> Parser<'i, 't> { /// Create a new parser #[inline] - pub fn new(input: &'t mut ParserInput<'i>) -> Parser<'i, 't> { + pub const fn new(input: &'t mut ParserInput<'i>) -> Parser<'i, 't> { Parser { input, at_start_of: None, @@ -435,7 +435,7 @@ impl<'i: 't, 't> Parser<'i, 't> { /// The current line number and column number. #[inline] - pub fn current_source_location(&self) -> SourceLocation { + pub const fn current_source_location(&self) -> SourceLocation { self.input.tokenizer.current_source_location() } @@ -459,13 +459,13 @@ impl<'i: 't, 't> Parser<'i, 't> { /// Create a new BasicParseError at the current location #[inline] - pub fn new_basic_error(&self, kind: BasicParseErrorKind<'i>) -> BasicParseError<'i> { + pub const fn new_basic_error(&self, kind: BasicParseErrorKind<'i>) -> BasicParseError<'i> { self.current_source_location().new_basic_error(kind) } /// Create a new basic ParseError at the current location #[inline] - pub fn new_error(&self, kind: BasicParseErrorKind<'i>) -> ParseError<'i, E> { + pub const fn new_error(&self, kind: BasicParseErrorKind<'i>) -> ParseError<'i, E> { self.current_source_location().new_error(kind) } @@ -477,13 +477,13 @@ impl<'i: 't, 't> Parser<'i, 't> { /// Create a new unexpected token BasicParseError at the current location #[inline] - pub fn new_basic_unexpected_token_error(&self, token: Token<'i>) -> BasicParseError<'i> { + pub const fn new_basic_unexpected_token_error(&self, token: Token<'i>) -> BasicParseError<'i> { self.new_basic_error(BasicParseErrorKind::UnexpectedToken(token)) } /// Create a new unexpected token ParseError at the current location #[inline] - pub fn new_unexpected_token_error(&self, token: Token<'i>) -> ParseError<'i, E> { + pub const fn new_unexpected_token_error(&self, token: Token<'i>) -> ParseError<'i, E> { self.new_error(BasicParseErrorKind::UnexpectedToken(token)) } diff --git a/src/rules_and_declarations.rs b/src/rules_and_declarations.rs index bdaef077..8778e4f4 100644 --- a/src/rules_and_declarations.rs +++ b/src/rules_and_declarations.rs @@ -232,7 +232,7 @@ impl<'i, 't, 'a, P, I, E> RuleBodyParser<'i, 't, 'a, P, I, E> { /// The return type for finished declarations and at-rules also needs to be the same, /// since `::next` can return either. /// It could be a custom enum. - pub fn new(input: &'a mut Parser<'i, 't>, parser: &'a mut P) -> Self { + pub const fn new(input: &'a mut Parser<'i, 't>, parser: &'a mut P) -> Self { Self { input, parser, @@ -340,7 +340,7 @@ where /// /// The return type for finished qualified rules and at-rules also needs to be the same, /// since `::next` can return either. It could be a custom enum. - pub fn new(input: &'a mut Parser<'i, 't>, parser: &'a mut P) -> Self { + pub const fn new(input: &'a mut Parser<'i, 't>, parser: &'a mut P) -> Self { Self { input, parser, diff --git a/src/serializer.rs b/src/serializer.rs index ed325fc9..d92c4837 100644 --- a/src/serializer.rs +++ b/src/serializer.rs @@ -306,7 +306,7 @@ where W: fmt::Write, { /// Wrap a text writer to create a `CssStringWriter`. - pub fn new(inner: &'a mut W) -> CssStringWriter<'a, W> { + pub const fn new(inner: &'a mut W) -> CssStringWriter<'a, W> { CssStringWriter { inner } } } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 1e2bb737..918b079e 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -229,7 +229,7 @@ enum SeenStatus { impl<'a> Tokenizer<'a> { #[inline] - pub fn new(input: &'a str) -> Self { + pub const fn new(input: &'a str) -> Self { Tokenizer { input, position: 0, @@ -274,7 +274,7 @@ impl<'a> Tokenizer<'a> { } #[inline] - pub fn current_source_location(&self) -> SourceLocation { + pub const fn current_source_location(&self) -> SourceLocation { SourceLocation { line: self.current_line_number, column: (self.position - self.current_line_start_position + 1) as u32,