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

Code before:
    pub fn iter(&self) -> Iter {
        self.into_iter()
    }

    pub fn iter_mut(&mut self) -> IterMut {
        self.into_iter()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Naive push to the end of the datastructure.
    ///
    /// <div class="warning">
    /// May duplicate data!
    ///
    /// Consider using [`CaseTypedRecord::insert`] or [`CasedRecord::insert`] instead.
    /// </div>
    pub fn push(&mut self, col: impl Into<String>, val: Value) {
        self.inner.push((col.into(), val));
    }

    pub fn get_index(&self, idx: usize) -> Option<(&String, &Value)> {
        self.inner.get(idx).map(|(col, val): &(_, _)| (col, val))
    }

    pub fn get_index_mut(&mut self, idx: usize) -> Option<(&mut String, &mut Value)> {
        self.inner.get_mut(idx).map(|(col, val)| (col, val))
    }

    /// Remove single value by index
    fn remove_index(&mut self, index: usize) -> Value {
        self.inner.remove(index).1
    }

    /// Remove elements in-place that do not satisfy `keep`
    ///
    /// ```rust
    /// use nu_protocol::{record, Value};
    ///
    /// let mut rec = record!(
    ///     "a" => Value::test_nothing(),
    ///     "b" => Value::test_int(42),
    ///     "c" => Value::test_nothing(),
    ///     "d" => Value::test_int(42),
    /// );

Review comment (suggestion):
With this attached I'm always unsure if we should mark this function as `unsafe`, as you can break the invariance of the `Record` if used `push` inproperly.

Code after:

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Naive push to the end of the datastructure.
    ///
    /// <div class="warning">
    /// May duplicate data!
    ///
    /// Consider using [`CasedRecord::insert`] or [`DynCasedRecord::insert`] instead.
    /// </div>
    pub fn push(&mut self, col: impl Into<String>, val: Value) {
        self.inner.push((col.into(), val));
    }

    pub fn get_index(&self, idx: usize) -> Option<(&String, &Value)> {
        self.inner.get(idx).map(|(col, val): &(_, _)| (col, val))
    }

    pub fn get_index_mut(&mut self, idx: usize) -> Option<(&mut String, &mut Value)> {
        self.inner.get_mut(idx).map(|(col, val)| (col, val))
    }

    /// Remove single value by index
    fn remove_index(&mut self, index: usize) -> Value {
        self.inner.remove(index).1
    }

    /// Remove elements in-place that do not satisfy `keep`
    ///
    /// ```rust
    /// use nu_protocol::{record, Value};
    ///
    /// let mut rec = record!(
    ///     "a" => Value::test_nothing(),
    ///     "b" => Value::test_int(42),
    ///     "c" => Value::test_nothing(),
    ///     "d" => Value::test_int(42),
    /// );
    /// rec.retain(|_k, val| !val.is_nothing());
    /// let mut iter_rec = rec.columns();
    /// assert_eq!(iter_rec.next().map(String::as_str), Some("b"));
    /// assert_eq!(iter_rec.next().map(String::as_str), Some("d"));
    /// assert_eq!(iter_rec.next(), None);
    /// ```
    pub fn retain<F>(&mut self, mut keep: F)