Repository: nushell/nushell
File: crates/nu-protocol/src/value/record.rs (Rust)

Code before:
    }

    /// Remove single value by key and return it
    pub fn remove(&mut self, col: impl AsRef<str>) -> Option<Value> {
        let index = self.index_of(col.as_ref())?;
        Some(self.0.remove_index(index))
    }

    /// Insert into the record, replacing preexisting value if found.
    ///
    /// Returns `Some(previous_value)` if found. Else `None`
    pub fn insert<K>(&mut self, col: K, val: Value) -> Option<Value>
    where
        K: AsRef<str> + Into<String>,
    {
        if let Some(curr_val) = self.get_mut(col.as_ref()) {
            Some(std::mem::replace(curr_val, val))
        } else {
            self.0.push(col, val);
            None
        }
    }
}

/// Extension trait for [`Record`]. Enables separate implementations for `&Record` and `&mut Record`
pub trait RecordExt {
    type Ref<S: CasingCmp>;
    fn case_sensitive(self) -> Self::Ref<CaseSensitive>;
    fn case_insensitive(self) -> Self::Ref<CaseInsensitive>;
}

impl<'a> RecordExt for &'a Record {
    type Ref<S: CasingCmp> = &'a CaseTypedRecord<S>;

    #[inline]
    fn case_sensitive(self) -> Self::Ref<CaseSensitive> {
        CaseTypedRecord::<CaseSensitive>::from_record(self)
    }

    #[inline]
    fn case_insensitive(self) -> Self::Ref<CaseInsensitive> {
        CaseTypedRecord::<CaseInsensitive>::from_record(self)
    }
}

impl<'a> RecordExt for &'a mut Record {
    type Ref<S: CasingCmp> = &'a mut CaseTypedRecord<S>;

    #[inline]
    fn case_sensitive(self) -> Self::Ref<CaseSensitive> {
        CaseTypedRecord::<CaseSensitive>::from_record_mut(self)

Review comment (suggestion):
I'm having an issue with that trait name. `Ext`-traits usually refer to a feature extension of a foreign type or trait. This is not the case here, we have full control over `Record` here. So I would name it differently to make it semantics more clear.

Code after:
    }

    /// Remove single value by key and return it
    pub fn remove(&mut self, col: impl AsRef<str>) -> Option<Value> {
        let index = self.index_of(col.as_ref())?;
        Some(self.0.remove_index(index))
    }

    /// Insert into the record, replacing preexisting value if found.
    ///
    /// Returns `Some(previous_value)` if found. Else `None`
    pub fn insert<K>(&mut self, col: K, val: Value) -> Option<Value>
    where
        K: AsRef<str> + Into<String>,
    {
        if let Some(curr_val) = self.get_mut(col.as_ref()) {
            Some(std::mem::replace(curr_val, val))
        } else {
            self.0.push(col, val);
            None
        }
    }
}

impl<'a> WrapCased for &'a Record {
    type Wrapper<S: CaseSensitivity> = &'a CasedRecord<S>;

    #[inline]
    fn case_sensitive(self) -> Self::Wrapper<CaseSensitive> {
        CasedRecord::<CaseSensitive>::from_record(self)
    }

    #[inline]
    fn case_insensitive(self) -> Self::Wrapper<CaseInsensitive> {
        CasedRecord::<CaseInsensitive>::from_record(self)
    }
}

impl<'a> WrapCased for &'a mut Record {
    type Wrapper<S: CaseSensitivity> = &'a mut CasedRecord<S>;

    #[inline]
    fn case_sensitive(self) -> Self::Wrapper<CaseSensitive> {
        CasedRecord::<CaseSensitive>::from_record_mut(self)
    }

    #[inline]
    fn case_insensitive(self) -> Self::Wrapper<CaseInsensitive> {
        CasedRecord::<CaseInsensitive>::from_record_mut(self)
    }
}