1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
use byteorder::{WriteBytesExt, BE}; use serde::{self, Serialize}; use std::error::Error as StdError; use std::fmt; use std::io::{self, Write}; use std::mem::transmute; use std::{self, i16, i32, i64, i8}; /// A serializer for a byte format that preserves lexicographic sort order. /// /// The byte format is designed with a few goals: /// /// * Order must be preserved /// * Serialized representations should be as compact as possible /// * Type information is *not* serialized with values /// /// #### Supported Data Types /// /// ##### Unsigned Integers /// /// `u8`, `u16`, `u32`, and `u64` are serialized into 1, 2, 4, and 8 bytes of output, respectively. /// Order is preserved by encoding the bytes in big-endian (most-significant bytes first) format. /// `usize` is always serialized as if it were `u64`. /// /// The `Serializer` also supports variable-length serialization of unsigned integers via the /// `serialize_var_u64` method. Smaller magnitude values (closer to 0) will encode into fewer /// bytes. /// /// ##### Signed Integers /// /// `i8`, `i16`, `i32`, and `i64` are encoded into 1, 2, 4, and 8 bytes of output, respectively. /// Order is preserved by taking the bitwise complement of the value, and encoding the resulting /// bytes in big-endian format. `isize` is always serialized as if it were `i64`. /// /// The `Serializer` also supports variable-length serialization of signed integers via the /// `serialize_var_i64` method. Smaller magnitude values (closer to 0) will encode into fewer /// bytes. /// /// ##### Floating Point Numbers /// /// `f32` and `f64` are serialized into 4 and 8 bytes of output, respectively. Order is preserved /// by encoding the value, or the bitwise complement of the value if negative, into bytes in /// big-endian format. `NAN` values will sort after all other values. In general, it is unwise to /// use IEEE 754 floating point values in keys, because rounding errors are pervasive. It is /// typically hard or impossible to use an approximate 'epsilon' approach when using keys for /// lookup. /// /// ##### Characters /// /// Characters are serialized into between 1 and 4 bytes of output. The resulting length is /// equivalent to the result of `char::len_utf8`. /// /// ##### Booleans /// /// Booleans are serialized into a single byte of output. `false` values will sort before `true` /// values. /// /// ##### Options /// /// An optional wrapper type adds a 1 byte overhead to the wrapped data type. `None` values will /// sort before `Some` values. /// /// ##### Structs, Tuples and Fixed-Size Arrays /// /// Structs and tuples are serialized by serializing their consituent fields in order with no /// prefix, suffix, or padding bytes. /// /// ##### Enums /// /// Enums are encoded with a `u32` variant index tag, plus the consituent fields in the case of an /// enum-struct. /// /// ##### Sequences, Strings and Maps /// /// Sequences are ordered from the most significant to the least. Strings are serialized into their /// natural UTF8 representation. /// /// The ordering of sequential elements follows the `Ord` implementation of `slice`, that is, from /// left to write when viewing a `Vec` printed via the `{:?}` formatter. /// /// The caveat with these types is that their length must be known before deserialization. This is /// because the length is *not* serialized prior to the elements in order to preserve ordering and /// there is no trivial way to tokenise between sequential elements that 1. does not corrupt /// ordering and 2. may not confuse tokenisation with following elements of a different type during /// tuple or struct deserialization. Thus, when deserializing sequences, strings and maps, the /// process will only be considered complete once the inner `reader` produces an EOF character. #[derive(Debug)] pub struct Serializer<W> where W: Write, { writer: W, } /// Errors that might occur while serializing. #[derive(Debug)] pub enum Error { /// Errors that might occur during serialization. /// /// E.g. the `Serialize` impl for `Mutex<T>` might return an error because the mutex is /// poisoned. Message(String), Io(io::Error), } /// Shorthand for `Result<T, bytekey::ser::Error>`. pub type Result<T> = std::result::Result<T, Error>; /// Serialize data into a vector of `u8` bytes. /// /// #### Usage /// /// ``` /// # use bytekey::serialize; /// assert_eq!(vec!(0x00, 0x00, 0x00, 0x2A), serialize(&42u32).unwrap()); /// assert_eq!(vec!(0x66, 0x69, 0x7A, 0x7A, 0x62, 0x75, 0x7A, 0x7A, 0x00), serialize(&"fizzbuzz").unwrap()); /// assert_eq!(vec!(0x2A, 0x66, 0x69, 0x7A, 0x7A, 0x00), serialize(&(42u8, "fizz")).unwrap()); /// ``` pub fn serialize<T>(v: &T) -> Result<Vec<u8>> where T: Serialize, { let mut bytes = vec![]; { let mut buffered = io::BufWriter::new(&mut bytes); serialize_into(&mut buffered, v)?; } Ok(bytes) } /// Serialize data into the given vector of `u8` bytes. /// /// #### Usage /// /// ``` /// # use bytekey::serialize_into; /// let mut bytes = vec![]; /// bytekey::serialize_into(&mut bytes, &5u8).unwrap(); /// assert_eq!(vec![5u8], bytes.clone()); /// bytekey::serialize_into(&mut bytes, &10u8).unwrap(); /// assert_eq!(vec![5u8, 10], bytes.clone()); /// ``` pub fn serialize_into<W, T>(writer: W, value: &T) -> Result<()> where W: Write, T: Serialize, { let mut serializer = Serializer::new(writer); value.serialize(&mut serializer) } impl<W> Serializer<W> where W: Write, { /// Creates a new ordered bytes encoder whose output will be written to the provided writer. pub fn new(writer: W) -> Serializer<W> { Serializer { writer: writer } } /// Encode a `u64` into a variable number of bytes. /// /// The variable-length encoding scheme uses between 1 and 9 bytes depending on the value. /// Smaller magnitude (closer to 0) `u64`s will encode to fewer bytes. /// /// ##### Encoding /// /// The encoding uses the first 4 bits to store the number of trailing bytes, between 0 and 8. /// Subsequent bits are the input value in big-endian format with leading 0 bytes removed. /// /// ##### Encoded Size /// /// <table> /// <tr> /// <th>range</th> /// <th>size (bytes)</th> /// </tr> /// <tr> /// <td>[0, 2<sup>4</sup>)</td> /// <td>1</td> /// </tr> /// <tr> /// <td>[2<sup>4</sup>, 2<sup>12</sup>)</td> /// <td>2</td> /// </tr> /// <tr> /// <td>[2<sup>12</sup>, 2<sup>20</sup>)</td> /// <td>3</td> /// </tr> /// <tr> /// <td>[2<sup>20</sup>, 2<sup>28</sup>)</td> /// <td>4</td> /// </tr> /// <tr> /// <td>[2<sup>28</sup>, 2<sup>36</sup>)</td> /// <td>5</td> /// </tr> /// <tr> /// <td>[2<sup>36</sup>, 2<sup>44</sup>)</td> /// <td>6</td> /// </tr> /// <tr> /// <td>[2<sup>44</sup>, 2<sup>52</sup>)</td> /// <td>7</td> /// </tr> /// <tr> /// <td>[2<sup>52</sup>, 2<sup>60</sup>)</td> /// <td>8</td> /// </tr> /// <tr> /// <td>[2<sup>60</sup>, 2<sup>64</sup>)</td> /// <td>9</td> /// </tr> /// </table> pub fn serialize_var_u64(&mut self, val: u64) -> Result<()> { if val < 1 << 4 { self.writer.write_u8(val as u8) } else if val < 1 << 12 { self.writer.write_u16::<BE>((val as u16) | 1 << 12) } else if val < 1 << 20 { self.writer.write_u8(((val >> 16) as u8) | 2 << 4)?; self.writer.write_u16::<BE>(val as u16) } else if val < 1 << 28 { self.writer.write_u32::<BE>((val as u32) | 3 << 28) } else if val < 1 << 36 { self.writer.write_u8(((val >> 32) as u8) | 4 << 4)?; self.writer.write_u32::<BE>(val as u32) } else if val < 1 << 44 { self.writer.write_u16::<BE>(((val >> 32) as u16) | 5 << 12)?; self.writer.write_u32::<BE>(val as u32) } else if val < 1 << 52 { self.writer.write_u8(((val >> 48) as u8) | 6 << 4)?; self.writer.write_u16::<BE>((val >> 32) as u16)?; self.writer.write_u32::<BE>(val as u32) } else if val < 1 << 60 { self.writer.write_u64::<BE>((val as u64) | 7 << 60) } else { self.writer.write_u8(8 << 4)?; self.writer.write_u64::<BE>(val) }.map_err(From::from) } /// Encode an `i64` into a variable number of bytes. /// /// The variable-length encoding scheme uses between 1 and 9 bytes depending on the value. /// Smaller magnitude (closer to 0) `i64`s will encode to fewer bytes. /// /// ##### Encoding /// /// The encoding uses the first bit to encode the sign: `0` for negative values and `1` for /// positive values. The following 4 bits store the number of trailing bytes, between 0 and 8. /// Subsequent bits are the absolute value of the input value in big-endian format with leading /// 0 bytes removed. If the original value was negative, than 1 is subtracted from the absolute /// value before encoding. Finally, if the value is negative, all bits except the sign bit are /// flipped (1s become 0s and 0s become 1s). /// /// ##### Encoded Size /// /// <table> /// <tr> /// <th>negative range</th> /// <th>positive range</th> /// <th>size (bytes)</th> /// </tr> /// <tr> /// <td>[-2<sup>3</sup>, 0)</td> /// <td>[0, 2<sup>3</sup>)</td> /// <td>1</td> /// </tr> /// <tr> /// <td>[-2<sup>11</sup>, -2<sup>3</sup>)</td> /// <td>[2<sup>3</sup>, 2<sup>11</sup>)</td> /// <td>2</td> /// </tr> /// <tr> /// <td>[-2<sup>19</sup>, -2<sup>11</sup>)</td> /// <td>[2<sup>11</sup>, 2<sup>19</sup>)</td> /// <td>3</td> /// </tr> /// <tr> /// <td>[-2<sup>27</sup>, -2<sup>19</sup>)</td> /// <td>[2<sup>19</sup>, 2<sup>27</sup>)</td> /// <td>4</td> /// </tr> /// <tr> /// <td>[-2<sup>35</sup>, -2<sup>27</sup>)</td> /// <td>[2<sup>27</sup>, 2<sup>35</sup>)</td> /// <td>5</td> /// </tr> /// <tr> /// <td>[-2<sup>43</sup>, -2<sup>35</sup>)</td> /// <td>[2<sup>35</sup>, 2<sup>43</sup>)</td> /// <td>6</td> /// </tr> /// <tr> /// <td>[-2<sup>51</sup>, -2<sup>43</sup>)</td> /// <td>[2<sup>43</sup>, 2<sup>51</sup>)</td> /// <td>7</td> /// </tr> /// <tr> /// <td>[-2<sup>59</sup>, -2<sup>51</sup>)</td> /// <td>[2<sup>51</sup>, 2<sup>59</sup>)</td> /// <td>8</td> /// </tr> /// <tr> /// <td>[-2<sup>63</sup>, -2<sup>59</sup>)</td> /// <td>[2<sup>59</sup>, 2<sup>63</sup>)</td> /// <td>9</td> /// </tr> /// </table> pub fn serialize_var_i64(&mut self, v: i64) -> Result<()> { // The mask is 0 for positive input and u64::MAX for negative input let mask = (v >> 63) as u64; let val = v.abs() as u64 - (1 & mask); if val < 1 << 3 { let masked = (val | (0x10 << 3)) ^ mask; self.writer.write_u8(masked as u8) } else if val < 1 << 11 { let masked = (val | (0x11 << 11)) ^ mask; self.writer.write_u16::<BE>(masked as u16) } else if val < 1 << 19 { let masked = (val | (0x12 << 19)) ^ mask; self.writer.write_u8((masked >> 16) as u8)?; self.writer.write_u16::<BE>(masked as u16) } else if val < 1 << 27 { let masked = (val | (0x13 << 27)) ^ mask; self.writer.write_u32::<BE>(masked as u32) } else if val < 1 << 35 { let masked = (val | (0x14 << 35)) ^ mask; self.writer.write_u8((masked >> 32) as u8)?; self.writer.write_u32::<BE>(masked as u32) } else if val < 1 << 43 { let masked = (val | (0x15 << 43)) ^ mask; self.writer.write_u16::<BE>((masked >> 32) as u16)?; self.writer.write_u32::<BE>(masked as u32) } else if val < 1 << 51 { let masked = (val | (0x16 << 51)) ^ mask; self.writer.write_u8((masked >> 48) as u8)?; self.writer.write_u16::<BE>((masked >> 32) as u16)?; self.writer.write_u32::<BE>(masked as u32) } else if val < 1 << 59 { let masked = (val | (0x17 << 59)) ^ mask; self.writer.write_u64::<BE>(masked as u64) } else { self.writer.write_u8((0x18 << 3) ^ mask as u8)?; self.writer.write_u64::<BE>(val ^ mask) }.map_err(From::from) } } impl<'a, W> serde::Serializer for &'a mut Serializer<W> where W: Write, { type Ok = (); type Error = Error; type SerializeSeq = Self; type SerializeTuple = Self; type SerializeTupleStruct = Self; type SerializeTupleVariant = Self; type SerializeMap = Self; type SerializeStruct = Self; type SerializeStructVariant = Self; fn serialize_bool(self, v: bool) -> Result<()> { let b = if v { 1 } else { 0 }; self.writer.write_u8(b)?; Ok(()) } fn serialize_i8(self, v: i8) -> Result<()> { self.writer.write_i8(v ^ i8::MIN)?; Ok(()) } fn serialize_i16(self, v: i16) -> Result<()> { self.writer.write_i16::<BE>(v ^ i16::MIN)?; Ok(()) } fn serialize_i32(self, v: i32) -> Result<()> { self.writer.write_i32::<BE>(v ^ i32::MIN)?; Ok(()) } fn serialize_i64(self, v: i64) -> Result<()> { self.writer.write_i64::<BE>(v ^ i64::MIN)?; Ok(()) } fn serialize_u8(self, v: u8) -> Result<()> { self.writer.write_u8(v)?; Ok(()) } fn serialize_u16(self, v: u16) -> Result<()> { self.writer.write_u16::<BE>(v)?; Ok(()) } fn serialize_u32(self, v: u32) -> Result<()> { self.writer.write_u32::<BE>(v)?; Ok(()) } fn serialize_u64(self, v: u64) -> Result<()> { self.writer.write_u64::<BE>(v)?; Ok(()) } /// Encode an `f32` into sortable bytes. /// /// `NaN`s will sort greater than positive infinity. -0.0 will sort directly before +0.0. /// /// See [Hacker's Delight 2nd Edition](http://www.hackersdelight.org/) Section 17-3. fn serialize_f32(self, v: f32) -> Result<()> { let val = unsafe { transmute::<f32, i32>(v) }; let t = (val >> 31) | i32::MIN; self.writer.write_i32::<BE>(val ^ t)?; Ok(()) } /// Encode an `f64` into sortable bytes. /// /// `NaN`s will sort greater than positive infinity. -0.0 will sort directly before +0.0. /// /// See [Hacker's Delight 2nd Edition](http://www.hackersdelight.org/) Section 17-3. fn serialize_f64(self, v: f64) -> Result<()> { let val = unsafe { transmute::<f64, i64>(v) }; let t = (val >> 63) | i64::MIN; self.writer.write_i64::<BE>(val ^ t)?; Ok(()) } fn serialize_char(self, v: char) -> Result<()> { let mut buf = [0u8; 4]; let n = v.encode_utf8(&mut buf).len(); self.writer.write_all(&buf[..n])?; Ok(()) } fn serialize_str(self, v: &str) -> Result<()> { self.writer.write_all(v.as_bytes())?; self.writer.write_u8(0u8)?; Ok(()) } fn serialize_bytes(self, v: &[u8]) -> Result<()> { self.writer.write_all(v)?; Ok(()) } fn serialize_none(self) -> Result<()> { self.writer.write_u8(0)?; Ok(()) } fn serialize_some<T>(self, v: &T) -> Result<()> where T: ?Sized + Serialize, { self.writer.write_u8(1)?; v.serialize(self) } fn serialize_unit(self) -> Result<()> { self.writer.write_all(&[])?; Ok(()) } fn serialize_unit_struct(self, _name: &'static str) -> Result<()> { self.serialize_unit() } fn serialize_unit_variant( self, _name: &'static str, variant_index: u32, _variant: &'static str, ) -> Result<()> { self.serialize_u32(variant_index) } fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(self) } fn serialize_newtype_variant<T>( self, _name: &'static str, variant_index: u32, _variant: &'static str, value: &T, ) -> Result<()> where T: ?Sized + Serialize, { self.writer.write_u32::<BE>(variant_index)?; value.serialize(self) } fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { Ok(self) } fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { Ok(self) } fn serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result<Self::SerializeTupleStruct> { Ok(self) } fn serialize_tuple_variant( self, _name: &'static str, variant_index: u32, _variant: &'static str, _len: usize, ) -> Result<Self::SerializeTupleVariant> { self.writer.write_u32::<BE>(variant_index)?; Ok(self) } fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeStruct> { Ok(self) } fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { Ok(self) } fn serialize_struct_variant( self, _name: &'static str, variant_index: u32, _variant: &'static str, _len: usize, ) -> Result<Self::SerializeStructVariant> { self.writer.write_u32::<BE>(variant_index)?; Ok(self) } } // Compound Implementations. impl<'a, W> serde::ser::SerializeSeq for &'a mut Serializer<W> where W: Write, { type Ok = (); type Error = Error; fn serialize_element<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(&mut **self) } fn end(self) -> Result<()> { Ok(()) } } impl<'a, W> serde::ser::SerializeTuple for &'a mut Serializer<W> where W: Write, { type Ok = (); type Error = Error; fn serialize_element<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(&mut **self) } fn end(self) -> Result<()> { Ok(()) } } impl<'a, W> serde::ser::SerializeTupleStruct for &'a mut Serializer<W> where W: Write, { type Ok = (); type Error = Error; fn serialize_field<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(&mut **self) } fn end(self) -> Result<()> { Ok(()) } } impl<'a, W> serde::ser::SerializeTupleVariant for &'a mut Serializer<W> where W: Write, { type Ok = (); type Error = Error; fn serialize_field<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(&mut **self) } fn end(self) -> Result<()> { Ok(()) } } impl<'a, W> serde::ser::SerializeMap for &'a mut Serializer<W> where W: Write, { type Ok = (); type Error = Error; fn serialize_key<T>(&mut self, key: &T) -> Result<()> where T: ?Sized + Serialize, { key.serialize(&mut **self) } fn serialize_value<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(&mut **self) } fn end(self) -> Result<()> { Ok(()) } } impl<'a, W> serde::ser::SerializeStruct for &'a mut Serializer<W> where W: Write, { type Ok = (); type Error = Error; fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(&mut **self) } fn end(self) -> Result<()> { Ok(()) } } impl<'a, W> serde::ser::SerializeStructVariant for &'a mut Serializer<W> where W: Write, { type Ok = (); type Error = Error; fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(&mut **self) } fn end(self) -> Result<()> { Ok(()) } } // Error implementation. impl From<io::Error> for Error { fn from(error: io::Error) -> Error { Error::Io(error) } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.to_string()) } } impl StdError for Error { fn description(&self) -> &str { match *self { Error::Message(ref msg) => msg, Error::Io(ref err) => err.description(), } } fn cause(&self) -> Option<&StdError> { match *self { Error::Message(ref _msg) => None, Error::Io(ref err) => err.source(), } } } impl serde::ser::Error for Error { fn custom<T: fmt::Display>(msg: T) -> Self { Error::Message(msg.to_string()) } }