Idiomatic fmt::Debug for 2d vector

I have a vec<u32> that is modeling a ringbuffer of N entries, of SQE_WORDS in size. I'd like to override the Debug/PrettyPrint output for my struct.

The goal is to have output that looks like:

head                :          0
tail                :          0
buf                 : [
[AABB0011, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000],
[00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000],
[00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000],
[00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000],
[00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000],
[00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000],
[00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000],
[00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000, 00000000],
]

The code I currently have is below, but am wondering if there is a better way to achieve this without repeated calls to write!().unwrap()

use std::fmt;
use std::fmt::Write;

struct AdminSq
{
    head:         u32,
    tail:         u32,

    buf: Vec<u32>,
}

impl fmt::Debug for AdminSq
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
    {
        let mut b = String::new();
        write!(&mut b, "{:20}: [\n", "buf").unwrap();

        for x in self
            .buf
            .iter()
            .collect::<Vec<_>>()
            .chunks(AdminSq::SQE_WORDS as usize)
        {
            write!(&mut b, "{:08X?},\n", x).unwrap();
        }
        write!(&mut b, "]\n").unwrap();

        write!(f,
            "{:20}: {:10}\n\
             {:20}: {:10}\n\
             {}\
             "
            , "head", &self.head
            , "tail", &self.tail
            , b
        )
    }
}

impl AdminSq
{
    const SQE_BYTES: u32 = 64;
    const SQE_WORDS: u32 = AdminSq::SQE_BYTES / 4;

    pub fn new(num_elements: u32) -> AdminSq
    {
        let buf = vec![0; (AdminSq::SQE_WORDS * num_elements) as usize];
        AdminSq {
            head:         0,
            tail:         0,
            buf:          buf,
        }
    }
}

fn main()
{
    let asq = AdminSq::new(8);
    println!("admin sq:--------------\n{:?}", asq);
}

(Playground)

If you use write!(...)?; instead, any errors should be propagated out to the caller instead of causing an immediate panic.

I wasn't aware of the ? operator, that clears up the error handling, thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.