- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 event_emmiter_rs用于我的应用程序中的事件处理。该库允许您订阅带有回调的事件并触发这些事件。事件采用 (strings, value) 的形式,回调采用接受值参数的闭包形式。通过事件回调发送的值必须实现 Serde::Deserialize。 We can see this here in the docs .所以我创建了这个简单的设置:
use event_emitter_rs::EventEmitter;
use serde::Serialize;
use serde::Deserialize;
use std::borrow::Cow;
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(bound(deserialize = "'de: 'static"))]
//#[serde(bound(deserialize = "'de: 'a"))] using the 'a lifetime gives same error
pub struct DraggableInfo<'a>{
parent: WidgetValue<'a>,
index: WidgetValue<'a>,
draggable_id: WidgetValue<'a>,
}
impl<'a> DraggableInfo<'a>{
pub fn new(parent: &'static str, index: u32, draggable_id: &'static str)->Self{
DraggableInfo{
parent: WidgetValue::CString(Cow::Borrowed(parent)),
index: WidgetValue::Unsized32(index),
draggable_id: WidgetValue::CString(Cow::Borrowed(draggable_id)),
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum WidgetValue<'a>{
Integer32(i32),
Unsized32(u32),
CString(Cow<'a, str>)
}
fn main(){
let mut event_emitter = EventEmitter::new();
event_emitter.on("Print Draggable Value", |dragValue: DraggableInfo| {dbg!(dragValue);});
event_emitter.emit("Print Draggable Value", DraggableInfo::new("root", 0, "layer 1"));
}
但这会导致错误消息:
error: implementation of `Deserialize` is not general enough
--> src\main.rs:34:19
|
34 | event_emitter.on("Print Draggable Value", |dragValue: DraggableInfo| {dbg!(dragValue);});
| ^^ implementation of `Deserialize` is not general enough
|
= note: `DraggableInfo<'_>` must implement `Deserialize<'0>`, for any lifetime `'0`...
= note: ...but `DraggableInfo<'_>` actually implements `Deserialize<'1>`, for some specific lifetime `'1`
我不确定 Deserialize<'0>
是什么和 Deserialize<'1>
消息所指的生命周期是,或者当编译器说 impl “过于笼统”时的确切含义。我该如何解决这个错误?
最佳答案
问题在评论中被驳回,但您的具体问题在此行中很突出:
#[serde(bound(deserialize = "'de: 'static"))]
作为Serde guide预先警告:
Note that
<T> where T: Deserialize<'static>
is never what you want. AlsoDeserialize<'de> + 'static
is never what you want. Generally writing'static
anywhere nearDeserialize
is a sign of being on the wrong track. Use one of the above bounds instead.
这应该具有实际意义:您永远不想从 100% 静态数据反序列化。那些WidgetValue
如果你有,它们会在运行时动态地进出范围(被创建/销毁),对吧?..
但是当你定义 CString
带有 reference 的变体到原始输入缓冲区(反序列化发生的事件负载)——你必须确保没有一个 WidgetValue
永远超过它的输入缓冲区。这就是 Rust 生命周期的目的,它们对机器检查保证 Bad Things™ 不会发生进行编码。
再次来自评论:简单的解决方案是拥有数据而不是借用(引用)它,即
pub enum WidgetValue {
Integer32(i32),
Unsized32(u32),
CString(String),
}
...但是这种简单性会让您付出性能代价,无论何时WidgetValue
,堆分配和无偿字符串复制。 s 被传递。你会失去 zero-copy capacity由 Serde 支持。并不是说它本质上是不好的;也许这个价格适合您的应用。
但是,许多程序员选择 Rust 用于那些性能很重要的应用程序。在安全方面也毫不妥协。也就是说:让我们亲自动手吧。
备注:Unsized32
是矛盾的,你可能打算写Unsigned32
.
注:在DraggableInfo::new
签名,为什么要求借来的字符串切片有'static
生命周期?这太严格了。简单 'a
就足够了。
the error is clear that the type needs to implement Deserialize for any lifetime
的确; .on
signature没有 'de
作为通用参数:
pub fn on<F, T>(&mut self, event: &str, callback: F) -> String where
T: Deserialize<'de>,
F: Fn(T) + 'static + Sync + Send,
这意味着调用者无法选择生命周期。这可能是无意的,也许可以尝试询问作者或 event_emitter_rs
图书馆;可能是一个微妙的错误,可能是设计使然。
顺便说一句:如果你想使用像 serde_json::from_reader
这样的东西为了在流中真正动态地反序列化——这整个零拷贝的努力是行不通的。这表示在DeserializeOwned
约束。 the guide 中也提到了这一事实, For example when deserializing from an IO stream no data can be borrowed.
在这一点上,我放弃了进一步的研究,因为我是在从流中解析 JSON 的上下文中访问这个问题的。这意味着我别无选择,只能做 DeserializeOwned
(即在我的数据结构中使用拥有的 String
而不是 &'a str
)。这也是有道理的,因为流数据是短暂的,所以从中借用根本行不通。
关于rust - Serde::Deserialize 的实现不够通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68156340/
我有一个使用the #[serde(default)] container attribute的结构。 但是,应该有一个字段是必需的(如果输入数据中不存在该字段,则反序列化器应该出错,而不是退回到默认
我正在尝试使用 actix-web 服务器作为通往小型堆栈的网关,以保证堆栈内部的数据格式严格,同时为用户提供一些自由。 为此,我想将 JSON 字符串反序列化为结构,然后对其进行验证、再次序列化并将
正在使用 apache-hive-0.13.1。在创建表配置单元时抛出如下错误 FAILED: Execution Error, return code 1 from org.apache.hadoo
我想添加一个简单的版本方案 + 检查我的结构: #[derive(Serialize, Deserialize)] struct Versioned { version: u32, o
我正在尝试使用 Serde 加载一个 Toml 文件,它包含多个 bool 值,如果在文本文件中找不到,我想将它们全部默认为 false。 我目前的实现是: #[derive(serde::Deser
如何使用 Serde 为远程类型创建序列化程序代理对象?这是一个最小的例子(playground): use serde; // 1.0.104 use serde_json; // 1.0.48 s
我有一个枚举: #[derive(Serialize, Deserialize)] enum Action { Join, Leave, } 和一个结构: #[derive(Seria
我有一个枚举: #[derive(Serialize, Deserialize)] enum Action { Join, Leave, } 和一个结构: #[derive(Seria
在 Rust 中,我从 websocket 接收数据。为简单起见,它看起来像这样: [1, {"a": ["1.2345", 5, "9.8765"]}] 我从 websocket 获得的字符串确实
我的程序解析足够大的 json 文档(30MB),在 CPU 较慢的机器上需要 70 毫秒,我想加快这个过程,我发现 27% 的解析发生在我的 foo_document_type_deserializ
我正在尝试在配置单元中创建一个正则表达式 serde 来读取一些日志文件,但是在让它工作时遇到了问题... 日志文件看起来像这样...... 14.196.202.16:9123 11329 2
我正在使用映射列的 JSON-Serde 功能来重命名我的 json 文档中的列 'Customer ID' -> 'customer_id。我使用映射函数的原因是因为 HQL 不允许在 CREATE
我有一个包含 Text 键和 DoubleWritable 值的序列文件。当我将文件加载为外部表时 Create external table t (id String, data Double) S
假设我有这个结构: use serde::{Serialize, Deserialize}; #[derive(Deserialize)] struct MyStruct { field_1:
这段代码完全按照我的意愿序列化了一个 32 字节的数组: #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOr
我正在使用 event_emmiter_rs用于我的应用程序中的事件处理。该库允许您订阅带有回调的事件并触发这些事件。事件采用 (strings, value) 的形式,回调采用接受值参数的闭包形式。
给定这个枚举定义: #[repr(u8)] #[derive(Debug, Serialize)] pub enum AnimalType { #[serde(rename = "cat")]
我目前正在使用 Rocket 开发一个 Web API,它使用以下结构进行错误响应: #[derive(Serialize, Deserialize)] pub struct ErrorRespons
我想解析这个日志样本 May 3 11:52:54 cdh-dn03 init: tty (/dev/tty6) main process (1208) killed by TERM signal M
我想从访问日志中提取(ip、requestUrl、timeStamp)以加载到 hive 数据库。访问日志中的一行如下。 66.249.68.6 - - [14/Jan/2012:06:25:03 -
我是一名优秀的程序员,十分优秀!