使用格式!具有 Box<dyn Display + 'static> 类型的宏

use the format! macro with something of type Box<dyn Display + 'static>

我正在尝试为我目前正在编写的语言编写单元测试,所以我正在努力在解析器中存储和检索变量,但出现错误

error[E0277]: the size for values of type `dyn std::fmt::Display` cannot be known at compilation time
   --> parser/src/env.rs:48:33
    |
48  |         assert_eq!(String::from(format!("{}", *get_binding("test", &env).unwrap().get())), "hello".to_string())
    |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-
time
    |
    = help: the trait `Sized` is not implemented for `dyn std::fmt::Display`
note: required by a bound in `ArgumentV1::<'a>::new`
   --> /home/muppi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:314:20
    |
314 |     pub fn new<'b, T>(x: &'b T, f: fn(&T, &mut Formatter<'_>) -> Result) -> ArgumentV1<'b> {
    |                    ^ required by this bound in `ArgumentV1::<'a>::new`
    = note: this error originates in the macro `$crate::__export::format_args` (in Nightly builds, run with -Z macro-backtrace for m
ore info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `parser` due to previous error

我的代码

pub mod env {
    use crate::vars::vars::*;
    use std::{collections::HashMap, fmt::Display};

    #[derive(Default)]
    pub struct Environ {
        pub bindings: HashMap<String, Var<Box<dyn Display + 'static>>>,
    }
    #[allow(dead_code)]
    pub(crate) fn store_var(name: &str, var: Var<Box<dyn Display + 'static>>, env: &mut Environ) {
        env.bindings.insert(String::from(name), var);
    }
    #[allow(dead_code)]
    pub(crate) fn get_binding<'a>(name: &'a str, env: &'a Environ) -> Result<&'a Var<Box<dyn Display>>, String> {
        if env.bindings.contains_key(&name.to_string()) {
            let val = env.bindings.get(&name.to_string());
            Ok(val.unwrap())
        } else {
            Err("Key does not exist".to_string())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::env::{Environ, store_var, *};
    use crate::vars::vars::*;
    
    #[test]
    fn store_binding() {
        let mut env = Environ::default();
        store_var("test", Var::<i32>::new(Box::new(5), None), &mut env);
        assert_eq!(env.bindings.len(), 1)
    }
    #[test]
    #[should_panic(expected="value not found")]
    fn retrieve_non_existent_binding() {
        let mut env = Environ::default();
        store_var("test", Var::<&str>::new(Box::new("hello"), None), &mut env);
        if get_binding("this_does_exist", &env).is_err() {
            panic!("value not found")
        }
    }
    #[test]
    fn retrieve_binding() {
        let mut env = Environ::default();
        store_var("test", Var::<&str>::new(Box::new("hello"), None), &mut env);
        assert_eq!(String::from(format!("{}", *get_binding("test", &env).unwrap().get())), "hello".to_string())
    }
}

最后一个测试是错误的地方。这个测试是为了使用格式!宏并格式化字符串以生成包含存储在变量中的数据的字符串。 变量库

pub mod vars {
    use std::fmt;

    #[derive(Debug, Clone, PartialEq)]
    pub struct Var<T> {
        pub val: Box<T>,
        pub desc: Option<String>,
    }

    impl<T> std::ops::Deref for Var<T> {
        type Target = Box<T>;
    
        fn deref(&self) -> &Self::Target {
            &self.val
        }
    }
    impl<T> Var<T> {
        pub fn new<Y>(val: Y, desc: Option<&str>) -> Var<Y> {
            if let Some(desc) = desc {
                Var {val: Box::new(val), desc: Some(desc.to_string())}
            } else {
                Var {val: Box::new(val), desc: None}
            }
        }
        pub fn get(self) -> T {
            *self.val
        }
    }
    impl<T> fmt::Display for Var<T>
    where
        T: fmt::Display,
    {
        fn fmt(self: &Var<T>, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{}", *self.val)
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::vars::vars::*;

    #[test]
    fn new_var() {
        assert_eq!(
            Var::<i32>::new(10, Some("hello_world")),
            Var {
                val: Box::new(10),
                desc: Some("hello_world".to_string())
            }
        )
    }
}

问题不在于显示,而是您无法在 get_binding() 返回的 &Var 上调用 get()。例如,这不会编译:

let x = get_binding("test", &env).unwrap().get();

这是因为 get_binding() returns 对 Var 的引用,而 Var::get() 按值获取 self(并使用它)。您尝试通过添加 * 取消引用来解决问题只会导致您遇到一个新的类型错误,它掩盖了实际问题和解决问题的正确方法。

一旦根本原因显而易见,明显的解决方法是更改​​ get(),使其采用 &self 和 returns 引用,使其与 [= 返回的引用兼容14=]。例如:

pub fn get(&self) -> &T {
    &self.val
}

通过此更改,您的测试可以编译并通过。