You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by "tungtose (via GitHub)" <gi...@apache.org> on 2025/12/01 18:23:44 UTC
[PR] feat(server): Costify semver [iggy]
tungtose opened a new pull request, #2427:
URL: https://github.com/apache/iggy/pull/2427
This PR addressing #2413
- Parse version at compile time instead of runtime
- Constify `SemanticVersion` struct
- Add tests
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "numinnex (via GitHub)" <gi...@apache.org>.
numinnex commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2580948679
##########
core/server/src/versioning.rs:
##########
@@ -26,7 +26,83 @@ pub struct SemanticVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
- pub prerelease: Option<String>,
+ pub prerelease: Option<&'static str>,
+}
+
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32_range(bytes: &[u8], start: usize, end: usize) -> u32 {
+ if start >= end {
+ panic!("Cannot parse empty range as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = start;
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ while i < end {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
Review Comment:
is_digit exists only on char not byte.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "spetz (via GitHub)" <gi...@apache.org>.
spetz merged PR #2427:
URL: https://github.com/apache/iggy/pull/2427
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "numinnex (via GitHub)" <gi...@apache.org>.
numinnex commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2578286311
##########
core/server/src/versioning.rs:
##########
@@ -67,13 +68,52 @@ impl FromStr for SemanticVersion {
}
}
-impl SemanticVersion {
- pub fn current() -> Result<Self, IggyError> {
- if let Ok(version) = VERSION.parse::<SemanticVersion>() {
- return Ok(version);
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32(s: &str) -> u32 {
+ let bytes = s.as_bytes();
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = 0;
+
+ while i < bytes.len() {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
+ panic!("Invalid digit in version number");
}
- Err(IggyError::InvalidVersion(VERSION.into()))
+ // ASCII '0' - '9' to 0-9
+ let digit = bytes[i] - b'0';
+ result = result * 10 + digit as u32;
+ i += 1;
+ }
+
+ result
+}
+
+impl SemanticVersion {
+ pub const fn current() -> Self {
Review Comment:
I actually realized that this whole feature can be implemented even simpler. Observe that there is an `VERSION` const that holds the env `CARGO_PKG_VERSION` and an `FromStr` impl does what your `parse_semver` function does, but since `impl const Trait` isn't stabilized yet, we can't use an trait and have to create a const function ourselves.
So, I think you can get rid of the `build.rs` step all together and just use the existing `VERSION` const and create an `const fn` equivalent to the `FromStr` impl for `VERSION` and use that to fill the `SEMANTIC_VERSION` const.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "hubcio (via GitHub)" <gi...@apache.org>.
hubcio commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2580839342
##########
core/server/src/versioning.rs:
##########
@@ -38,7 +114,9 @@ impl FromStr for SemanticVersion {
// Split on '-' to separate prerelease identifier
let mut parts = version_core.split('-');
let version_numbers = parts.next().unwrap();
- let prerelease = parts.next().map(String::from);
+ let prerelease = parts
+ .next()
+ .map(|s| Box::leak(s.to_string().into_boxed_str()) as &'static str);
Review Comment:
i dont like it. why not use `Cow<'static, str>`? this way you'll avoid leaking. so prerelease field should be `Option<Cow<'static, str>>`
##########
core/server/src/versioning.rs:
##########
@@ -26,7 +26,83 @@ pub struct SemanticVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
- pub prerelease: Option<String>,
+ pub prerelease: Option<&'static str>,
+}
+
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32_range(bytes: &[u8], start: usize, end: usize) -> u32 {
+ if start >= end {
+ panic!("Cannot parse empty range as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = start;
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ while i < end {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
Review Comment:
use `is_digit()`
##########
core/server/src/versioning.rs:
##########
@@ -26,7 +26,83 @@ pub struct SemanticVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
- pub prerelease: Option<String>,
+ pub prerelease: Option<&'static str>,
+}
+
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32_range(bytes: &[u8], start: usize, end: usize) -> u32 {
+ if start >= end {
+ panic!("Cannot parse empty range as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = start;
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ while i < end {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
+ panic!("Invalid digit in version number");
+ }
+
+ // ASCII '0' - '9' to 0-9
+ let digit = bytes[i] - b'0';
+ result = result * 10 + digit as u32;
+ i += 1;
+ }
+
+ result
+}
+
+/// Find pos of a byte in a range, return `end` if not found any
+const fn find_byte_in_range(bytes: &[u8], target: u8, start: usize, end: usize) -> usize {
+ let mut i = start;
+
+ while i < end {
+ if bytes[i] == target {
+ return i;
+ }
+ i += 1;
+ }
+
+ end
+}
+
+/// Find pos of a byte in a slice, return bytes.len() if not found any
+const fn find_byte_pos_or_len(bytes: &[u8], target: u8) -> usize {
+ let mut i = 0;
+
+ while i < bytes.len() {
+ if bytes[i] == target {
+ return i;
+ }
+ i += 1;
+ }
+
+ bytes.len()
+}
+
+/// Extract substring in const context
+const fn const_str_slice(s: &str, start: usize, end: usize) -> &str {
+ let bytes = s.as_bytes();
+
+ // SAFETY: Creating a slice within the bound of original byte slice.
Review Comment:
add check then, and panic if it's not met so that we'll know where exactly is the problem
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "tungtose (via GitHub)" <gi...@apache.org>.
tungtose commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2580112165
##########
core/server/src/versioning.rs:
##########
@@ -67,13 +68,52 @@ impl FromStr for SemanticVersion {
}
}
-impl SemanticVersion {
- pub fn current() -> Result<Self, IggyError> {
- if let Ok(version) = VERSION.parse::<SemanticVersion>() {
- return Ok(version);
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32(s: &str) -> u32 {
+ let bytes = s.as_bytes();
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = 0;
+
+ while i < bytes.len() {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
+ panic!("Invalid digit in version number");
}
- Err(IggyError::InvalidVersion(VERSION.into()))
+ // ASCII '0' - '9' to 0-9
+ let digit = bytes[i] - b'0';
+ result = result * 10 + digit as u32;
+ i += 1;
+ }
+
+ result
+}
+
+impl SemanticVersion {
+ pub const fn current() -> Self {
Review Comment:
@numinnex I have updated the code as your request change, please help me to review. Thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "tungtose (via GitHub)" <gi...@apache.org>.
tungtose commented on PR #2427:
URL: https://github.com/apache/iggy/pull/2427#issuecomment-3601534478
Thanks. I've resolved that comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "numinnex (via GitHub)" <gi...@apache.org>.
numinnex commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2578257259
##########
core/server/build.rs:
##########
@@ -48,3 +61,22 @@ fn main() -> Result<(), Box<dyn error::Error>> {
Ok(())
}
+
+/// Parses semantic version string into (major, minor, patch, prerelease)
+///
+/// Note: Duplicated in versioning.rs because build scripts run at compile time
+/// and cannot share code with the runtime library
+fn parse_semver(version: &str) -> (&str, &str, &str, Option<&str>) {
Review Comment:
I think we already have such function somewhere that does something similar.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "numinnex (via GitHub)" <gi...@apache.org>.
numinnex commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2580588394
##########
core/server/src/versioning.rs:
##########
@@ -26,7 +26,81 @@ pub struct SemanticVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
- pub prerelease: Option<String>,
+ pub prerelease: Option<&'static str>,
+}
+
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32_range(bytes: &[u8], start: usize, end: usize) -> u32 {
+ if start >= end {
+ panic!("Cannot parse empty range as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = start;
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ while i < end {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
+ panic!("Invalid digit in version number");
+ }
+
+ // ASCII '0' - '9' to 0-9
+ let digit = bytes[i] - b'0';
+ result = result * 10 + digit as u32;
+ i += 1;
+ }
+
+ result
+}
+
+/// Find pos of a byte in a range, return `end` if not found any
+const fn find_byte_in_range(bytes: &[u8], target: u8, start: usize, end: usize) -> usize {
+ let mut i = start;
+
+ while i < end {
+ if bytes[i] == target {
+ return i;
+ }
+ i += 1;
+ }
+
+ end
+}
+
+/// Find pos of a byte in a slice, return bytes.len() if not found any
+const fn find_byte_pos_or_len(bytes: &[u8], target: u8) -> usize {
+ let mut i = 0;
+
+ while i < bytes.len() {
+ if bytes[i] == target {
+ return i;
+ }
+ i += 1;
+ }
+
+ bytes.len()
+}
+
+/// Extract substring in const context
+const fn const_str_slice(s: &str, start: usize, end: usize) -> &str {
+ let bytes = s.as_bytes();
+
+ // SAFETY: Creating a slice within the bound of original byte slice.
+ // The caller should ensure that: start <= end <= bytes.len()
+ let slice = unsafe { core::slice::from_raw_parts(bytes.as_ptr().add(start), end - start) };
+
+ // SAFETY: the original string is a valid UTF-8 (&str)
+ unsafe { core::str::from_utf8_unchecked(slice) }
Review Comment:
I think we can use the `from_utf8` and just panic on `Err`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "numinnex (via GitHub)" <gi...@apache.org>.
numinnex commented on PR #2427:
URL: https://github.com/apache/iggy/pull/2427#issuecomment-3601355282
Left one more comment, once it's resolved we can merge this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "tungtose (via GitHub)" <gi...@apache.org>.
tungtose commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2579645464
##########
core/server/src/versioning.rs:
##########
@@ -67,13 +68,52 @@ impl FromStr for SemanticVersion {
}
}
-impl SemanticVersion {
- pub fn current() -> Result<Self, IggyError> {
- if let Ok(version) = VERSION.parse::<SemanticVersion>() {
- return Ok(version);
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32(s: &str) -> u32 {
+ let bytes = s.as_bytes();
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = 0;
+
+ while i < bytes.len() {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
+ panic!("Invalid digit in version number");
}
- Err(IggyError::InvalidVersion(VERSION.into()))
+ // ASCII '0' - '9' to 0-9
+ let digit = bytes[i] - b'0';
+ result = result * 10 + digit as u32;
+ i += 1;
+ }
+
+ result
+}
+
+impl SemanticVersion {
+ pub const fn current() -> Self {
Review Comment:
Thanks @numinnex . I actually attempted this approach from the beginning, but it turned out `str::split()` is not const-stable yet. So, I switched to `build.rs` to make a simpler solution. Let me try this to get rid of the `build.rs`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "hubcio (via GitHub)" <gi...@apache.org>.
hubcio commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2580816686
##########
core/server/src/versioning.rs:
##########
@@ -26,7 +26,83 @@ pub struct SemanticVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
- pub prerelease: Option<String>,
+ pub prerelease: Option<&'static str>,
+}
+
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32_range(bytes: &[u8], start: usize, end: usize) -> u32 {
+ if start >= end {
+ panic!("Cannot parse empty range as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = start;
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ while i < end {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
Review Comment:
use `is_digit()` if it exists for `u8`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "tungtose (via GitHub)" <gi...@apache.org>.
tungtose commented on PR #2427:
URL: https://github.com/apache/iggy/pull/2427#issuecomment-3601752494
Thanks @hubcio . I've resolved your comment
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "numinnex (via GitHub)" <gi...@apache.org>.
numinnex commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2578257259
##########
core/server/build.rs:
##########
@@ -48,3 +61,22 @@ fn main() -> Result<(), Box<dyn error::Error>> {
Ok(())
}
+
+/// Parses semantic version string into (major, minor, patch, prerelease)
+///
+/// Note: Duplicated in versioning.rs because build scripts run at compile time
+/// and cannot share code with the runtime library
+fn parse_semver(version: &str) -> (&str, &str, &str, Option<&str>) {
Review Comment:
I think we already have such function somewhere that does something similar.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "numinnex (via GitHub)" <gi...@apache.org>.
numinnex commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2578286311
##########
core/server/src/versioning.rs:
##########
@@ -67,13 +68,52 @@ impl FromStr for SemanticVersion {
}
}
-impl SemanticVersion {
- pub fn current() -> Result<Self, IggyError> {
- if let Ok(version) = VERSION.parse::<SemanticVersion>() {
- return Ok(version);
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32(s: &str) -> u32 {
+ let bytes = s.as_bytes();
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = 0;
+
+ while i < bytes.len() {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
+ panic!("Invalid digit in version number");
}
- Err(IggyError::InvalidVersion(VERSION.into()))
+ // ASCII '0' - '9' to 0-9
+ let digit = bytes[i] - b'0';
+ result = result * 10 + digit as u32;
+ i += 1;
+ }
+
+ result
+}
+
+impl SemanticVersion {
+ pub const fn current() -> Self {
Review Comment:
I actually realized that this whole feature can be implemented even simpler. Observe that there is an `VERSION` const that holds the env `CARGO_PKG_VERSION` and an `FromStr` impl on it does what your `parse_semver` function does, but since `impl const Trait` isn't stabilized yet, we can't use an trait, instead we have to create a const function ourselves.
So, I think you can get rid of the `build.rs` step all together and just use the existing `VERSION` const and create an `const fn` equivalent to the `FromStr` impl for `VERSION` and use that to fill the `SEMANTIC_VERSION` const.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "numinnex (via GitHub)" <gi...@apache.org>.
numinnex commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2578286311
##########
core/server/src/versioning.rs:
##########
@@ -67,13 +68,52 @@ impl FromStr for SemanticVersion {
}
}
-impl SemanticVersion {
- pub fn current() -> Result<Self, IggyError> {
- if let Ok(version) = VERSION.parse::<SemanticVersion>() {
- return Ok(version);
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32(s: &str) -> u32 {
+ let bytes = s.as_bytes();
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = 0;
+
+ while i < bytes.len() {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
+ panic!("Invalid digit in version number");
}
- Err(IggyError::InvalidVersion(VERSION.into()))
+ // ASCII '0' - '9' to 0-9
+ let digit = bytes[i] - b'0';
+ result = result * 10 + digit as u32;
+ i += 1;
+ }
+
+ result
+}
+
+impl SemanticVersion {
+ pub const fn current() -> Self {
Review Comment:
I actually realized that this whole feature can be implemented even simpler. Observe that there is an `VERSION` const that holds the env `CARGO_PKG_VERSION` and an `FromStr` impl on it does what your `parse_semver` function does, but since `impl const Trait` isn't stabilized yet, we can't use an trait and have to create a const function ourselves.
So, I think you can get rid of the `build.rs` step all together and just use the existing `VERSION` const and create an `const fn` equivalent to the `FromStr` impl for `VERSION` and use that to fill the `SEMANTIC_VERSION` const.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "tungtose (via GitHub)" <gi...@apache.org>.
tungtose commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2579645464
##########
core/server/src/versioning.rs:
##########
@@ -67,13 +68,52 @@ impl FromStr for SemanticVersion {
}
}
-impl SemanticVersion {
- pub fn current() -> Result<Self, IggyError> {
- if let Ok(version) = VERSION.parse::<SemanticVersion>() {
- return Ok(version);
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32(s: &str) -> u32 {
+ let bytes = s.as_bytes();
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = 0;
+
+ while i < bytes.len() {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
+ panic!("Invalid digit in version number");
}
- Err(IggyError::InvalidVersion(VERSION.into()))
+ // ASCII '0' - '9' to 0-9
+ let digit = bytes[i] - b'0';
+ result = result * 10 + digit as u32;
+ i += 1;
+ }
+
+ result
+}
+
+impl SemanticVersion {
+ pub const fn current() -> Self {
Review Comment:
I actually attempted this approach from the beginning, but it turned out `str::split()` is not const-stable yet. So, I switched to `build.rs` to make a simpler solution. Let me try this to get rid of the `build.rs`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(server): constify semver [iggy]
Posted by "hubcio (via GitHub)" <gi...@apache.org>.
hubcio commented on code in PR #2427:
URL: https://github.com/apache/iggy/pull/2427#discussion_r2580816686
##########
core/server/src/versioning.rs:
##########
@@ -26,7 +26,83 @@ pub struct SemanticVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
- pub prerelease: Option<String>,
+ pub prerelease: Option<&'static str>,
+}
+
+/// Parses a string slice to u32 at compile time.
+///
+/// # Panics
+/// - If the string is empty
+/// - If the string contains non-digit characters
+const fn const_parse_u32_range(bytes: &[u8], start: usize, end: usize) -> u32 {
+ if start >= end {
+ panic!("Cannot parse empty range as u32");
+ }
+
+ let mut result = 0u32;
+ let mut i = start;
+
+ if bytes.is_empty() {
+ panic!("Can not parse empty string as u32");
+ }
+
+ while i < end {
+ let byte = bytes[i];
+
+ // Validate if the byte is digit
+ if byte < b'0' || byte > b'9' {
Review Comment:
use `is_digit()` if it exists for `u8`
edit: `is_ascii_digit()`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org