From 3eed34094ac7fc8bfd748bd3c0eff52d4bdaaa5d Mon Sep 17 00:00:00 2001 From: ShryukGrandhi <85529990+ShryukGrandhi@users.noreply.github.com> Date: Tue, 20 Oct 2026 00:13:02 -0700 Subject: [PATCH 1/2] feat(linalg): implement Display for VecView and VecMutView --- src/linalg/basic/vector.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/linalg/basic/vector.rs b/src/linalg/basic/vector.rs index d2e0bae6..0050e457 100644 --- a/src/linalg/basic/vector.rs +++ b/src/linalg/basic/vector.rs @@ -1,4 +1,4 @@ -use std::fmt::{Debug, Display}; +use std::fmt::{self, Debug, Display}; use std::ops::Range; use crate::linalg::basic::arrays::{Array, Array1, ArrayView1, MutArray, MutArrayView1}; @@ -152,6 +152,12 @@ impl MutArray for VecMutView<'_, T> impl ArrayView1 for VecMutView<'_, T> {} impl MutArrayView1 for VecMutView<'_, T> {} +impl fmt::Display for VecMutView<'_, T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self.ptr) + } +} + impl Array for VecView<'_, T> { fn get(&self, i: usize) -> &T { &self.ptr[i] @@ -173,6 +179,12 @@ impl Array for VecView<'_, T> { impl ArrayView1 for VecView<'_, T> {} +impl fmt::Display for VecView<'_, T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self.ptr) + } +} + #[cfg(test)] mod tests { use super::*; @@ -345,4 +357,15 @@ mod tests { vector_ops(&x); } + + #[test] + fn test_display() { + let mut x = vec![1, 2, 3, 4]; + + let view = VecView { ptr: &x[1..3] }; + assert_eq!("[2, 3]", format!("{view}")); + + let mut_view = VecMutView { ptr: &mut x[..2] }; + assert_eq!("[1, 2]", format!("{mut_view}")); + } } From 66b462ca0bc6f672f56aa1dcd66aeb3ea04efe85 Mon Sep 17 00:00:00 2001 From: ShryukGrandhi <85529990+ShryukGrandhi@users.noreply.github.com> Date: Tue, 20 Oct 2026 01:10:04 -0700 Subject: [PATCH 2/2] feat(linalg): implement Display for 1D view trait objects --- src/linalg/basic/arrays.rs | 18 +++++++++++++++ src/linalg/basic/vector.rs | 46 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/linalg/basic/arrays.rs b/src/linalg/basic/arrays.rs index 7c082030..b0fa2b4c 100644 --- a/src/linalg/basic/arrays.rs +++ b/src/linalg/basic/arrays.rs @@ -335,6 +335,15 @@ pub trait ArrayView1: Array { } } +/// Format a 1D view as a list, for example `[1.0, 2.5]`. +/// Elements use their `Debug` format. The elements are streamed into the +/// formatter and are not copied. Formatter flags such as `{:.2}` apply to each element. +impl fmt::Display for dyn ArrayView1 + '_ { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list().entries(self.iterator(0)).finish() + } +} + /// Trait for 2D-array pub trait ArrayView2: Array { /// return max value in array @@ -727,6 +736,15 @@ pub trait MutArrayView1: } } +/// Format a mutable 1D view as a list, for example `[1.0, 2.5]`. +/// Elements use their `Debug` format. The elements are streamed into the +/// formatter and are not copied. Formatter flags such as `{:.2}` apply to each element. +impl fmt::Display for dyn MutArrayView1 + '_ { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list().entries(self.iterator(0)).finish() + } +} + /// Trait for mutable 2D-array views pub trait MutArrayView2: MutArray + ArrayView2 diff --git a/src/linalg/basic/vector.rs b/src/linalg/basic/vector.rs index 0050e457..d3350011 100644 --- a/src/linalg/basic/vector.rs +++ b/src/linalg/basic/vector.rs @@ -154,7 +154,7 @@ impl MutArrayView1 for VecMutView<'_, T> { impl fmt::Display for VecMutView<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self.ptr) + f.debug_list().entries(self.iterator(0)).finish() } } @@ -181,7 +181,7 @@ impl ArrayView1 for VecView<'_, T> {} impl fmt::Display for VecView<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self.ptr) + f.debug_list().entries(self.iterator(0)).finish() } } @@ -368,4 +368,46 @@ mod tests { let mut_view = VecMutView { ptr: &mut x[..2] }; assert_eq!("[1, 2]", format!("{mut_view}")); } + + #[test] + fn test_display_slice() { + let x = vec![1, 2, 3, 4]; + assert_eq!("[2, 3]", format!("{}", x.slice(1..3))); + assert_eq!("[]", format!("{}", x.slice(2..2))); + assert_eq!("[4]", format!("{}", x.slice(3..4))); + + // Elements use the Debug format: 1.0 prints as "1.0", not "1". + let y = vec![1.0f64, 2.5, -0.5]; + assert_eq!("[1.0, 2.5, -0.5]", format!("{}", y.slice(0..3))); + assert_eq!("[1.0]", format!("{}", y.slice(0..1))); + assert_eq!("[1.00, 2.50]", format!("{:.2}", y.slice(0..2))); + } + + #[test] + fn test_display_slice_mut() { + let mut x = vec![1, 2, 3, 4]; + assert_eq!("[2, 3]", format!("{}", x.slice_mut(1..3))); + assert_eq!("[]", format!("{}", x.slice_mut(2..2))); + assert_eq!("[4]", format!("{}", x.slice_mut(3..4))); + + // Elements use the Debug format: 1.0 prints as "1.0", not "1". + let mut y = vec![1.0f64, 2.5, -0.5]; + assert_eq!("[1.0, 2.5, -0.5]", format!("{}", y.slice_mut(0..3))); + assert_eq!("[1.0]", format!("{}", y.slice_mut(0..1))); + assert_eq!("[1.00, 2.50]", format!("{:.2}", y.slice_mut(0..2))); + } + + #[test] + fn test_display_concrete_views() { + let mut y = vec![1.0f64, 2.5]; + assert_eq!("[]", format!("{}", VecView { ptr: &y[..0] })); + assert_eq!("[1.0]", format!("{}", VecView { ptr: &y[..1] })); + assert_eq!("[1.00, 2.50]", format!("{:.2}", VecView { ptr: &y[..] })); + assert_eq!("[]", format!("{}", VecMutView { ptr: &mut y[..0] })); + assert_eq!("[2.5]", format!("{}", VecMutView { ptr: &mut y[1..] })); + assert_eq!( + "[1.00, 2.50]", + format!("{:.2}", VecMutView { ptr: &mut y[..] }) + ); + } }