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
Use serde_bytes crate #2598
Use serde_bytes crate #2598
Changes from all commits
File filter...
Jump to…
Use serde_bytes crate
This speeds up serializing and deserializing of binary data.
- Loading branch information
Some generated files are not rendered by default. Learn more.
| @@ -2,6 +2,8 @@ | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| extern crate serde_bytes; | ||
|
|
||
| use font::{FontInstanceKey, FontKey, FontTemplate}; | ||
| use std::sync::Arc; | ||
| use {DevicePoint, DeviceUintRect, IdNamespace, TileOffset, TileSize}; | ||
| @@ -110,11 +112,26 @@ impl ImageDescriptor { | ||
|
|
||
| #[derive(Clone, Debug, Serialize, Deserialize)] | ||
| pub enum ImageData { | ||
| Raw(Arc<Vec<u8>>), | ||
| Blob(BlobImageData), | ||
| Raw(#[serde(with = "serde_image_data_raw")] Arc<Vec<u8>>), | ||
| Blob(#[serde(with = "serde_bytes")] BlobImageData), | ||
| External(ExternalImageData), | ||
| } | ||
|
|
||
| mod serde_image_data_raw { | ||
dtolnay
|
||
| extern crate serde_bytes; | ||
|
|
||
| use std::sync::Arc; | ||
| use serde::{Deserializer, Serializer}; | ||
|
|
||
| pub fn serialize<'a, S: Serializer>(bytes: &'a Arc<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error> { | ||
| serde_bytes::serialize(bytes.as_slice(), serializer) | ||
| } | ||
|
|
||
| pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Arc<Vec<u8>>, D::Error> { | ||
| serde_bytes::deserialize(deserializer).map(Arc::new) | ||
| } | ||
| } | ||
|
|
||
| impl ImageData { | ||
| pub fn new(bytes: Vec<u8>) -> Self { | ||
| ImageData::Raw(Arc::new(bytes)) | ||
This is necessary because we are dealing with an
Arc. However, maybe it would be better to patchserde_bytesto support this?