我正在使用纹理合成板条箱。我要创建一个结构RenderSettings,并将其提供给这个板条箱的一些函数:
texture-synthesis = "0.8.0"use texture_synthesis as ts;
#[derive(Debug, Clone, Copy)]
pub struct RenderSettings{
seed: u64,
tiling_mode: bool,
nearest_neighbors: u32,
output_size: ts::Dims, //throws error
}然而,这给了我一个错误:
`texture_synthesis::Dims` doesn't implement `std::fmt::Debug`
`texture_synthesis::Dims` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`我在source code中看到了Dims结构的定义,它似乎实现了调试:
#[derive(Copy, Clone)]
#[cfg_attr(test, derive(Debug, PartialEq))]
pub struct Dims {
pub width: u32,
pub height: u32,
}我想我的问题和#[cfg_attr(test, derive(Debug, PartialEq))]有关。我不太确定这句话是什么意思,但它似乎表明我可以通过某种方式将Debug与此结构一起使用。那么我该如何修复这个错误呢?
发布于 2020-12-19 10:10:00
#[cfg_attr(test, derive(Debug, PartialEq))]是conditional compilation的一个例子,它只会在指定test属性时派生Debug和PartialEq (本质上是在运行cargo test时)。
如果你想要Debug,你可以考虑做一个新类型的包装器:
use std::fmt;
#[derive(Copy, Clone)]
pub struct DimsWrapper(ts::Dims);
impl fmt::Debug for DimsWrapper {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("DimsWrapper")
.field("width", &self.0.width)
.field("height", &self.0.height)
.finish()
}
}然后,您可以使用以下命令创建一个:
DimsWrapper(ts::Dims {
width: 5,
height: 6
});并使用以下命令提取ts::Dims:
dims_wrapper.0或者使用模式匹配。如果您希望能够直接在其上调用Dims的方法,请考虑实现Deref
impl std::ops::Deref for DimsWrapper {
type Target = ts::Dims;
fn deref(&self) -> &Self::Target {
&self.0
}
}https://stackoverflow.com/questions/65364341
复制相似问题