more datum
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 25 Jul 2025 14:53:28 +0000 (07:53 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 25 Jul 2025 14:53:28 +0000 (07:53 -0700)
rust/pspp/src/data.rs

index b907fc513efea87b32c4a3ef088aaedcff1fa959..a6f1519dcf23d59c27d471bbe3be219543fc5f61 100644 (file)
@@ -28,7 +28,7 @@
 #![cfg_attr(not(test), warn(missing_docs))]
 
 use std::{
-    borrow::{Borrow, Cow},
+    borrow::{Borrow, BorrowMut, Cow},
     cmp::Ordering,
     fmt::{Debug, Display, Formatter},
     hash::Hash,
@@ -675,23 +675,33 @@ where
             _ => self == other,
         }
     }
+
+    pub fn as_encoded<'a>(&'a self, encoding: &'static Encoding) -> EncodedDat<'a> {
+        match self {
+            Datum::Number(number) => EncodedDat::Number(*number),
+            Datum::String(raw_string) => {
+                EncodedDat::String(raw_string.borrow().as_encoded(encoding))
+            }
+        }
+    }
 }
 
-impl Datum<RawString> {
+impl<B> Datum<B>
+where
+    B: BorrowMut<RawString>,
+{
     /// Returns the string inside this datum as a mutable borrow, or `None` if
     /// this is a numeric datum.
     pub fn as_string_mut(&mut self) -> Option<&mut RawString> {
         match self {
             Self::Number(_) => None,
-            Self::String(s) => Some(s),
+            Self::String(s) => Some(s.borrow_mut()),
         }
     }
 
-    pub fn as_encoded<'a>(&'a self, encoding: &'static Encoding) -> EncodedDat<'a> {
-        match self {
-            Datum::Number(number) => EncodedDat::Number(*number),
-            Datum::String(raw_string) => EncodedDat::String(raw_string.as_encoded(encoding)),
-        }
+    /// Removes trailing ASCII spaces from this datum, if it is a string.
+    pub fn trim_end(&mut self) {
+        self.as_string_mut().map(|s| s.trim_end());
     }
 
     /// Resizes this datum to the given `width`.  Returns an error, without
@@ -701,6 +711,7 @@ impl Datum<RawString> {
         match (self, width) {
             (Self::Number(_), VarWidth::Numeric) => Ok(()),
             (Self::String(s), VarWidth::String(new_width)) => {
+                let s = s.borrow_mut();
                 if s.is_resizable(new_width as usize) {
                     s.resize(new_width as usize);
                     Ok(())
@@ -711,15 +722,9 @@ impl Datum<RawString> {
             _ => Err(()),
         }
     }
+}
 
-    /// Removes trailing ASCII spaces from this datum, if it is a string.
-    pub fn trim_end(&mut self) {
-        match self {
-            Self::Number(_) => (),
-            Self::String(s) => s.trim_end(),
-        }
-    }
-
+impl Datum<RawString> {
     pub fn with_encoding(self, encoding: &'static Encoding) -> EncodedDatum {
         match self {
             Datum::Number(number) => EncodedDatum::Number(number),