Lifetime inference problem when implementing iterator with refs - iterator

I'm implementing a simple Iterator for a struct that contains a ref:
extern crate zip;
extern crate quick_xml;
extern crate chrono;
use std::io::{Seek, Write, Read, Error};
use std::fs::File;
use xlsx_read::zip::read::ZipFile;
use xlsx_read::zip::result::ZipResult;
use xlsx_read::zip::ZipArchive;
use xlsx_read::zip::write::{FileOptions, ZipWriter};
use xlsx_read::quick_xml::Reader as XmlReader;
use xlsx_read::quick_xml::events::Event;
use std::io::BufReader;
use xlsx_read::chrono::prelude::*;
pub struct XlsxFile<'a> {
path: &'a str,
archive: ZipArchive<File>,
sheet_count: usize,
curr: usize,
}
impl<'a> XlsxFile<'a> {
pub fn from(path: &'a str) -> Result<XlsxFile, Error> {
let file = File::open(path)?;
let archive = ZipArchive::new(file)?;
let sheet_count = archive.len();
Ok(XlsxFile {
path: path,
archive: archive,
sheet_count,
curr: 0,
})
}
}
pub struct XlsxSheet<'a> {
pub name: &'a str,
pub index: usize,
}
impl<'a> Iterator for XlsxFile<'a> {
type Item = XlsxSheet<'a>;
fn next(&mut self) -> Option<XlsxSheet<'a>> {
loop {
if self.sheet_count > 0 &&
self.sheet_count > self.curr {
let zip_file = self.archive.by_index(self.curr).unwrap();
let file_name = zip_file.name();
if file_name.contains("xl/worksheets/sheet") {
let sheet = XlsxSheet {
name: file_name, // works fine if String::from(file_name) is used
index: self.curr,
};
self.curr += 1;
return Some(sheet);
}
self.curr += 1;
continue;
} else {
break;
}
}
return None;
}
}
static XLSX_FILE: &'static str = "<location_to_xlsx_file>";
fn main() {
let mut file = xlsx_read::XlsxFile::from(XLSX_FILE).unwrap();
file.for_each(|s| println!("idx: {:?}", s.name));
}
But I get the following error:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/xlsx_read.rs:50:45
|
50 | let zip_file = self.archive.by_index(self.curr).unwrap();
| ^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 46:5...
--> src/xlsx_read.rs:46:5
|
46 | / fn next(&mut self) -> Option<XlsxSheet<'a>> {
47 | | loop {
48 | | if self.sheet_count > 0 &&
49 | | self.sheet_count > self.curr {
... |
66 | | return None;
67 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/xlsx_read.rs:50:32
|
50 | let zip_file = self.archive.by_index(self.curr).unwrap();
| ^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 43:1...
--> src/xlsx_read.rs:43:1
|
43 | impl<'a> Iterator for XlsxFile<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...so that the expression is assignable:
expected std::option::Option<xlsx_read::XlsxSheet<'a>>
found std::option::Option<xlsx_read::XlsxSheet<'_>>
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
My question is, how to tell Rust compiler to use appropriate lifetime here? Even though I've defined XlsxSheet<'a> with lifetime modifier and want to tie name to &'a str but somehow this doesn't translate into a valid Rust code.

Easy Solution: This problem can be trivially fixed by using String instead of &'a str.
Explanation:
I don't know the definition of by_index, which seems to be quite crucial to this problem. The following reasoning is pure guess and it's not reliable. It's offered only for reference.
self.archive borrows self (which is valid over the entire scope, let's say the lifetime is named 'me), and has lifetime 'me.
Thus the return value of by_index has lifetime 'me.
Oops, XlsxSheet<'me> is not compatible with XlsxSheet<'a> (which is expected)!
What we want here is XlsxSheet<'me> being a subtype of XlsxSheet<'a>, which in turn implies 'me being a subtype of 'a, if XlsxSheet is covariant. Therefore, you can state them explicitly
fn next(&mut self) -> Option<XlsxSheet<'a>> where Self: 'a
// or
impl<'a> Iterator for XlsxFile<'a> + 'a

Related

Support errors with lifetimes to have a source

I have the following 3 errors, AError, BError and CError, where CError contains a borrowed string from some input, and AError is composed from either BError or CError, and thus has a lifetime too.
use std::error::Error;
use std::fmt;
#[derive(Debug)]
pub enum AError<'a> {
B(BError),
C(CError<'a>),
}
impl<'a> fmt::Display for AError<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "AError")
}
}
impl<'a> Error for AError<'a> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
AError::B(err) => Some(err),
AError::C(_) => None, // If I try to use `C(err) => Some(err)` it errors
}
}
}
#[derive(Debug)]
pub struct BError;
impl fmt::Display for BError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "BError")
}
}
impl Error for BError {}
#[derive(Debug)]
pub struct CError<'a> {
c: &'a str,
}
impl<'a> fmt::Display for CError<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "CError: {}", self.c)
}
}
impl<'a> Error for CError<'a> {}
If I try to make A return C as a source error, I get the following error:
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/lib.rs:18:15
|
18 | match self {
| ^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the impl at 16:6...
--> src/lib.rs:16:6
|
16 | impl<'a> Error for AError<'a> {
| ^^
note: ...so that the types are compatible
--> src/lib.rs:18:15
|
18 | match self {
| ^^^^
= note: expected `&AError<'_>`
found `&AError<'a>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
--> src/lib.rs:18:9
|
18 | / match self {
19 | | AError::B(err) => Some(err),
20 | | AError::C(err) => Some(err),
21 | | }
| |_________^
= note: expected `Option<&(dyn std::error::Error + 'static)>`
found `Option<&dyn std::error::Error>`
Which makes sense as source is declared with fn source(&self) -> Option<&(dyn Error + 'static)>.
I understand in order to be able to downcast, you must have a static reference, but all I want to do with it is print a backtrace of errors using source as such:
pub fn fmt_err(err: &dyn Error, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{}", err)?;
if let Some(source) = err.source() {
fmt_err(source, f)?;
}
Ok(())
}
which doesn't require downcasting, simply uses the fact that fmt::Display is a super-trait of Error.
How can I make my errors with lifetimes support this type of usecase?
I know Error::cause exists, but it is deprecated and likely not intended for this use case and popular error deriving libraries (such as thiserror) likely don't implement it either, so I have to make do with source as my only way of getting an underlying error.
Is my only option to make my own trait and implement it for any errors with lifetimes?

How to destructure enum when de/serializing yaml or json with serde?

I have a piece of serde code which does what I want, but I don't like how it does it. I'm looking for help with figuring out on how to improve it.
Playground
use std::any::Any;
trait Model: std::fmt::Debug + Any {
fn as_any(&self) -> &dyn Any;
}
impl Model for Generator {
fn as_any(&self) -> &dyn Any {
self
}
}
impl Model for Connector {
fn as_any(&self) -> &dyn Any {
self
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Generator {
id: String,
#[serde(rename = "sourceID")]
source_id: String,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Connector {
id: String,
#[serde(rename = "sourceID")]
source_id: String,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type")]
enum AllModels {
Generator(Generator),
Connector(Connector),
}
fn main() {
let data = r#"
- sourceID: "generator-01"
id: "connector-01"
type: "Generator"
- sourceID: "geneiator-01"
type: "Connector"
id: "connector-01"
"#;
let p: Vec<Box<dyn Model>> = serde_yaml::from_str::<Vec<AllModels>>(&data)
.unwrap()
.into_iter()
.collect();
println!("{:?}", p);
let l = serde_yaml::to_string(&p.into_iter().collect::<Vec<AllModels>>());
println!("{:?}", l);
}
impl std::iter::FromIterator<AllModels> for Vec<Box<dyn Model>> {
fn from_iter<I: IntoIterator<Item = AllModels>>(iter: I) -> Self {
let mut v: Vec<Box<dyn Model>> = Vec::new();
for i in iter {
match i {
AllModels::Generator(d) => {
v.push(Box::new(d));
}
AllModels::Connector(d) => {
v.push(Box::new(d));
}
}
}
v
}
}
impl std::iter::FromIterator<std::boxed::Box<dyn Model>> for std::vec::Vec<AllModels> {
fn from_iter<I: IntoIterator<Item = Box<dyn Model>>>(iter: I) -> Self {
let mut v: Vec<AllModels> = Vec::new();
for i in iter {
if let Some(model) = i.as_any().downcast_ref::<Generator>() {
v.push(AllModels::Generator(model.clone()));
} else if let Some(model) = i.as_any().downcast_ref::<Connector>() {
v.push(AllModels::Connector(model.clone()));
}
}
v
}
}
What I'm trying to achieve is to de/serialize yaml into one of multiple structs, dynamically choosing to which struct should it deserialize to based on value of type field in yaml it parses. e.g.
- id: Foo
source: Bar
type: Connector
should be parsed into struct Connector
I figured I could use enum representation to deal with that, however, it produces undesired side effect - by default following yaml:
- id: Foo
source: Bar
type: Connector
- id: Foo
source: Bar
type: Generator
will be parsed as:
[Connector(Connector{...}), Generator(Generator{...})]
so my structs are wrapped in enum variants. In order to "unwrap it" I figured I could implement FromIterator<AllModels> for Vec<Box<dyn Model>> , thanks to which and type conversion/coercion(not sure which one is the right word) the output changes to:
[Connector{...}, Generator{...}]
so far so good.
Two issues I'm having with this solution, are:
code repetition - for each new struct (Connector,Generator,...) I have to update enum AllModels and match arm inside FromIterator implementation - the latter is what bothers me the most. I could do it with macro probably, but I haven't learned how to write them, and before I do so, I'd like to explore other possible solutions
extra iteration - in order to convert from Vec<enum variant> to Vec<struct> I need to do the following: let p: Vec<Box<dyn Model>> = serde_yaml::from_str::<Vec<AllModels>>(&data).unwrap().into_iter().collect();. I'd prefer if the conversion would happen without an extra iteration
I have considered a few of options, but I'm not able to figure how to implement them...
A. serde container attribute from/into docs
#[serde(from = "FromType")] - the way I think it would work is by force-converting my enum variant straight into desired struct, with no extra iteration and no code repetition. However, I fail to implement it - Playground.
When I'm trying to add from attribute
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
enum AllModels {
Gene(Generator),
Connector(Connector),
}
the compiler will yell at me
❯ cargo r
Compiling serdeissue v0.1.0 (/sandbox/serdeissue)
error[E0277]: the trait bound `Box<dyn Model>: From<AllModels>` is not satisfied
--> src/main.rs:21:24
|
21 | #[derive(Debug, Clone, Serialize, Deserialize)]
| ^^^^^^^^^ the trait `From<AllModels>` is not implemented for `Box<dyn Model>`
|
= help: the following implementations were found:
<Box<(dyn StdError + 'a)> as From<E>>
<Box<(dyn StdError + 'static)> as From<&str>>
<Box<(dyn StdError + 'static)> as From<Cow<'a, str>>>
<Box<(dyn StdError + 'static)> as From<std::string::String>>
and 22 others
= note: required because of the requirements on the impl of `Into<Box<dyn Model>>` for `AllModels`
= note: required by `into`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `dyn Model: Serialize` is not satisfied
--> src/main.rs:21:24
|
21 | #[derive(Debug, Clone, Serialize, Deserialize)]
| ^^^^^^^^^ the trait `Serialize` is not implemented for `dyn Model`
|
::: /home/marcin/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/ser/mod.rs:247:18
|
247 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
| - required by this bound in `serialize`
|
= note: required because of the requirements on the impl of `Serialize` for `Box<dyn Model>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `dyn Model: Deserialize<'_>` is not satisfied
--> src/main.rs:21:35
|
21 | #[derive(Debug, Clone, Serialize, Deserialize)]
| ^^^^^^^^^^^ the trait `Deserialize<'_>` is not implemented for `dyn Model`
|
::: /home/marcin/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/de/mod.rs:539:12
|
539 | D: Deserializer<'de>;
| ----------------- required by this bound in `_::_serde::Deserialize::deserialize`
|
= note: required because of the requirements on the impl of `Deserialize<'_>` for `Box<dyn Model>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `AllModels: From<Box<dyn Model>>` is not satisfied
--> src/main.rs:21:35
|
21 | #[derive(Debug, Clone, Serialize, Deserialize)]
| ^^^^^^^^^^^ the trait `From<Box<dyn Model>>` is not implemented for `AllModels`
|
::: /home/marcin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/convert/mod.rs:372:1
|
372 | pub trait From<T>: Sized {
| ------------------------ required by this bound in `From`
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
My angle of attack is following: use error msg to copy-pasteroni-dummy-implementoni missing trait bounds:
impl From<AllModels> for Box<dyn Model> {
fn from(am: AllModels) -> Self {
Box::new(Generator{id:String::from("arst"),source_id:String::from("arst")})
}
}
impl Serialize for dyn Model {
fn serialize(&self) -> Self {
Box::new(Generator{id:String::from("arst"),source_id:String::from("arst")})
}
}
impl Deserialize<'_> for dyn Model {}
impl From<Box<dyn Model>> for AllModels {
fn from(dm: Box<dyn Model>) -> Self {
AllModels::Gene(Generator{id:String::from("arst"),source_id:String::from("arst")})
}
}
but then this happens:
❯ cargo r
Compiling serdeissue v0.1.0 (/sandbox/serdeissue)
error[E0277]: the size for values of type `(dyn Model + 'static)` cannot be known at compilation time
--> src/main.rs:75:6
|
75 | impl Deserialize<'_> for dyn Model {}
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
::: /home/marcin/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/de/mod.rs:530:29
|
530 | pub trait Deserialize<'de>: Sized {
| ----- required by this bound in `Deserialize`
|
= help: the trait `Sized` is not implemented for `(dyn Model + 'static)`
and that's a game over for me
B. erased-serde
this seems to be the right tool for the job, but again, I run into problems when implementing it (no wonder - I have no idea what I'm doing:)
use erased_serde::{Deserializer, Serializer, serialize_trait_object};
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Generator {
id: String,
source: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Connector {
id: String,
source: String,
}
trait Model: std::fmt::Debug + erased_serde::Serialize {}
erased_serde::serialize_trait_object!(Model);
impl Model for Generator {}
impl Model for Connector {}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", into = "Box<dyn Model>", from = "Box<dyn Model>")]
enum AllModels {
Generator(Generator),
Connector(Connector),
}
fn main() {
let data = r#"
- source: "generator-01"
id: "g-01"
type: "Generator"
- source: "connector-01"
type: "Connector"
id: "c-01"
"#;
let p: Vec<Box<dyn Model>> = serde_yaml::from_str(&data).unwrap();
println!("{:?}", p);
}
impl From<AllModels> for Box<dyn Model> {
fn from(am: AllModels) -> Self {
Box::new(Generator{id:String::from("arst"),source_id:String::from("arst")})
}
}
// impl Serialize for dyn Model {
// fn serialize(&self) -> Self {
// Box::new(Generator{id:String::from("arst"),source_id:String::from("arst")})
// }
// }
impl Deserialize<'_> for dyn Model {}
impl From<Box<dyn Model>> for AllModels {
fn from(dm: Box<dyn Model>) -> Self {
AllModels::Generator(Generator{id:String::from("arst"),source_id:String::from("arst")})
}
}
// impl std::convert::From<AllModels> for Box<dyn Model> {
// fn from(m: AllModels) -> Self {
// Box::new(Generator {source_id: String::from("i"), id: String::from("r")})
// }
// }
I get this error when compiling:
❯ cargo r
Compiling serdeissue v0.1.0 (/sandbox/serdeissue)
warning: unused imports: `Deserializer`, `Serializer`, `serialize_trait_object`
--> src/main.rs:1:20
|
1 | use erased_serde::{Deserializer, Serializer, serialize_trait_object};
| ^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error[E0277]: the size for values of type `(dyn Model + 'static)` cannot be known at compilation time
--> src/main.rs:76:6
|
76 | impl Deserialize<'_> for dyn Model {}
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
::: /home/marcin/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/de/mod.rs:530:29
|
530 | pub trait Deserialize<'de>: Sized {
| ----- required by this bound in `Deserialize`
|
= help: the trait `Sized` is not implemented for `(dyn Model + 'static)`
which is something I thought erased_serde could help with, and maybe it does, but I have no clue how to implement it.
Sadly I can't use typetag crate since it doesn't support wasm compilation target which I need. I am not considering using #[serde(serialize_with = "path")] for each enum variant, since it makes my issue #1 much worse than it currently is.
I'm also aware of this question How to implement `serde::Serialize` for a boxed trait object? however the code provided by #dtolnay doesn't compile and I don't know how to fix it
❯ cargo r
Compiling serdeissue v0.1.0 (/sandbox/serdeissue)
error[E0603]: module `export` is private
--> src/main.rs:168:10
|
168 | #[derive(Serialize)]
| ^^^^^^^^^ private module
|
note: the module `export` is defined here
--> /home/marcin/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/lib.rs:275:5
|
275 | use self::__private as export;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0603]: module `export` is private
--> src/main.rs:173:10
|
173 | #[derive(Serialize)]
| ^^^^^^^^^ private module
|
note: the module `export` is defined here
--> /home/marcin/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/lib.rs:275:5
|
275 | use self::__private as export;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0603]: module `export` is private
--> src/main.rs:179:10
|
179 | #[derive(Serialize)]
| ^^^^^^^^^ private module
|
note: the module `export` is defined here
--> /home/marcin/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/lib.rs:275:5
|
275 | use self::__private as export;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0603]: module `export` is private
--> src/main.rs:184:10
|
184 | #[derive(Serialize)]
| ^^^^^^^^^ private module
|
note: the module `export` is defined here
--> /home/marcin/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.123/src/lib.rs:275:5
|
275 | use self::__private as export;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
warning: trait objects without an explicit `dyn` are deprecated
--> src/main.rs:176:22
|
176 | widgets: Vec<Box<WidgetTrait>>,
| ^^^^^^^^^^^ help: use `dyn`: `dyn WidgetTrait`
|
= note: `#[warn(bare_trait_objects)]` on by default
It looks like the feature I'm looking for is waiting to be implemented here: https://github.com/serde-rs/serde/issues/1402
There is also this issue https://github.com/serde-rs/serde/issues/1350 which suggests manual Deserializer implementation Playground. The playground code indicates this could help with my issue #2 "extra iteration", however there's still some repetition involved in code implementation, therefore I'm still looking for a better answer.
Edit: I'm also considering Enum or trait object, can't figure what's the right approach for evaluating whether I need one or the other.
I found a solution which satisfies me:
slick
no code repetition
no dynamic dispatch, no extra iterations
uses enum and serde(tag="...")
type field can be out of order in json/yaml (doesn't have to be first)
type field is included when serializing back to json/yaml
The trick was to use enum_dispatch.
use serde::{Serialize, Deserialize};
use enum_dispatch::enum_dispatch;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Generator {
source_id: String,
id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Gate {
source_id: String,
id: String,
}
#[enum_dispatch]
trait Model {fn time_advance(self, a:i32,b:i32) -> i32;}
impl Model for Generator { fn time_advance(self,a:i32,b:i32) -> i32 {a+b} }
impl Model for Gate { fn time_advance(self,a:i32,b:i32) -> i32 {a+b} }
#[enum_dispatch(Model)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
enum ModelTypes {
Generator(Generator),
Gate(Gate),
}
fn main() {
let s = r#"
- source_id: "generator-01"
id: "connector-01"
type: "Generator"
- source_id: "geneiator-01"
type: "Gate"
id: "connector-01"
"#;
let data: Vec<ModelTypes> = serde_yaml::from_str(s).unwrap();
println!("{:?}", serde_yaml::to_string(&data));
for d in data {
println!("{:?}", d.time_advance(4, 2));
}
}
[package]
name = "enum_unpack"
version = "0.1.0"
authors = ["---"]
edition = "2018"
[dependencies]
serde = { version = "1.0.124", features = ["derive"] }
serde_yaml = "0.8.1"
enum_dispatch = "0.3.5"
output:
> cargo run
Compiling enum_unpack v0.1.0 (.../enum_unpack)
Finished dev [unoptimized + debuginfo] target(s) in 0.66s
Running `target/debug/enum_unpack`
Ok("---\n- type: Generator\n source_id: generator-01\n id: connector-01\n- type: Gate\n source_id: geneiator-01\n id: connector-01\n")
6
6

Values does not live long enough in constructor and setter in OOP Rust

I have the following code:
//! # Messages
/// Represents a simple text message.
pub struct SimpleMessage<'a> {
pub user: &'a str,
pub content: &'a str,
}
impl<'a> SimpleMessage<'a> {
/// Creates a new SimpleMessage.
fn new_msg(u: &'a str, c: &'a str) -> SimpleMessage<'a> {
SimpleMessage { user: u,
content: &c.to_string(), }
}
/// Sets a User in a Message.
pub fn set_user(&mut self, u: User<'a>){
self.user = &u;
}
}
But $ cargo run returns:
error[E0597]: borrowed value does not live long enough
--> src/messages.rs:34:35
|
34 | content: &c.to_string(), }
| ^^^^^^^^^^^^^ temporary value does not live long enough
35 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 28:1...
|
28 | impl<'a> SimpleMessage<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0597]: `u` does not live long enough
|
54 | self.user = &u;
| ^ borrowed value does not live long enough
55 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 28:1...
--> src/messages.rs:28:1
|
28 | impl<'a> SimpleMessage<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
I've tried changing the borrowing format of the variables at the function signature and it's contents with no success, it doesn't seem as a borrowing issue, but I don't really understand it, as the lifetime <'a> defined at pub struct SimpleMessage<'a> clearly designates the longest lifetime to it's components, and the impl<'a> SimpleMessage<'a> uses the same lifetime.
What am I missing?
Similar Question:
“borrowed value does not live long enough” when using the builder pattern
Doesn't really help resolve this issue.
str.to_string() will create an owned String, which will not live longer than the new_msg method, so you will not be able to pass a slice of it anywhere. Instead, just use the &str argument, since it is valid for the lifetime 'a, which is what you need.
/// Creates a new SimpleMessage.
fn new_msg(u: &'a User, c: &'a str) -> SimpleMessage<'a> {
SimpleMessage { user: u, content: c, }
}
The other method also has a problem. You are trying to give an owned User, but the SimpleMessage struct requires a reference. It should look like this:
/// Sets a User in a Message.
pub fn set_user(&mut self, u: &'a User<'a>){
self.user = u;
}
You don't need to_string if you want to store a reference to a string. Also, set_user must also take a reference, not a value (because there's no field in the struct to store it):
pub struct User<'a> {
pub data: &'a u8,
}
/// Represents a simple text message.
pub struct SimpleMessage<'a> {
pub user: &'a User<'a>,
pub content: &'a str,
}
impl<'a> SimpleMessage<'a> {
fn new_msg(user: &'a User, content: &'a str) -> SimpleMessage<'a> {
SimpleMessage { user, content }
}
pub fn set_user(&mut self, user: &'a User<'a>) {
self.user = user;
}
}
fn main() {
let data1 = 1;
let data2 = 2;
let user1 = User { data: &data1 };
let user2 = User { data: &data2 };
let mut msg = SimpleMessage::new_msg(&user1, "test");
msg.set_user(&user2);
}
Playground
If you want to save strings that are created at runtime (for example, with format!() call) you may want to store a String:
pub struct SimpleMessage<'a> {
pub user: &'a User<'a>,
pub content: String,
}
. . .
fn new_msg(user: &'a User, content: String) -> SimpleMessage<'a> {
SimpleMessage { user, content }
}
. . .
let mut msg = SimpleMessage::new_msg(&user1, format!("created at {}", "runtime"));
Playground

How can we write a generic function for checking Serde serialization and deserialization?

In a project where custom Serde (1.0) serialization and deserialization methods are involved, I have relied on this test routine to check whether serializing an object and back would yield an equivalent object.
// let o: T = ...;
let buf: Vec<u8> = to_vec(&o).unwrap();
let o2: T = from_slice(&buf).unwrap();
assert_eq!(o, o2);
Doing this inline works pretty well. My next step towards reusability was to make a function check_serde for this purpose.
pub fn check_serde<T>(o: T)
where
T: Debug + PartialEq<T> + Serialize + DeserializeOwned,
{
let buf: Vec<u8> = to_vec(&o).unwrap();
let o2: T = from_slice(&buf).unwrap();
assert_eq!(o, o2);
}
This works well for owning types, but not for types with lifetime bounds (Playground):
check_serde(5);
check_serde(vec![1, 2, 5]);
check_serde("five".to_string());
check_serde("wait"); // [E0279]
The error:
error[E0279]: the requirement `for<'de> 'de : ` is not satisfied (`expected bound lifetime parameter 'de, found concrete lifetime`)
--> src/main.rs:24:5
|
24 | check_serde("wait"); // [E0277]
| ^^^^^^^^^^^
|
= note: required because of the requirements on the impl of `for<'de> serde::Deserialize<'de>` for `&str`
= note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `&str`
= note: required by `check_serde`
As I wish to make the function work with these cases (including structs with string slices), I attempted a new version with an explicit object deserialization lifetime:
pub fn check_serde<'a, T>(o: &'a T)
where
T: Debug + PartialEq<T> + Serialize + Deserialize<'a>,
{
let buf: Vec<u8> = to_vec(o).unwrap();
let o2: T = from_slice(&buf).unwrap();
assert_eq!(o, &o2);
}
check_serde(&5);
check_serde(&vec![1, 2, 5]);
check_serde(&"five".to_string());
check_serde(&"wait"); // [E0405]
This implementation leads to another issue, and it won't compile (Playground).
error[E0597]: `buf` does not live long enough
--> src/main.rs:14:29
|
14 | let o2: T = from_slice(&buf).unwrap();
| ^^^ does not live long enough
15 | assert_eq!(o, &o2);
16 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 10:1...
--> src/main.rs:10:1
|
10 | / pub fn check_serde<'a, T>(o: &'a T)
11 | | where T: Debug + PartialEq<T> + Serialize + Deserialize<'a>
12 | | {
13 | | let buf: Vec<u8> = to_vec(o).unwrap();
14 | | let o2: T = from_slice(&buf).unwrap();
15 | | assert_eq!(o, &o2);
16 | | }
| |_^
I have already expected this one: this version implies that the serialized content (and so the deserialized object) lives as long as the input object, which is not true. The buffer is only meant to live as long as the function's scope.
My third attempt seeks to build owned versions of the original input, thus evading the issue of having a deserialized object with different lifetime boundaries. The ToOwned trait appears to suit this use case.
pub fn check_serde<'a, T: ?Sized>(o: &'a T)
where
T: Debug + ToOwned + PartialEq<<T as ToOwned>::Owned> + Serialize,
<T as ToOwned>::Owned: Debug + DeserializeOwned,
{
let buf: Vec<u8> = to_vec(&o).unwrap();
let o2: T::Owned = from_slice(&buf).unwrap();
assert_eq!(o, &o2);
}
This makes the function work for plain string slices now, but not for composite objects containing them (Playground):
check_serde(&5);
check_serde(&vec![1, 2, 5]);
check_serde(&"five".to_string());
check_serde("wait");
check_serde(&("There's more!", 36)); // [E0279]
Again, we stumble upon the same error kind as the first version:
error[E0279]: the requirement `for<'de> 'de : ` is not satisfied (`expected bound lifetime parameter 'de, found concrete lifetime`)
--> src/main.rs:25:5
|
25 | check_serde(&("There's more!", 36)); // [E0279]
| ^^^^^^^^^^^
|
= note: required because of the requirements on the impl of `for<'de> serde::Deserialize<'de>` for `&str`
= note: required because of the requirements on the impl of `for<'de> serde::Deserialize<'de>` for `(&str, {integer})`
= note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `(&str, {integer})`
= note: required by `check_serde`
Granted, I'm at a loss. How can we build a generic function that, using Serde, serializes an object and deserializes it back into a new object? In particular, can this function be made in Rust (stable or nightly), and if so, what adjustments to my implementation are missing?
Unfortunately, what you need is a feature that is not yet implemented in Rust: generic associated types.
Let's look at a different variant of check_serde:
pub fn check_serde<T>(o: T)
where
for<'a> T: Debug + PartialEq<T> + Serialize + Deserialize<'a>,
{
let buf: Vec<u8> = to_vec(&o).unwrap();
let o2: T = from_slice(&buf).unwrap();
assert_eq!(o, o2);
}
fn main() {
check_serde("wait"); // [E0279]
}
The problem here is that o2 cannot be of type T: o2 refers to buf, which is a local variable, but type parameters cannot be inferred to types constrained by a lifetime that is restricted to the function's body. We'd like for T to be something like &str without a specific lifetime attached to it.
With generic associated types, this could be solved with something like this (obviously I can't test it, since it's not implemented yet):
trait SerdeFamily {
type Member<'a>: Debug + for<'b> PartialEq<Self::Member<'b>> + Serialize + Deserialize<'a>;
}
struct I32Family;
struct StrFamily;
impl SerdeFamily for I32Family {
type Member<'a> = i32; // ignoring a parameter is allowed
}
impl SerdeFamily for StrFamily {
type Member<'a> = &'a str;
}
pub fn check_serde<'a, Family>(o: Family::Member<'a>)
where
Family: SerdeFamily,
{
let buf: Vec<u8> = to_vec(&o).unwrap();
// `o2` is of type `Family::Member<'b>`
// with a lifetime 'b different from 'a
let o2: Family::Member = from_slice(&buf).unwrap();
assert_eq!(o, o2);
}
fn main() {
check_serde::<I32Family>(5);
check_serde::<StrFamily>("wait");
}
The answer from Francis Gagné has shown that we cannot do this efficiently without generic associated types. Establishing deep ownership of the deserialized object is a possible work-around which I describe here.
The third attempt is very close to a flexible solution, but it falls short due to how std::borrow::ToOwned works. The trait is not suitable for retrieving a deeply owned version of an object. Attempting to use the implementation of ToOwned for &str, for instance, gives you another string slice.
let a: &str = "hello";
let b: String = (&a).to_owned(); // expected String, got &str
Likewise, the Owned type for a struct containing string slices cannot be a struct containing Strings. In code:
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Foo<'a>(&str, i32);
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct FooOwned(String, i32);
We cannot impl ToOwned for Foo to provide FooOwned because:
If we derive Clone, the implementation of ToOwned for T: Clone is only applicable to Owned = Self.
Even with a custom implementation of ToOwned, the trait requires that the owned type can be borrowed into the original type (due to the constraint Owned: Borrow<Self>). That is, we are supposed to be able to retrieve a &Foo(&str, i32) out of a FooOwned, but their internal structure is different, and so this is not attainable.
This means that, in order to follow the third approach, we need a different trait. Let's have a new trait ToDeeplyOwned which turns an object into a fully owned one, with no slices or references involved.
pub trait ToDeeplyOwned {
type Owned;
fn to_deeply_owned(&self) -> Self::Owned;
}
The intent here is to produce a deep copy out of anything. There doesn't seem to be an easy catch-all implementation, but some tricks are possible. First, we can implement it to all reference types where T: ToDeeplyOwned.
impl<'a, T: ?Sized + ToDeeplyOwned> ToDeeplyOwned for &'a T {
type Owned = T::Owned;
fn to_deeply_owned(&self) -> Self::Owned {
(**self).to_deeply_owned()
}
}
At this point we would have to selectively implement it to non-reference types where we know it's ok. I wrote a macro for making this process less verbose, which uses to_owned() internally.
macro_rules! impl_deeply_owned {
($t: ty, $t2: ty) => { // turn $t into $t2
impl ToDeeplyOwned for $t {
type Owned = $t2;
fn to_deeply_owned(&self) -> Self::Owned {
self.to_owned()
}
}
};
($t: ty) => { // turn $t into itself, self-contained type
impl ToDeeplyOwned for $t {
type Owned = $t;
fn to_deeply_owned(&self) -> Self::Owned {
self.to_owned()
}
}
};
}
For the examples in the question to work, we need at least these:
impl_deeply_owned!(i32);
impl_deeply_owned!(String);
impl_deeply_owned!(Vec<i32>);
impl_deeply_owned!(str, String);
Once we implement the necessary traits on Foo/FooOwned and adapt serde_check to use the new trait, the code now compiles and runs successfully (Playground):
#[derive(Debug, PartialEq, Serialize)]
struct Foo<'a>(&'a str, i32);
#[derive(Debug, PartialEq, Clone, Deserialize)]
struct FooOwned(String, i32);
impl<'a> ToDeeplyOwned for Foo<'a> {
type Owned = FooOwned;
fn to_deeply_owned(&self) -> FooOwned {
FooOwned(self.0.to_string(), self.1)
}
}
impl<'a> PartialEq<FooOwned> for Foo<'a> {
fn eq(&self, o: &FooOwned) -> bool {
self.0 == o.0 && self.1 == o.1
}
}
pub fn check_serde<'a, T: ?Sized>(o: &'a T)
where
T: Debug + ToDeeplyOwned + PartialEq<<T as ToDeeplyOwned>::Owned> + Serialize,
<T as ToDeeplyOwned>::Owned: Debug + DeserializeOwned,
{
let buf: Vec<u8> = to_vec(&o).unwrap();
let o2: T::Owned = from_slice(&buf).unwrap();
assert_eq!(o, &o2);
}
// all of these are ok
check_serde(&5);
check_serde(&vec![1, 2, 5]);
check_serde(&"five".to_string());
check_serde("wait");
check_serde(&"wait");
check_serde(&Foo("There's more!", 36));
Update (04.09.2021):
The latest nightly has some fixes around GATs which basically allows the original example:
#![feature(generic_associated_types)]
use serde::{Deserialize, Serialize};
use serde_json::{from_slice, to_vec};
use std::fmt::Debug;
trait SerdeFamily {
type Member<'a>:
Debug +
for<'b> PartialEq<Self::Member<'b>> +
Serialize +
Deserialize<'a>;
}
struct I32Family;
struct StrFamily;
impl SerdeFamily for I32Family {
type Member<'a> = i32;
}
impl SerdeFamily for StrFamily {
type Member<'a> = &'a str;
}
fn check_serde<F: SerdeFamily>(o: F::Member<'_>) {
let buf: Vec<u8> = to_vec(&o).unwrap();
let o2: F::Member<'_> = from_slice(&buf).unwrap();
assert_eq!(o, o2);
}
fn main() {
check_serde::<I32Family>(5);
check_serde::<StrFamily>("wait");
}
The example above compiles now: playground.
As of now it's possible to implement this on rust nightly (with an explicit variance workaround):
#![feature(generic_associated_types)]
use serde::{Deserialize, Serialize};
use serde_json::{from_slice, to_vec};
use std::fmt::Debug;
trait SerdeFamily {
type Member<'a>: Debug + PartialEq + Serialize + Deserialize<'a>;
// https://internals.rust-lang.org/t/variance-of-lifetime-arguments-in-gats/14769/19
fn upcast_gat<'short, 'long: 'short>(long: Self::Member<'long>) -> Self::Member<'short>;
}
struct I32Family;
struct StrFamily;
impl SerdeFamily for I32Family {
type Member<'a> = i32; // we can ignore parameters
fn upcast_gat<'short, 'long: 'short>(long: Self::Member<'long>) -> Self::Member<'short> {
long
}
}
impl SerdeFamily for StrFamily {
type Member<'a> = &'a str;
fn upcast_gat<'short, 'long: 'short>(long: Self::Member<'long>) -> Self::Member<'short> {
long
}
}
fn check_serde<F: SerdeFamily>(o: F::Member<'_>) {
let buf: Vec<u8> = to_vec(&o).unwrap();
let o2: F::Member<'_> = from_slice(&buf).unwrap();
assert_eq!(F::upcast_gat(o), o2);
}
fn main() {
check_serde::<I32Family>(5);
check_serde::<StrFamily>("wait");
}
Playground
Simple (but a little awkward) solution: Provide buf from outside of the function.
pub fn check_serde<'a, T>(o: &'a T, buf: &'a mut Vec<u8>)
where
T: Debug + PartialEq<T> + Serialize + Deserialize<'a>,
{
*buf = to_vec(o).unwrap();
let o2: T = from_slice(buf).unwrap();
assert_eq!(o, &o2);
}
buf can be reused with Cursor
pub fn check_serde_with_cursor<'a, T>(o: &'a T, buf: &'a mut Vec<u8>)
where
T: Debug + PartialEq<T> + Serialize + Deserialize<'a>,
{
buf.clear();
let mut cursor = Cursor::new(buf);
to_writer(&mut cursor, o).unwrap();
let o2: T = from_slice(cursor.into_inner()).unwrap();
assert_eq!(o, &o2);
}

Wrong number of lifetime parameters when using a modified `Chars` iterator

I want to implement the IntoIterator trait for a struct containing a String. The iterator is based on the chars() iterator, is supposed to count the '1' chars and accumulate the result. This is a simplified version of what I got so far:
use std::iter::Map;
use std::str::Chars;
fn main() {
let str_struct = StringStruct { system_string: String::from("1101") };
for a in str_struct {
println!("{}", a);
}
}
struct StringStruct {
system_string: String
}
impl IntoIterator for StringStruct {
type Item = u32;
type IntoIter = Map<Chars, Fn(char) -> u32>;
fn into_iter(self) -> Self::IntoIter {
let count = 0;
return self.system_string.chars().map(|c| match c {
Some('1') => {
count += 1;
return Some(count);
},
Some(chr) => return Some(count),
None => return None
});
}
}
Expected output: 1, 2, 2, 3
This fails with:
error[E0107]: wrong number of lifetime parameters: expected 1, found 0
--> src/main.rs:17:25
|
17 | type IntoIter = Map<Chars, Fn(char) -> u32>;
| ^^^^^ expected 1 lifetime parameter
The chars iterator should have the same lifetime as the StringStruct::system_string, but I have no idea how to express this or if this approach is viable at all.
To answer the question you asked, I'd recommend to impl IntoIterator for &StringStruct (a reference to a StringStruct instead of the struct directly). The code would look like this:
impl<'a> IntoIterator for &'a StringStruct {
type Item = u32;
type IntoIter = Map<Chars<'a>, Fn(char) -> u32>;
// ...
}
However, you will notice many more errors that have a different origin afterwards. The next error that pops up is that Fn(char) -> u32 does not have a constant size at compile time.
The problem is that you try to name the type of your closure by writing Fn(char) -> u32. But this is not the type of your closure, but merely a trait which is implemented by the closure. The type of a closure can't be named (sometimes called "Voldemort type").
This means that, right now, you can't specify the type of a Map<_, _> object. This is a known issue; the recently accepted impl Trait-RFC might offer a workaround for cases like this. But right now, it's not possible, sorry.
So how to solve it then? You need to create your own type that implements Iterator and use it instead of Map<_, _>. Note that you can still use the Chars iterator. Here is the full solution:
struct StringStructIter<'a> {
chars: Chars<'a>,
count: u32,
}
impl<'a> Iterator for StringStructIter<'a> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
self.chars.next().map(|c| {
if c == '1' {
self.count += 1;
}
self.count
})
}
}
impl<'a> IntoIterator for &'a StringStruct {
type Item = u32;
type IntoIter = StringStructIter<'a>;
fn into_iter(self) -> Self::IntoIter {
StringStructIter {
chars: self.system_string.chars(),
count: 0,
}
}
}
fn main() {
let str_struct = StringStruct { system_string: String::from("1101") };
for a in &str_struct {
println!("{}", a);
}
}
And just a small note: an explicit return when not necessary is considered bad style in Rust. Better stick to rule and write idiomatic code by removing return whenever possible ;-)