New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add faster serde serialization, sacrificing invariant-safety for speed in release mode; test that it roudtrips #252
Closed
+293
−16
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ad8d172
Add faster serde serialization, sacrificing invariant-safety for spee…
Manishearth 7f31957
Add an external serde code generator to cut down on compile time
erickt 1c00b56
Use serialize_efficient and deserialize_efficient instead of hijackin…
Manishearth 70a816a
Rename to serialize_unsafe
Manishearth File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
Loading status checks…
Add faster serde serialization, sacrificing invariant-safety for spee…
…d in release mode; test that it roudtrips
- Loading branch information
commit ad8d172b7e220a752003d259d507ba7eb9d9d23d
| @@ -312,17 +312,23 @@ impl Url { | ||
| self.serialization | ||
| } | ||
|
|
||
| #[doc(hidden)] | ||
| pub fn assert_invariants(&self) { | ||
| self.assert_invariants_result().unwrap() | ||
| } | ||
|
|
||
| /// For internal testing, not part of the public API. | ||
| /// | ||
| /// Methods of the `Url` struct assume a number of invariants. | ||
| /// This checks each of these invariants and panic if one is not met. | ||
| /// This is for testing rust-url itself. | ||
| #[doc(hidden)] | ||
| pub fn assert_invariants(&self) { | ||
| pub fn assert_invariants_result(&self) -> Result<(), String> { | ||
| macro_rules! assert { | ||
| ($x: expr) => { | ||
| if !$x { | ||
| panic!("!( {} ) for URL {:?}", stringify!($x), self.serialization) | ||
| return Err(format!("!( {} ) for URL {:?}", | ||
| stringify!($x), self.serialization)) | ||
| } | ||
| } | ||
| } | ||
| @@ -333,8 +339,9 @@ impl Url { | ||
| let a = $a; | ||
| let b = $b; | ||
| if a != b { | ||
| panic!("{:?} != {:?} ({} != {}) for URL {:?}", | ||
| a, b, stringify!($a), stringify!($b), self.serialization) | ||
| return Err(format!("{:?} != {:?} ({} != {}) for URL {:?}", | ||
| a, b, stringify!($a), stringify!($b), | ||
| self.serialization)) | ||
| } | ||
| } | ||
| } | ||
| @@ -415,6 +422,7 @@ impl Url { | ||
| assert_eq!(self.path_start, other.path_start); | ||
| assert_eq!(self.query_start, other.query_start); | ||
| assert_eq!(self.fragment_start, other.fragment_start); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Return the origin of this URL (https://url.spec.whatwg.org/#origin) | ||
| @@ -1506,20 +1514,51 @@ impl rustc_serialize::Decodable for Url { | ||
| /// | ||
| /// This implementation is only available if the `serde` Cargo feature is enabled. | ||
| #[cfg(feature="serde")] | ||
| #[deny(unused)] | ||
| impl serde::Serialize for Url { | ||
| fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: serde::Serializer { | ||
| format!("{}", self).serialize(serializer) | ||
| // Destructuring first lets us ensure that adding or removing fields forces this method | ||
| // to be updated | ||
| let Url { ref serialization, ref scheme_end, | ||
| ref username_end, ref host_start, | ||
| ref host_end, ref host, ref port, | ||
| ref path_start, ref query_start, | ||
| ref fragment_start} = *self; | ||
| (serialization, scheme_end, username_end, | ||
| host_start, host_end, host, port, path_start, | ||
| query_start, fragment_start).serialize(serializer) | ||
| } | ||
| } | ||
|
|
||
| /// Deserializes this URL from a `serde` stream. | ||
| /// | ||
| /// This implementation is only available if the `serde` Cargo feature is enabled. | ||
| #[cfg(feature="serde")] | ||
| #[deny(unused)] | ||
| impl serde::Deserialize for Url { | ||
| fn deserialize<D>(deserializer: &mut D) -> Result<Url, D::Error> where D: serde::Deserializer { | ||
| let string_representation: String = try!(serde::Deserialize::deserialize(deserializer)); | ||
| Ok(Url::parse(&string_representation).unwrap()) | ||
| use serde::{Deserialize, Error}; | ||
| let (serialization, scheme_end, username_end, | ||
| host_start, host_end, host, port, path_start, | ||
| query_start, fragment_start) = Deserialize::deserialize(deserializer)?; | ||
| let url = Url { | ||
| serialization: serialization, | ||
| scheme_end: scheme_end, | ||
| username_end: username_end, | ||
| host_start: host_start, | ||
| host_end: host_end, | ||
| host: host, | ||
| port: port, | ||
| path_start: path_start, | ||
| query_start: query_start, | ||
| fragment_start: fragment_start | ||
| }; | ||
| #[cfg(debug_assertions)] { | ||
nox
Member
|
||
| if let Err(s) = url.assert_invariants_result() { | ||
| return Err(Error::invalid_value(&s)) | ||
| } | ||
| } | ||
| Ok(url) | ||
| } | ||
| } | ||
|
|
||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
would be better as (2u8, addr).serialize(serializer)