我应该使用什么类型来反序列化 Legion ECS 的世界?
What type should I use to deserialize Legion ECS' World?
我想反序列化军团世界,但不知道应该使用什么类型。这是我的反序列化函数:
pub fn deserialize(path: &str) -> World {
let registry = get_registry();
let data_raw = pollster::block_on(load_string(path)).expect("Unable to load file");
let mut deserializer = ron::from_str(data_raw.as_str()).expect("Unable to deserialze the file");
let entity_serializer = Canon::default();
registry.as_deserialize(&entity_serializer).deserialize(&mut deserializer).unwrap()
}
如您所见,deserializer
没有类型。
这可能没有帮助,但这是我实现的序列化功能:
pub fn serialize(world: &World, path: &str) {
let registry = get_registry();
let entity_serializer = Canon::default();
let serializable = world.as_serializable(any(), ®istry, &entity_serializer);
let ron = ron::to_string(
&serializable
).expect("Cannot Serialize World!");
let mut file = File::create(path).expect("Unable to create file");
file.write_all(ron.as_bytes()).expect("Unable to write it to the file");
}
问题中的deserialize
方法来自DeserializeSeed
trait, so its argument have to be something implementing Deserializer
. In case of ron
, the type to use is ron::Deserializer
(准确地说是&mut ron::Deserializer
),可以用Deserializer::from_str
.
创建
因此,这段代码应该有效:
pub fn deserialize(path: &str) -> World {
let registry = get_registry();
let data_raw = pollster::block_on(load_string(path)).expect("Unable to load file");
let mut deserializer = ron::Deserializer::from_str(data_raw.as_str()).expect("Unable to deserialze the file");
let entity_serializer = Canon::default();
registry.as_deserialize(&entity_serializer).deserialize(&mut deserializer).unwrap()
}
我想反序列化军团世界,但不知道应该使用什么类型。这是我的反序列化函数:
pub fn deserialize(path: &str) -> World {
let registry = get_registry();
let data_raw = pollster::block_on(load_string(path)).expect("Unable to load file");
let mut deserializer = ron::from_str(data_raw.as_str()).expect("Unable to deserialze the file");
let entity_serializer = Canon::default();
registry.as_deserialize(&entity_serializer).deserialize(&mut deserializer).unwrap()
}
如您所见,deserializer
没有类型。
这可能没有帮助,但这是我实现的序列化功能:
pub fn serialize(world: &World, path: &str) {
let registry = get_registry();
let entity_serializer = Canon::default();
let serializable = world.as_serializable(any(), ®istry, &entity_serializer);
let ron = ron::to_string(
&serializable
).expect("Cannot Serialize World!");
let mut file = File::create(path).expect("Unable to create file");
file.write_all(ron.as_bytes()).expect("Unable to write it to the file");
}
deserialize
方法来自DeserializeSeed
trait, so its argument have to be something implementing Deserializer
. In case of ron
, the type to use is ron::Deserializer
(准确地说是&mut ron::Deserializer
),可以用Deserializer::from_str
.
因此,这段代码应该有效:
pub fn deserialize(path: &str) -> World {
let registry = get_registry();
let data_raw = pollster::block_on(load_string(path)).expect("Unable to load file");
let mut deserializer = ron::Deserializer::from_str(data_raw.as_str()).expect("Unable to deserialze the file");
let entity_serializer = Canon::default();
registry.as_deserialize(&entity_serializer).deserialize(&mut deserializer).unwrap()
}