Remove mem::transmute (closes #39)
This commit is contained in:
parent
3e08dee3f4
commit
20d7db6d73
@ -44,32 +44,18 @@ pub fn serialize(
|
|||||||
if let Some(ref d) = field_attrs.default {
|
if let Some(ref d) = field_attrs.default {
|
||||||
let default_function = Ident::new(&d, field.span());
|
let default_function = Ident::new(&d, field.span());
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
let content = self.#label.to_string();
|
||||||
let struct_start_event =
|
let struct_start_event =
|
||||||
if self.#label != #default_function() {
|
if self.#label != #default_function() {
|
||||||
struct_start_event.attr(#label_name, &*{
|
struct_start_event.attr(#label_name, &content)
|
||||||
use std::mem;
|
|
||||||
unsafe {
|
|
||||||
let content = format!("{}", self.#label);
|
|
||||||
let ret : &'static str = mem::transmute(&content as &str);
|
|
||||||
mem::forget(content);
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
struct_start_event
|
struct_start_event
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
let struct_start_event = struct_start_event.attr(#label_name, &*{
|
let content = self.#label.to_string();
|
||||||
use std::mem;
|
let struct_start_event = struct_start_event.attr(#label_name, &content);
|
||||||
unsafe {
|
|
||||||
let content = format!("{}", self.#label);
|
|
||||||
let ret : &'static str = mem::transmute(&content as &str);
|
|
||||||
mem::forget(content);
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,18 +100,11 @@ pub fn serialize(
|
|||||||
if let Some(ref d) = field_attrs.default {
|
if let Some(ref d) = field_attrs.default {
|
||||||
let default_function = Ident::new(&d, field.span());
|
let default_function = Ident::new(&d, field.span());
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
let content = self.#label.map_or_else(|| String::new(), |v| v.to_string());
|
||||||
let struct_start_event =
|
let struct_start_event =
|
||||||
if self.#label != #default_function() {
|
if self.#label != #default_function() {
|
||||||
if let Some(ref value) = self.#label {
|
if let Some(ref value) = self.#label {
|
||||||
struct_start_event.attr(#label_name, &*{
|
struct_start_event.attr(#label_name, &content)
|
||||||
use std::mem;
|
|
||||||
unsafe {
|
|
||||||
let content = format!("{}", value);
|
|
||||||
let ret : &'static str = mem::transmute(&content as &str);
|
|
||||||
mem::forget(content);
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
struct_start_event
|
struct_start_event
|
||||||
}
|
}
|
||||||
@ -135,17 +114,10 @@ pub fn serialize(
|
|||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
let content = self.#label.map_or_else(|| String::new(), |v| v.to_string());
|
||||||
let struct_start_event =
|
let struct_start_event =
|
||||||
if let Some(ref value) = self.#label {
|
if let Some(ref value) = self.#label {
|
||||||
struct_start_event.attr(#label_name, &*{
|
struct_start_event.attr(#label_name, &content)
|
||||||
use std::mem;
|
|
||||||
unsafe {
|
|
||||||
let content = format!("{}", value);
|
|
||||||
let ret : &'static str = mem::transmute(&content as &str);
|
|
||||||
mem::forget(content);
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
struct_start_event
|
struct_start_event
|
||||||
};
|
};
|
||||||
@ -180,21 +152,12 @@ pub fn serialize(
|
|||||||
if let Some(ref d) = field_attrs.default {
|
if let Some(ref d) = field_attrs.default {
|
||||||
let default_function = Ident::new(&d, field.span());
|
let default_function = Ident::new(&d, field.span());
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
let content = self.#label
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| Ok(String::new()), |v| yaserde::ser::to_string_content(v))?;
|
||||||
let struct_start_event = if let Some(ref value) = self.#label {
|
let struct_start_event = if let Some(ref value) = self.#label {
|
||||||
if *value != #default_function() {
|
if *value != #default_function() {
|
||||||
struct_start_event.attr(#label_name, &*{
|
struct_start_event.attr(#label_name, &content)
|
||||||
use std::mem;
|
|
||||||
match yaserde::ser::to_string_content(value) {
|
|
||||||
Ok(value) => {
|
|
||||||
unsafe {
|
|
||||||
let ret : &'static str = mem::transmute(&value as &str);
|
|
||||||
mem::forget(value);
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(msg) => return Err("Unable to serialize content".to_owned()),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
struct_start_event
|
struct_start_event
|
||||||
}
|
}
|
||||||
@ -204,20 +167,11 @@ pub fn serialize(
|
|||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
let content = self.#label
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(|| Ok(String::new()), |v| yaserde::ser::to_string_content(v))?;
|
||||||
let struct_start_event = if let Some(ref value) = self.#label {
|
let struct_start_event = if let Some(ref value) = self.#label {
|
||||||
struct_start_event.attr(#label_name, &*{
|
struct_start_event.attr(#label_name, &content)
|
||||||
use std::mem;
|
|
||||||
match yaserde::ser::to_string_content(value) {
|
|
||||||
Ok(value) => {
|
|
||||||
unsafe {
|
|
||||||
let ret : &'static str = mem::transmute(&value as &str);
|
|
||||||
mem::forget(value);
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(msg) => return Err("Unable to serialize content".to_owned()),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
struct_start_event
|
struct_start_event
|
||||||
};
|
};
|
||||||
@ -230,40 +184,18 @@ pub fn serialize(
|
|||||||
if let Some(ref d) = field_attrs.default {
|
if let Some(ref d) = field_attrs.default {
|
||||||
let default_function = Ident::new(&d, field.span());
|
let default_function = Ident::new(&d, field.span());
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
let content = yaserde::ser::to_string_content(&self.#label)?;
|
||||||
let struct_start_event =
|
let struct_start_event =
|
||||||
if self.#label != #default_function() {
|
if self.#label != #default_function() {
|
||||||
struct_start_event.attr(#label_name, &*{
|
struct_start_event.attr(#label_name, &content)
|
||||||
use std::mem;
|
|
||||||
match yaserde::ser::to_string_content(&self.#label) {
|
|
||||||
Ok(value) => {
|
|
||||||
unsafe {
|
|
||||||
let ret : &'static str = mem::transmute(&value as &str);
|
|
||||||
mem::forget(value);
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(msg) => return Err("Unable to serialize content".to_owned()),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
struct_start_event
|
struct_start_event
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
let struct_start_event = struct_start_event.attr(#label_name, &*{
|
let content = yaserde::ser::to_string_content(&self.#label)?;
|
||||||
use std::mem;
|
let struct_start_event = struct_start_event.attr(#label_name, &content);
|
||||||
match yaserde::ser::to_string_content(&self.#label) {
|
|
||||||
Ok(value) => {
|
|
||||||
unsafe {
|
|
||||||
let ret : &'static str = mem::transmute(&value as &str);
|
|
||||||
mem::forget(value);
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(msg) => return Err("Unable to serialize content".to_owned()),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user