[pbs-devel] [PATCH v4 proxmox-backup 4/5] node: status: declutter kernel-version

Gabriel Goller g.goller at proxmox.com
Wed Nov 29 13:50:00 CET 2023


On 11/29/23 11:23, Wolfgang Bumiller wrote:
> On Wed, Nov 29, 2023 at 10:07:45AM +0100, Gabriel Goller wrote:
>>   
>> +#[api]
>> +#[derive(Serialize, Deserialize, Default)]
>> +#[serde(rename_all = "lowercase")]
>> +/// The current kernel version (output of `uname`)
>> +pub struct KernelVersionInformation {
>> +    /// The systemname/nodename
>> +    pub sysname: String,
>> +    /// The kernel release number
>> +    pub release: String,
>> +    /// The kernel version
>> +    pub version: String,
>> +    /// The machine architecture
>> +    pub machine: String,
>> +}
>> +
>> +impl KernelVersionInformation {
>> +    pub fn from_ostr(sysname: &OsStr, release: &OsStr, version: &OsStr, machine: &OsStr) -> Self {
> from_ostr is a bit of a weird name for a public method taking 4
> parameters.
>
> consider a `From<&UtsName>` implementation.
Hmm I was under the impression that we don't want to pull in any more 
crates into `pbs-api-types`...
Another option I had was implement From<[&OsStr; 4] on 
KernelVersionInformation?
>> +        KernelVersionInformation {
>> +            sysname: sysname
>> +                .to_os_string()
>> +                .into_string()
>> +                .unwrap_or("".to_string()),
> .unwrap_or_default(), also, whenver you think "".to_string(), type
> String::new() instead ;-)
>
> also, you first convert to an owned string - this always works, and then
> do the fallible `into_string()`, so even if it's not a valid string you
> clone and discard, instead, do:
>      sysname
>          .to_str()
>          .map(String::from)
>          .unwrap_or_default()
>
> this way there's no string copying if it's not valid utf-8 becausee that
> is checked first
>
> same for the cases below
Ohh, thanks for the heads up!




More information about the pbs-devel mailing list