Re: [PATCH v7 5/6] rust: str: add radix prefixed integer parsing functions

Andreas Hindborg
2025-02-24T13:44:05Z

"Daniel Almeida" <daniel.almeida@collabora.com> writes:

> Hi Andreas,
>
>> On 18 Feb 2025, at 10:00, Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> Add the trait `ParseInt` for parsing string representations of integers
>> where the string representations are optionally prefixed by a radix
>> specifier. Implement the trait for the primitive integer types.
>>
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> ---
>> rust/kernel/str.rs | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 118 insertions(+)
>>
>> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
>> index db272d2198fcc..8b0d814b47f52 100644
>> --- a/rust/kernel/str.rs
>> +++ b/rust/kernel/str.rs
>> @@ -945,3 +945,121 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
>> macro_rules! fmt {
>> ($($f:tt)*) => ( core::format_args!($($f)*) )
>> }
>> +
>> +pub mod parse_int {
>> + //! Integer parsing functions for parsing signed and unsigned integers
>> + //! potentially prefixed with `0x`, `0o`, or `0b`.
>> +
>> + use crate::prelude::*;
>> + use crate::str::BStr;
>> + use core::ops::Deref;
>> +
>> + /// Trait that allows parsing a [`&BStr`] to an integer with a radix.
>> + ///
>> + /// [`&BStr`]: kernel::str::BStr
>> + // This is required because the `from_str_radix` function on the primitive
>> + // integer types is not part of any trait.
>> + pub trait FromStrRadix: Sized {
>
> Is this supposed to be implemented by somebody else? Otherwise we should seal it,
> perhaps?

That is a good point. I did not intend for the user to implement this,
same for `ParseInt`. I will look into sealing them.

Best regards,
Andreas Hindborg