From: Ben Pfaff Date: Fri, 25 Jul 2025 14:53:28 +0000 (-0700) Subject: more datum X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2c0a08c8572c2035c2742a7075b002d4e7ff9d0;p=pspp more datum --- diff --git a/rust/pspp/src/data.rs b/rust/pspp/src/data.rs index b907fc513e..a6f1519dcf 100644 --- a/rust/pspp/src/data.rs +++ b/rust/pspp/src/data.rs @@ -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 { +impl Datum +where + B: BorrowMut, +{ /// 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 { 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 { _ => 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 { pub fn with_encoding(self, encoding: &'static Encoding) -> EncodedDatum { match self { Datum::Number(number) => EncodedDatum::Number(number),