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 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
//! A pure-rust implementation of the GVariant serialisation format intended for //! fast reading of in-memory buffers. //! //! ```rust //! # use gvariant::{aligned_bytes::copy_to_align, gv, Marker}; //! let data = copy_to_align(b"\x22\x00\x00\x00William\0"); //! let (age, name) = gv!("(is)").cast(data.as_ref()).into(); //! assert_eq!( //! format!("My name is {} and I am {} years old!", name, age), //! "My name is William and I am 34 years old!"); //! ``` //! //! This library operates by reinterpreting byte buffers as a GVariant type. It //! doesn't do any of its own allocations. As a result proper alignment of byte //! buffers is the responsibility of the user. See [`aligned_bytes`]. //! //! It's intended to conform to the [GVariant specification] and match the //! behaviour of the reference [GLib implementation], preferring the latter //! rather than the former where they disagree. Exceptions to this are //! described in ["Deviations from the Specification and reference //! implementation"](#deviations-from-the-specification-and-reference-implementation) //! below. //! //! This library assumes you know the types of the data you are dealing with at //! compile time. This is in contrast to the GLib implementation where you //! could construct a GVariant type string dynamically. This allows for a much //! smaller and faster implementation, more in line with Alexander Larsson's //! [GVariant Schema Compiler]. As a result GVariant structs are supported //! through use of code generation via macros. See the gvariant-macro //! subdirectory. //! //! The library is intended to be sound and safe to run on untrusted input, //! although the implementation does include use of `unsafe`. See ["Use of //! `unsafe`"](#use-of-unsafe) below. Help with validating the unsafe portions //! of the library would be gratefully received. //! //! This library works Rust stable. As a result we can't use const-generics, //! which would make some of the code much more streightforward. A future //! version of this library may use const-generics, once they are available in //! stable rust. //! //! [GLib implementation]: https://developer.gnome.org/glib/stable/glib-GVariant.html //! [GVariant Schema Compiler]: https://gitlab.gnome.org/alexl/variant-schema-compiler/ //! [GVariant specification]: https://people.gnome.org/~desrt/gvariant-serialisation.pdf //! //! ## Status //! //! * Serialization and Deserialization is supported //! * Support for all GVariant types is implemented //! * Behaviour is identical to GLib's implementation for all data in "normal //! form". This has been confirmed with fuzz testing. There are some //! differences for data not in normal form. See [GNOME/glib#2121] for more //! information. //! //! [GNOME/glib#2121]: https://gitlab.gnome.org/GNOME/glib/-/issues/2121 //! //! ### TODO //! //! * Fuzz testing of structure types - compare against the GLib version //! * Serialization of structure types //! * Fuzz test serialization //! //! ## Features //! //! ### `std` - enabled by default //! //! Required for: //! //! * our errors to implement [`std::error::Error`] //! * [`Marker::deserialize`] //! * [`aligned_bytes::read_to_slice`] //! * Some CPU dependent string handling optimisations in the memchr crate //! //! Disable this feature for no-std support. //! //! ### `alloc` - enabled by default //! //! Required for: //! //! * Allocating [`AlignedSlice`]s with [`ToOwned`], //! [`copy_to_align`][aligned_bytes::copy_to_align] and //! [`alloc_aligned`][aligned_bytes::alloc_aligned]. //! * The convenience API `Marker::from_bytes` - use `Marker::cast` instead //! * Correctly displaying non-utf-8 formatted strings //! * Copying unsized GVariant objects with `to_owned()` //! * The std feature //! //! ## Deviations from the Specification and reference implementation //! //! This implementation is intended to conform to the [GVariant specification] //! and match the behaviour of the reference [GLib implementation], preferring //! the latter rather than the former where they disagree. //! //! ### Maximum size of objects //! //! The spec says: //! //! > **2.3.6 Framing Offsets** //! > //! >There is no theoretical upper limit in how large a framing offset can be. //! >This fact (along with the absence of other limitations in the serialisation //! >format) allows for values of arbitrary size. //! //! In this implementation the maximum size of an object is [`usize`] (typically //! 64-bits). This should not be a problem in practice on 64-bit machines. //! //! ### Equality of Variant **v** type for non-normal form data //! //! See note under [`Variant`]. //! //! ### Validation of non-normal form object path "o" and signature "g" types //! //! The spec says: //! //! > ### 2.7.3 Handling Non-Normal Serialised Data //! > //! > #### Invalid Object Path //! > //! > If the serialised form of an object path is not a valid object path //! > followed by a zero byte then the default value is used. //! > //! > #### Invalid Signature //! > //! > If the serialised form of a signature string is not a valid DBus signature //! > followed by a zero byte then the default value is used. //! //! We don't currently do any validation of the object path or signature types, //! treating them as normal strings. //! //! ### Data that overlaps framing offsets (non-normal form) //! //! This applies to arrays of non-fixed size type in non-normal form. We follow //! the behaviour of GLib reference implementation rather than the GVariant spec //! in this instance. //! //! The spec says: //! //! > #### Child Values Overlapping Framing Offsets //! > //! > If the byte sequence of a child value overlaps the framing offsets of the //! > container it resides within then this error is ignored. The child is given //! > a value that corresponds to the normal deserialisation process performed //! > on this byte sequence (including the bytes from the framing offsets) with //! > the type of the child. //! //! Whereas we give the child value the default value for the type consistent //! with the GLib implementation. This is the behaviour in GLib since 2.60, //! 2.58.2 and 2.56.4. //! //! See [GNOME/glib#2121] for more information. //! //! ### Handling of non-normal form strings //! //! We are consistent with the GLib implementation in this regard rather than //! the spec. See the note under [`Str::to_str`] //! //! ## Design //! //! The intention is to build abstractions that are transparent to the compiler, //! such that they compile down to simple memory accesses, like reading the //! fields of a struct. For many of the GVariant types rust already has a type //! with the same representation (such as `i32` for **i** or `[u8]` for **ay**). //! For other types this library defines such types (such as //! [`gvariant::Str`][Str] for **s** or //! [`gvariant::NonFixedWidthArray<[i32]>`][NonFixedWidthArray] for **aai**). //! For structure types this library provides a macro [`gv!`] to generate the //! code for struct types. //! //! If we have a type with the same representation as the underlying bytes we //! can just cast the data to the appropriate type and then read it. The macro //! [`gv!`] maps from GVariant typestrs to compatible Rust types returning a //! [`Marker`]. This [`Marker`] can then be used to cast data into that type by //! calling `Marker::cast`. //! //! So typically code might look like: //! //! # use gvariant::{aligned_bytes::alloc_aligned, gv, Marker}; //! # use std::io::Read; //! # fn a() -> std::io::Result<()> { //! # let mut file = std::fs::File::open("")?; //! let mut buf = alloc_aligned(4096); //! let len = file.read(&mut buf)?; //! let data = gv!("a(sia{sv})").cast(&buf[..len]); //! # todo!() //! # } //! //! For casting data to be valid and safe the byte buffer must be aligned... //! //! ### Use of `unsafe` //! //! I've tried to concentrate almost all of the unsafe in [`aligned_bytes`] and //! [`casting`] to make it easier to review. I also take advantage of the //! [`ref_cast`] crate to avoid some unsafe casting that I'd otherwise require. //! //! A review of the use of `unsafe`, or advice on how the amount of unsafe could //! be reduced would be greatly appreciated. //! //! ## Comparison to and relationship with other projects //! //! * [GVariant Schema Compiler] - Similar to this project the GSC generates //! code at compile time to represent the types the user is interested in. GSC //! targets the C language. Unlike this project the types are generated from //! schema files, allowing structures to have named fields. In gvariant-rs we //! generate our code just from the plain GVariant type strings using macros. //! This makes the build process simpler - there are no external tools, and it //! makes it easier to get started - there is no new schema format to learn. //! The cost is that the user is responsible for remember which field means //! what and what endianness should be used to interpret the data. //! //! It might make sense in the future to extend GSC to generate rust code as //! well - in which case the generated code may depend on this library. //! * [gtk-rs glib::variant](https://gtk-rs.org/docs/glib/variant/index.html) - //! This is a binding to the GLib GVariant implementation in C, so depends on //! glib. It's currently incomplete. The docs say "Although `GVariant` //! supports arbitrarily complex types, this binding is currently limited to //! the basic ones: `bool`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, //! `f64` and `&str`/`String`." //! * [zvariant](https://crates.io/crates/zvariant) - Implements the similar //! DBus serialisation format rather than GVariant. Docs say: "GVariant ... //! will be supported by a future version of this crate." //! * [serde_gvariant](https://github.com/lucab/serde_gvariant) - Implements the //! same format, but for serde integration. Described as "WIP" and not //! published on crates.io #![cfg_attr(not(feature = "std"), no_std)] #[cfg(feature = "alloc")] extern crate alloc; #[cfg(feature = "alloc")] use alloc::{borrow::ToOwned, string::String}; use core::{ convert::TryInto, fmt::{Debug, Display}, hash::Hash, marker::PhantomData, }; #[cfg(feature = "std")] use std::io::Write; use memchr; use ref_cast::RefCast; pub mod aligned_bytes; use offset::align_offset; pub mod casting; mod offset; use aligned_bytes::{empty_aligned, AlignedSlice, AsAligned, A8}; use casting::{AlignOf, AllBitPatternsValid}; #[doc(hidden)] pub use gvariant_macro::{define_gv as _define_gv, gv_type as _gv_type}; /// This is the return type of the `gv!` macro. /// /// This acts as a kind of factory trait for GVariant types, creating them from /// aligned data using the `cast` method. /// /// Do not implement this trait yourself, `gv!` is responsible for creating the /// marker structs that implement this. Use that instead. /// /// See the documentation of `gv!` for usage examples. pub trait Marker: Copy { /// The typestr that was passed to the `gv!` macro. const TYPESTR: &'static [u8]; /// This type has the same representation in memory as the GVariant type /// with the signature given by `Self::TYPESTR`, and implements `Cast` so it /// can be created from appropriately aligned data. type Type: Cast + ?Sized; // I'd like to remove the `&self` argument as it isn't used, but see comment // below in macro_rules! gv /// Cast `data` to the appropriate rust type `Self::Type` for the type /// string `Self::TYPESTR`. /// /// Use this in preference to `deserialize` if you already have the data in /// (properly aligned) memory. This makes no allocations and has no /// dependency on `std` or `alloc`. /// /// Example /// /// # use gvariant::{gv, Marker, Structure}; /// # let aligned_data = gvariant::aligned_bytes::empty_aligned(); /// let (my_int, my_str) = gv!("(ias)").cast(aligned_data).to_tuple(); fn cast<'a>(&self, data: &'a AlignedSlice<<Self::Type as AlignOf>::AlignOf>) -> &'a Self::Type { Self::Type::from_aligned_slice(data) } /// Cast `data` to the appropriate rust type `Self::Type` for the type /// string `Self::TYPESTR`. fn try_cast_mut<'a>( data: &'a mut AlignedSlice<<Self::Type as AlignOf>::AlignOf>, ) -> Result<&'a mut Self::Type, casting::WrongSize> { Self::Type::try_from_aligned_slice_mut(data) } /// Read the data from r returning an owned deserialised GVariant object /// /// Example /// /// # use gvariant::{gv, Marker}; /// # fn moo(myfile: &str) -> std::io::Result<()> { /// # let myfile = ""; /// let v = gv!("s").deserialize(std::fs::File::open(myfile)?)?; /// assert_eq!(&*v, "An example string"); /// # Ok(()) /// # } /// /// This requires the features std and alloc be enabled on the gvariant /// crate. #[cfg(feature = "std")] fn deserialize( &self, r: impl std::io::Read, ) -> std::io::Result<<Self::Type as ToOwned>::Owned> { let data = aligned_bytes::read_to_slice(r, None)?; Ok(self.cast(&*data).to_owned()) } /// Deserialise the given `data`, making a copy in the process. /// /// This is a convenience API wrapper around `copy_to_align` and `cast` /// allowing users to not have to think about the alignment of their data. /// It is usually better to ensure the data you have is aligned, for example /// using `alloc_aligned` or `read_to_slice`, and then use `cast` directly. /// This way you can avoid additional allocations, avoid additional copying, /// and work in noalloc contexts. /// /// Example /// /// # use gvariant::{gv, Marker}; /// let v = gv!("s").from_bytes(b"An example string\0"); /// assert_eq!(&*v, "An example string"); #[cfg(feature = "alloc")] fn from_bytes(&self, data: impl AsRef<[u8]>) -> <Self::Type as ToOwned>::Owned { let cow = aligned_bytes::copy_to_align(data.as_ref()); self.cast(cow.as_ref()).to_owned() } /// Serialize the data to the given stream as a GVariant /// /// To be serialized as a GVariant the passed type must implement /// `SerializeTo<>` for the relevant GVariant type. /// /// Example /// /// # use gvariant::{gv, Marker}; /// # fn m(mut myfile: impl std::io::Write) -> std::io::Result<()> { /// let comment = Some("It's great!"); /// gv!("ms").serialize(&comment, &mut myfile)?; /// # Ok(()) /// # } #[cfg(feature = "std")] fn serialize( &self, data: impl SerializeTo<Self::Type>, out: &mut impl Write, ) -> std::io::Result<usize> { data.serialize(out) } /// Convenience method for in-memory serialization /// /// Used by our tests. You probably want to use the more flexible /// `serialize` instead which can be used to write to files/sockets. #[cfg(feature = "std")] fn serialize_to_vec(&self, data: impl SerializeTo<Self::Type>) -> Vec<u8> { let mut out = vec![]; self.serialize(data, &mut out) .expect("Serialization to Vec should be infallible"); out } } /// Trait to enable Serialization to GVariant /// /// T in this instance is the type to be serialised to. For example: for /// GVariant type "ai" this will be `[i32]`. This means that there is a /// different SerializeTo trait for every GVariant type, and any rust type may /// implement serialization to any GVariant type. For example `&[u8]` can be /// serialized as a GVariant string "s" with `SerializeTo<Str>` or as a byte /// array with `SerializeTo<[u8]>`. /// /// `SerializeTo<>` is implemented for appropriate built-in types, and you may /// wish to implement it for your own types as well. pub trait SerializeTo<T: Cast + ?Sized> { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize>; } /// Maps from GVariant typestrs to compatible Rust types returning a `Marker`. /// This `Marker` can then be used to cast data into that type by calling /// `Marker::cast`. /// /// The signature is essentially `fn gv(typestr : &str) -> impl Marker`. /// /// This is the main entrypoint to the library. /// /// Given `data` that you want to interpret as a GVariant of type **as** you /// write: /// /// # use gvariant::{aligned_bytes::empty_aligned, gv, Marker}; /// # let data = empty_aligned(); /// gv!("as").cast(data); /// /// Similarly if you want to interpret some data in a variant as an **as** you /// write: /// /// # use gvariant::{aligned_bytes::empty_aligned, gv, Marker, Variant}; /// # let v = gv!("v").cast(empty_aligned()); /// v.get(gv!("as")); /// /// The returned marker has a automatically generated type. `Marker::TYPESTR` /// will equal the typestr passed into the `gv!` invocation. `Marker::Type` is /// a type that has the same bit representation as GVariant type passed in. /// /// The types are mapped as follows: /// /// | GVariant Type | Rust Type | Sized | /// | ------------- | ------------------------------------------------------------------------------------------- | --------------------------------- | /// | **b** | [`Bool`] | Yes | /// | **y** | [`u8`] | Yes | /// | **n** | [`i16`] | Yes | /// | **q** | [`u16`] | Yes | /// | **i** | [`i32`] | Yes | /// | **u** | [`u32`] | Yes | /// | **x** | [`i64`] | Yes | /// | **t** | [`u64`] | Yes | /// | **d** | [`f64`] | Yes | /// | **s** | [`Str`] | No | /// | **o** | [`Str`] | No | /// | **g** | [`Str`] | No | /// | **v** | [`Variant`] | No | /// | **m**s | [`MaybeNonFixedSize<Str>`][MaybeNonFixedSize] - and similarly for all non-[`Sized`] types | No | /// | **m**i | [`MaybeFixedSize<i32>`][MaybeFixedSize] - and similarly for all [`Sized`] types | No | /// | **a**s | [`NonFixedWidthArray<Str>`][NonFixedWidthArray] - and similarly for all non-[`Sized`] types | No | /// | **a**i | `[i32]` and similarly for all [`Sized`] types | No | /// | **(sv)** | Custom struct generated by this macro. Implements `.to_tuple()` method | Yes if all children are [`Sized`] | /// | **{si}** | Custom struct generated by this Macro. Implements `.to_tuple()` method | Yes if all children are [`Sized`] | #[macro_export] macro_rules! gv { ($typestr:literal) => {{ #[allow(unused_imports)] mod _m { use $crate::aligned_bytes::{ align_offset, empty_aligned, AlignedOffset, AlignedSlice, AsAligned, }; use $crate::casting::{AlignOf, AllBitPatternsValid}; use $crate::*; _define_gv!($typestr); #[derive(Copy, Clone)] pub(crate) struct Marker(); impl $crate::Marker for Marker { type Type = _gv_type!($typestr); #[allow(clippy::string_lit_as_bytes)] const TYPESTR: &'static [u8] = $typestr.as_bytes(); } }; // TODO: I'd much rather that this macro returns a type, rather than // a value. That way getting a gvariant looks like: // // let a : <gv!("as")> = v.get()? // let b = <gv!("(yii)")>::cast(bytes); // // rather than: // // let a = v.get(gv!("as"))? // let b = gv!("(yii)").cast(bytes); // // The former makes it much clearer what's happening at compile time // and what's happening at run time. // // As it is, when I try to make this return a type I get the error // message _m::Marker() }}; } /// Trait implemented by all our types that have the same representation as the /// GVariant type /// /// This allows casting appropriately aligned [`AlignedSlice`]s to rust types. /// /// Don't implement this class for your own types. It's already implemented for /// all appropriate types. It's automatically implemented for [`Structure`] /// types generated by the [`gv!`] macro. pub trait Cast: casting::AlignOf + casting::AllBitPatternsValid + 'static + PartialEq + Debug + ToOwned { /// Cast `slice` to type `Self`. /// /// This always succeeds. If the slice is the wrong size a defualt value is /// returned in accordance with the GVariant spec. fn from_aligned_slice(slice: &AlignedSlice<Self::AlignOf>) -> &Self { match Self::try_from_aligned_slice(slice) { Ok(x) => x, Err(_) => Self::default_ref(), } } /// Get a static reference to the default value for this type. /// /// In GVariant every type has a default value which is used in certian /// circumstances in-lieu of returning errors during deserialisation. We're /// always dealing with references so [`std::default::Default`] isn't /// appropriate. fn default_ref() -> &'static Self; fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize>; fn try_from_aligned_slice_mut( slice: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize>; } macro_rules! impl_cast_for { ($t:ty, $default:expr) => { impl Cast for $t { fn default_ref() -> &'static Self { &$default } fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize> { casting::try_cast_slice_to::<Self>(slice) } fn try_from_aligned_slice_mut( slice: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize> { casting::try_cast_slice_to_mut::<Self>(slice) } } impl SerializeTo<$t> for $t { fn serialize(self, f: &mut impl std::io::Write) -> std::io::Result<usize> { f.write_all(self.to_ne_bytes().as_ref())?; Ok(std::mem::size_of::<$t>()) } } impl SerializeTo<$t> for &$t { fn serialize(self, f: &mut impl std::io::Write) -> std::io::Result<usize> { (*self).serialize(f) } } }; } impl_cast_for!(u8, 0); impl_cast_for!(u16, 0); impl_cast_for!(i16, 0); impl_cast_for!(u32, 0); impl_cast_for!(i32, 0); impl_cast_for!(u64, 0); impl_cast_for!(i64, 0); impl_cast_for!(f64, 0.); /// Type with same representation as GVariant "s", "o" and "g" types /// /// This is the type returned by: /// /// # use gvariant::{aligned_bytes::empty_aligned, gv, Marker}; /// # let data = empty_aligned(); /// gv!("s").cast(data); /// /// We can't use Rust's `str` type here because GVariant strings always end with /// a NUL byte. #[derive(RefCast, Eq)] #[repr(transparent)] pub struct Str { data: [u8], } #[cfg(feature = "alloc")] impl ToOwned for Str { type Owned = Box<Self>; fn to_owned(&self) -> Self::Owned { casting::ref_cast_box(self.data.to_owned().into_boxed_slice()) } } impl Str { /// Convert `&Str` to `&[u8]` /// /// This will give the same result as `s.to_str().as_bytes()` for normal /// data, but unlike using `to_str()` it should be 0-cost as it doesn't /// require scanning the underlying data. /// /// The result of this function will deviate from the GLib GVariant /// implementation if the data contains embedded NULs or non-utf8 data. The /// spec says: /// /// > **2.7.3 Handling Non-Normal Serialised Data** /// > /// >**String with Embedded Nul** /// > /// > If a string has a nul character as its final byte, but also contains /// > another nul character before this final terminator, the value of the /// > string is taken to be the part of the string that precedes the embedded /// > nul. This means that obtaining a C pointer to a string is still a /// > constant time operation. /// /// Instead this function will return the data with the embedded NULs intact /// (excluding the final NUL byte) pub fn as_bytes_non_conformant(&self) -> &[u8] { let d: &[u8] = self.data.as_ref(); match d.last() { Some(b'\0') => &d[..d.len() - 1], _ => b"", } } /// Convert `&Str` to `&str` /// /// For consistency with the GLib implementation this will return a empty /// string if the underlying data is not utf-8 encoded or contains embedded /// NULs. This differs from the wording of the GVariant specification which /// says that "the use of UTF-8 is expected and encouraged", but it is not /// guaranteed. /// /// This function executes in linear time with the length of the data. If /// you know that your data is in normal form you can use /// `self.to_bytes_non_conformant()` instead which executes in constant /// time. pub fn to_str(&self) -> &str { let b = self.as_bytes_non_conformant(); if memchr::memchr(b'\0', b).is_some() { "" } else { match core::str::from_utf8(&self.as_bytes_non_conformant()) { Ok(x) => x, Err(_) => "", } } } } unsafe impl AllBitPatternsValid for Str {} unsafe impl AlignOf for Str { type AlignOf = aligned_bytes::A1; } impl Cast for Str { fn default_ref() -> &'static Self { unsafe { &*(b"" as *const [u8] as *const Str) } } fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize> { Ok(Self::ref_cast(slice.as_ref())) } fn try_from_aligned_slice_mut( slice: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize> { Ok(Self::ref_cast_mut(slice.as_mut())) } } impl SerializeTo<Str> for &Str { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { let b = self.to_str().as_bytes(); f.write_all(b)?; f.write_all(b"\0")?; Ok(b.len() + 1) } } impl SerializeTo<Str> for &str { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { let b = self.as_bytes(); if memchr::memchr(b'\0', b).is_some() { // Can't encode strings with embedded NULs with GVariant return Err(std::io::Error::new( std::io::ErrorKind::InvalidInput, "Strings may not contain NULs", )); } f.write_all(self.as_bytes())?; f.write_all(b"\0")?; Ok(self.len() + 1) } } impl<T: SerializeTo<Str> + Copy> SerializeTo<Str> for &T { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { (*self).serialize(f) } } impl SerializeTo<Str> for &Box<Str> { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { self.to_str().serialize(f) } } impl SerializeTo<Str> for &String { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { f.write_all(self.as_bytes())?; f.write_all(b"\0")?; Ok(self.len() + 1) } } impl PartialEq for Str { fn eq(&self, other: &Self) -> bool { self.as_bytes_non_conformant() == other.as_bytes_non_conformant() || self.to_str() == other.to_str() } } impl PartialEq<Str> for str { fn eq(&self, other: &Str) -> bool { self == other.to_str() } } impl PartialEq<str> for Str { fn eq(&self, other: &str) -> bool { self.to_str() == other } } impl Display for Str { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::fmt::Display::fmt(self.to_str(), f) } } impl Debug for Str { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::fmt::Debug::fmt(self.to_str(), f) } } // TODO: Replace this with core::str::lossy::Utf8Lossy if it's ever stabilised. struct DisplayUtf8Lossy<'a>(&'a [u8]); impl core::fmt::Display for DisplayUtf8Lossy<'_> { #[cfg(feature = "alloc")] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::fmt::Display::fmt(&String::from_utf8_lossy(self.0).as_ref(), f) } #[cfg(not(feature = "alloc"))] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::fmt::Display::fmt( match core::str::from_utf8(self.0) { Ok(x) => x, Err(_) => "<Error: Invalid Utf-8>", }, f, ) } } impl core::fmt::Debug for DisplayUtf8Lossy<'_> { #[cfg(feature = "alloc")] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::fmt::Debug::fmt(&String::from_utf8_lossy(self.0).as_ref(), f) } #[cfg(not(feature = "alloc"))] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match core::str::from_utf8(self.0) { Ok(x) => core::fmt::Debug::fmt(x, f), Err(_) => core::fmt::Display::fmt("<Error: Invalid Utf-8>", f), } } } /// The GVariant Variant **v** type /// /// The Variant type can contain any GVariant value. /// /// ### Non-spec conformant implementation of Equality with non-normal form data /// /// While every value has a single canoncial byte representation ("normal form") /// there other representations that have the same value. For example: values /// of type **(yi)** have 3B of padding between the **y** and the **i**. In /// normal form these bytes are 0, but they are irrelevant for the actual value. /// Ignoring the value of the padding bytes is correct according to the spec. /// /// This is handled correctly when comparing the values of two **(yi)** /// instances in rust code. What isn't correct is the handling of `Variant` /// **v** types that contain non-normal data with respect to checking for /// equality. Correct checking of equality would require deserialising the data /// according to the typestr contained within the variant, and then doing the /// comparison. We don't do run-time interpretation of typestrs in this crate, /// prefering to do it at compile time. Instead we just compare the underlying /// data. This gives correct results for data in normal form, but there will be /// some false-negatives for non-normal form data. /// /// Therefore [`Variant`] implements [`PartialEq`], but not [`Eq`] because the /// comparison is not "reflexive". #[derive(RefCast)] #[repr(transparent)] pub struct Variant(AlignedSlice<A8>); unsafe impl AlignOf for Variant { type AlignOf = A8; } unsafe impl AllBitPatternsValid for Variant {} impl Cast for Variant { fn default_ref() -> &'static Self { Self::ref_cast(empty_aligned()) } fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize> { Ok(Self::ref_cast(slice)) } fn try_from_aligned_slice_mut( slice: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize> { Ok(Self::ref_cast_mut(slice)) } } impl SerializeTo<Variant> for &Variant { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { // Our ability to normalise variants is limited because we only deal // with typestrs at compile time and this could be anything. let (typestr, data) = self.split(); f.write_all(data)?; f.write_all(b"\0")?; f.write_all(typestr)?; Ok(data.len() + typestr.len() + 1) } } impl Debug for Variant { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let (gv_type, data) = self.split(); write!( f, "Variant {{ type: {:?}, data: {:?} }}", DisplayUtf8Lossy(gv_type), data.as_ref() as &[u8] ) } } #[cfg(feature = "alloc")] impl ToOwned for Variant { type Owned = Box<Self>; fn to_owned(&self) -> Self::Owned { casting::ref_cast_box(self.0.to_owned()) } } impl Variant { /// Get the value from the variant, if it matches the type passed in. /// /// Example: /// /// # use gvariant::{aligned_bytes::empty_aligned, gv, Marker, Variant}; /// # let v = gv!("v").cast(empty_aligned()); /// let a = v.get(gv!("ai")); /// // a now has type &[i32] pub fn get<M: Marker>(&self, m: M) -> Option<&M::Type> where AlignedSlice<A8>: AsAligned<<M::Type as AlignOf>::AlignOf>, { let (typestr, data) = self.split(); if typestr == M::TYPESTR { Some(m.cast(data.as_aligned())) } else { None } } /// Destructures the variant into (typestr, data). /// /// Note: typestr is not guaranteed to be a valid GVariant type. /// /// Example use: /// /// # use gvariant::{aligned_bytes::{A8, copy_to_align, empty_aligned}, gv, Variant, Marker}; /// # let data = copy_to_align::<A8>(b"a\0(is)"); /// # let data = data.as_ref(); /// # let v = gv!("v").cast(data); /// match v.split() { /// (b"(is)", _) => { /// let s = v.get(gv!("(is)")); /// // Do something with s /// } /// (ty, _) => panic!("Unexpected variant type {:?}", ty) /// } pub fn split(&self) -> (&[u8], &AlignedSlice<A8>) { // Variants are serialised by storing the serialised data of the child, // plus a zero byte, plus the type string of the child. let split_pos = memchr::memrchr(b'\0', &self.0); if let Some(mid) = split_pos { let (data, ty) = self.0.split_at(mid); (&ty[1..], data) } else { (b"()", empty_aligned()) } } } impl PartialEq for Variant { /// Caveat: The current implementation has false negatives for data not in /// "normal form". This may change in the future. fn eq(&self, other: &Self) -> bool { self.split() == other.split() } } // #### 2.5.3.1 Fixed Width Arrays // // In this case, the serialised form of each array element is packed // sequentially, with no extra padding or framing, to obtain the array. Since // all fixed-sized values have a size that is a multiple of their alignment // requirement, and since all elements in the array will have the same alignment // requirements, all elements are automatically aligned. // // The length of the array can be determined by taking the size of the array and // dividing by the fixed element size. This will always work since all // fixed-size values have a non-zero size. // // We implement this a normal rust slice. impl<'a, T: Cast + 'static + Copy> Cast for [T] { fn default_ref() -> &'static Self { &[] } fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize> { casting::cast_slice::<Self::AlignOf, T>(slice) } fn try_from_aligned_slice_mut( _: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize> { todo!() } } impl<GvT: Cast + Copy, It: IntoIterator> SerializeTo<[GvT]> for It where It::Item: SerializeTo<GvT>, { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { let mut bytes_written = 0; for x in self.into_iter() { bytes_written += x.serialize(f)?; } Ok(bytes_written) } } // 2.3.6 Framing Offsets // // If a container contains non-fixed-size child elements, it is the // responsibility of the container to be able to determine their sizes. This is // done using framing offsets. // // A framing offset is an integer of some predetermined size. The size is always // a power of 2. The size is determined from the overall size of the container // byte sequence. It is chosen to be just large enough to reference each of the // byte boundaries in the container. // // As examples, a container of size 0 would have framing offsets of size 0 // (since no bits are required to represent no choice). A container of sizes 1 // through 255 would have framing offsets of size 1 (since 256 choices can be // represented with a single byte). A container of sizes 256 through 65535 would // have framing offsets of size 2. A container of size 65536 would have framing // offsets of size 4. // // There is no theoretical upper limit in how large a framing offset can be. // This fact (along with the absence of other limitations in the serialisation // format) allows for values of arbitrary size. // // When serialising, the proper framing offset size must be determined by “trial // and error” — checking each size to determine if it will work. It is possible, // since the size of the offsets is included in the size of the container, that // having larger offsets might bump the size of the container up into the next // category, which would then require larger offsets. Such containers, however, // would not be considered to be in “normal form”. The smallest possible offset // size must be used if the serialised data is to be in normal form. // // Framing offsets always appear at the end of containers and are unaligned. // They are always stored in little-endian byte order. #[doc(hidden)] #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum OffsetSize { U0 = 0, U1 = 1, U2 = 2, U4 = 4, U8 = 8, } #[doc(hidden)] pub fn offset_size(len: usize) -> OffsetSize { match len { 0 => OffsetSize::U0, 0x1..=0xFF => OffsetSize::U1, 0x100..=0xFFFF => OffsetSize::U2, 0x1_0000..=0xFFFF_FFFF => OffsetSize::U4, 0x1_0000_0000..=0xFFFF_FFFF_FFFF_FFFF => OffsetSize::U8, _ => unreachable!(), } } #[doc(hidden)] pub fn read_uint(data: &[u8], size: OffsetSize, n: usize) -> usize { let s = n * size as usize; match size { OffsetSize::U0 => 0, OffsetSize::U1 => data[s] as usize, OffsetSize::U2 => u16::from_le_bytes(data[s..s + 2].try_into().unwrap()) as usize, OffsetSize::U4 => u32::from_le_bytes(data[s..s + 4].try_into().unwrap()) as usize, OffsetSize::U8 => u64::from_le_bytes(data[s..s + 8].try_into().unwrap()) as usize, } } fn read_last_frame_offset(data: &[u8]) -> (OffsetSize, usize) { let osz = offset_size(data.len()); if osz == OffsetSize::U0 { (OffsetSize::U1, 0) } else { let last = read_uint(&data[data.len() - osz as usize..], osz, 0); if last > data.len() { return (osz, data.len()); } let size = data.len() - last; if size % osz as usize == 0 { // Normal form (osz, last) } else { // In the event that the final framing offset of a non-fixed-width // array ... indicates a non-integral number of framing offsets is // present in the array, the value is taken to be the empty array. (osz, data.len()) } } } /// Type with same representation as GVariant "aX" type where X is any non-fixed /// size type /// /// This is similar to a [`slice`][std::slice], but for non-fixed width types, /// and implements many of the same methods. Items can be retrieved by indexing /// or iterated over. /// /// For fixed-width types a standard rust slice is used. #[derive(RefCast)] #[repr(transparent)] pub struct NonFixedWidthArray<T: Cast + ?Sized> { data: AlignedSlice<T::AlignOf>, } impl<T: Cast + Debug + ?Sized> Debug for NonFixedWidthArray<T> { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "[")?; for child in self { write!(f, "{:?}, ", child)?; } write!(f, "]") } } #[cfg(feature = "alloc")] impl<T: Cast + ?Sized> ToOwned for NonFixedWidthArray<T> { type Owned = Box<Self>; fn to_owned(&self) -> Self::Owned { casting::ref_cast_box(self.data.to_owned()) } } unsafe impl<T: Cast + ?Sized> AlignOf for NonFixedWidthArray<T> { type AlignOf = T::AlignOf; } unsafe impl<T: Cast + ?Sized> AllBitPatternsValid for NonFixedWidthArray<T> {} impl<T: Cast + ?Sized> Cast for NonFixedWidthArray<T> { fn default_ref() -> &'static Self { Self::ref_cast(empty_aligned()) } fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize> { Ok(Self::ref_cast(slice)) } fn try_from_aligned_slice_mut( slice: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize> { Ok(Self::ref_cast_mut(slice)) } } impl<T: Cast + ?Sized> NonFixedWidthArray<T> { /// Returns the number of elements in the array. pub fn len(&self) -> usize { // Since determining the length of the array relies on our ability // to count the number of framing offsets and since the number of // framing offsets is determined from how much space they take up, // zero byte framing offsets are not permitted in arrays, even in // the case where all other serialised data has a size of zero. This // special exception avoids having to divide zero by zero and wonder // what the answer is. let (osz, lfo) = read_last_frame_offset(&self.data); (self.data.len() - lfo) / osz as usize } /// Returns `true` if the array has a length of 0. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Returns an iterator over the array. pub fn iter(&self) -> NonFixedWidthArrayIterator<T> { self.into_iter() } /// Returns the first element of the array, or [`None`] if it is empty. pub fn first(&self) -> Option<&T> { if self.is_empty() { None } else { Some(&self[0]) } } /// Returns the last element of the array, or [`None`] if it is empty. pub fn last(&self) -> Option<&T> { if self.is_empty() { None } else { Some(&self[self.len() - 1]) } } } impl<T: Cast + PartialEq + ?Sized> PartialEq for NonFixedWidthArray<T> { fn eq(&self, other: &Self) -> bool { if self.len() != other.len() { return false; } for (a, b) in self.iter().zip(other) { if a != b { return false; } } true } } impl<T: Cast + PartialEq + Eq + ?Sized> Eq for NonFixedWidthArray<T> {} /// A iterator over the items of a [`NonFixedWidthArray`] /// /// This struct is created by the [`iter`] method on [`NonFixedWidthArray`]. /// See its documentation for more. /// /// [`iter`]: NonFixedWidthArray::iter pub struct NonFixedWidthArrayIterator<'a, Item: Cast + ?Sized> { data: &'a AlignedSlice<Item::AlignOf>, offsets: &'a [u8], next_start: usize, offset_idx: usize, offset_size: OffsetSize, } impl<Item: Cast + ?Sized + PartialEq<T>, T: ?Sized> PartialEq<[&T]> for NonFixedWidthArray<Item> { fn eq(&self, other: &[&T]) -> bool { if self.len() != other.len() { return false; } for (a, b) in self.iter().zip(other.iter()) { if a != *b { return false; } } true } } impl<Item: Cast + ?Sized + PartialEq<T>, T: ?Sized> PartialEq<NonFixedWidthArray<Item>> for [&T] { fn eq(&self, other: &NonFixedWidthArray<Item>) -> bool { other == self } } impl<'a, Item: Cast + 'static + ?Sized> Iterator for NonFixedWidthArrayIterator<'a, Item> { type Item = &'a Item; fn next(&mut self) -> Option<Self::Item> { if self.offset_idx >= self.offsets.len() { None } else { let start = align_offset::<Item::AlignOf>(self.next_start); let end = read_uint(&self.offsets[self.offset_idx..], self.offset_size, 0); self.offset_idx += self.offset_size as usize; self.next_start = end; if end < start || end > self.data.len() { // If the framing offsets (or calculations based on them) // indicate that any part of the byte sequence of a child value // would fall outside of the byte sequence of the parent then // the child is given the default value for its type. Some(Item::try_from_aligned_slice(aligned_bytes::empty_aligned()).unwrap()) } else { Some(Item::try_from_aligned_slice(&self.data[..end][start..]).unwrap()) } } } fn size_hint(&self) -> (usize, Option<usize>) { let l = match self.offset_size { OffsetSize::U0 => 0, _ => (self.offsets.len() - self.offset_idx) / self.offset_size as usize, }; (l, Some(l)) } } impl<'a, Item: Cast + ?Sized> ExactSizeIterator for NonFixedWidthArrayIterator<'a, Item> {} impl<'a, Item: Cast + 'static + ?Sized> IntoIterator for &'a NonFixedWidthArray<Item> { type Item = &'a Item; type IntoIter = NonFixedWidthArrayIterator<'a, Item>; fn into_iter(self) -> Self::IntoIter { let (osz, lfo) = read_last_frame_offset(&self.data); let (data, offsets) = self.data.split_at(lfo); NonFixedWidthArrayIterator { data, offsets, next_start: 0, offset_idx: 0, offset_size: osz, } } } impl<Item: Cast + 'static + ?Sized> core::ops::Index<usize> for NonFixedWidthArray<Item> { type Output = Item; fn index(&self, index: usize) -> &Self::Output { let (osz, lfo) = read_last_frame_offset(&self.data); let frame_offsets = &self.data.as_ref()[lfo..]; let end = read_uint(frame_offsets, osz, index); let start = align_offset::<Item::AlignOf>(match index { 0 => 0, x => read_uint(frame_offsets, osz, x - 1), }); if start < self.data.len() && end <= lfo && start <= end { Item::try_from_aligned_slice(&self.data[..end][start..]).unwrap() } else { // Start or End Boundary of a Child Falls Outside the Container // // If the framing offsets (or calculations based on them) indicate // that any part of the byte sequence of a child value would fall // outside of the byte sequence of the parent then the child is given // the default value for its type. Item::try_from_aligned_slice(aligned_bytes::empty_aligned()).unwrap() } } } impl<GvT: Cast + ?Sized, It: IntoIterator> SerializeTo<NonFixedWidthArray<GvT>> for It where It::Item: SerializeTo<GvT>, { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { let mut bytes_written = 0; let mut offsets = vec![]; for x in self.into_iter() { bytes_written += x.serialize(f)?; offsets.push(bytes_written); let padding = align_offset::<GvT::AlignOf>(bytes_written).to_usize() - bytes_written; f.write_all(&b"\0\0\0\0\0\0\0"[..padding])?; bytes_written += padding; } write_offsets(bytes_written, offsets.as_ref(), f) } } fn write_offsets( mut bytes_written: usize, offsets: &[usize], f: &mut impl Write, ) -> std::io::Result<usize> { if bytes_written + offsets.len() <= 0xff { bytes_written += offsets.len(); for offset in offsets { f.write_all((*offset as u8).to_le_bytes().as_ref())?; } } else if bytes_written + offsets.len() * 2 <= 0xffff { bytes_written += offsets.len() * 2; for offset in offsets { f.write_all((*offset as u16).to_le_bytes().as_ref())?; } } else if bytes_written + offsets.len() * 4 <= 0xffff_ffff { bytes_written += offsets.len() * 4; for offset in offsets { f.write_all((*offset as u32).to_le_bytes().as_ref())?; } } else { bytes_written += offsets.len() * 8; for offset in offsets { f.write_all((*offset as u64).to_le_bytes().as_ref())?; } } Ok(bytes_written) } // 2.5.2 Maybes // // Maybes are encoded differently depending on if their element type is // fixed-sized or not. // // The alignment of a maybe type is always equal to the alignment of its element // type. /// Type with same representation as GVariant "mX" type where X is any fixed /// size type /// /// This is the type returned by: /// /// # use gvariant::{aligned_bytes::empty_aligned, gv, Marker}; /// # let data = empty_aligned(); /// gv!("mb").cast(data); /// # let data = empty_aligned(); /// gv!("mi").cast(data); /// gv!("m(yi)").cast(data); /// /// Rust's built in [`Option`] doesn't have any specified byte representation so /// we need our own type here. /// /// Maybes are encoded differently depending on if their element type is /// fixed-sized or not. [`MaybeNonFixedSize`] is used when the contained size /// is non-fixed, but it implements the same interface as this type /// /// You probably just want to call `.to_option()` on this type. #[repr(transparent)] #[derive(RefCast)] pub struct MaybeFixedSize<T: Cast> { marker: PhantomData<T>, data: AlignedSlice<T::AlignOf>, } #[cfg(feature = "alloc")] impl<T: Cast> ToOwned for MaybeFixedSize<T> { // I'd like this to be Option<T>, but ToOwned requires Owned to be // Borrow<Self>, and we can't implement this because while we're guaranteed // that `T::AlignOf >= mem::align_of<T>`, the inverse is not true. We could // make that guarantee, but then this wouldn't be valid on architectures // where i64 is 32-bit aligned for example. type Owned = Box<Self>; fn to_owned(&self) -> Self::Owned { casting::ref_cast_box(self.data.to_owned()) } } impl<T: Cast + Debug> Debug for MaybeFixedSize<T> { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.to_option().fmt(f) } } impl<T: Cast> MaybeFixedSize<T> { /// Convert to a rust native [`Option`] type. /// /// Note: this doesn't copy the data, it returns an option to a reference to /// the underlying data. pub fn to_option(&self) -> Option<&T> { // 2.5.2.1 Maybe of a Fixed-Sized Element // // For the `Nothing` case, the serialised data is the empty byte // sequence. For the `Just` case, the serialised data is exactly // equal to the serialised data of the child. This is always // distinguishable from the `Nothing` case because all fixed-sized // values have a non-zero size. // // Wrong Size for Fixed Sized Maybe // // In the event that a maybe instance with a fixed element size // is not exactly equal to the size of that element, then the // value is taken to be `Nothing`. T::try_from_aligned_slice(&self.data).ok() } } impl<'a, T: Cast> From<&'a MaybeFixedSize<T>> for Option<&'a T> { fn from(m: &'a MaybeFixedSize<T>) -> Self { m.to_option() } } impl<T: Cast + PartialEq> PartialEq for MaybeFixedSize<T> { fn eq(&self, other: &Self) -> bool { self.to_option() == other.to_option() } } impl<T: Cast + Eq> Eq for MaybeFixedSize<T> {} impl<T: Cast + PartialEq> PartialEq<Option<&T>> for &MaybeFixedSize<T> { fn eq(&self, other: &Option<&T>) -> bool { self.to_option() == *other } } impl<T: Cast + PartialEq> PartialEq<&MaybeFixedSize<T>> for Option<&T> { fn eq(&self, other: &&MaybeFixedSize<T>) -> bool { other == self } } impl<T: Cast + PartialOrd> PartialOrd for MaybeFixedSize<T> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { self.to_option().partial_cmp(&other.to_option()) } } impl<T: Cast + Hash> Hash for MaybeFixedSize<T> { fn hash<H: std::hash::Hasher>(&self, state: &mut H) { self.to_option().hash(state) } } unsafe impl<T: Cast> AlignOf for MaybeFixedSize<T> { type AlignOf = T::AlignOf; } unsafe impl<T: Cast> AllBitPatternsValid for MaybeFixedSize<T> {} impl<T: Cast> Cast for MaybeFixedSize<T> { fn default_ref() -> &'static Self { Self::ref_cast(empty_aligned()) } fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize> { Ok(Self::ref_cast(slice)) } fn try_from_aligned_slice_mut( slice: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize> { Ok(Self::ref_cast_mut(slice)) } } impl<'a, T: Cast> IntoIterator for &'a MaybeFixedSize<T> { type Item = &'a T; type IntoIter = core::option::IntoIter<&'a T>; fn into_iter(self) -> Self::IntoIter { self.to_option().into_iter() } } impl<'a, GvT: Cast + SerializeTo<GvT>> SerializeTo<MaybeFixedSize<GvT>> for &'a MaybeFixedSize<GvT> where &'a GvT: SerializeTo<GvT>, { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { if let Some(x) = self.to_option() { x.serialize(f) } else { Ok(0) } } } impl<GvT: Cast, T: SerializeTo<GvT> + Copy> SerializeTo<MaybeFixedSize<GvT>> for &Option<T> where T: SerializeTo<GvT>, { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { if let Some(x) = self { let len = x.serialize(f)?; f.write_all(b"\0")?; Ok(len + 1) } else { Ok(0) } } } /// Type with same representation as GVariant "mX" type where X is any non-fixed /// size type /// /// This is the type returned by: /// /// # use gvariant::{aligned_bytes::empty_aligned, gv, Marker}; /// # let data = empty_aligned(); /// gv!("ms").cast(data); /// # let data = empty_aligned(); /// gv!("mmi").cast(data); /// gv!("m(ias)").cast(data); /// /// Rust's built in [`Option`] doesn't have any specified byte representation so /// we need our own type here. /// /// Maybes are encoded differently depending on if their element type is /// fixed-sized or not. [`MaybeFixedSize`] is used when the contained size is /// fixed, but it implements the same interface as this type. #[derive(RefCast)] #[repr(transparent)] pub struct MaybeNonFixedSize<T: Cast + ?Sized> { marker: PhantomData<T>, data: AlignedSlice<T::AlignOf>, } impl<T: Cast + Debug + ?Sized> Debug for MaybeNonFixedSize<T> { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { self.to_option().fmt(f) } } #[cfg(feature = "alloc")] impl<T: Cast + ?Sized> ToOwned for MaybeNonFixedSize<T> { type Owned = Box<Self>; fn to_owned(&self) -> Self::Owned { casting::ref_cast_box(self.data.to_owned()) } } impl<T: Cast + ?Sized> MaybeNonFixedSize<T> { /// Convert to a rust native [`Option`] type. /// /// Note: this doesn't copy the data, it returns an option to a reference to /// the underlying data. pub fn to_option(&self) -> Option<&T> { if self.data.is_empty() { // #### 2.5.2.2 Maybe of a Non-Fixed-Sized Element // // For the `Nothing` case, the serialised data is, again, the empty // byte sequence. None } else { // For the Just case, the serialised form is the serialised data of // the child element, followed by a single zero byte. This extra // byte ensures that the `Just` case is distinguishable from the // `Nothing` case even in the event that the child value has a size // of zero. Some(T::try_from_aligned_slice(&self.data[..self.data.len() - 1]).unwrap()) } } } unsafe impl<T: Cast + ?Sized> AlignOf for MaybeNonFixedSize<T> { type AlignOf = T::AlignOf; } unsafe impl<T: Cast + ?Sized> AllBitPatternsValid for MaybeNonFixedSize<T> {} impl<T: Cast + ?Sized> Cast for MaybeNonFixedSize<T> { fn default_ref() -> &'static Self { Self::ref_cast(empty_aligned()) } fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize> { Ok(Self::ref_cast(slice)) } fn try_from_aligned_slice_mut( slice: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize> { Ok(Self::ref_cast_mut(slice)) } } impl<'a, T: Cast + ?Sized> IntoIterator for &'a MaybeNonFixedSize<T> { type Item = &'a T; type IntoIter = core::option::IntoIter<&'a T>; fn into_iter(self) -> Self::IntoIter { self.to_option().into_iter() } } impl<'a, T: Cast + ?Sized> From<&'a MaybeNonFixedSize<T>> for Option<&'a T> { fn from(m: &'a MaybeNonFixedSize<T>) -> Self { m.to_option() } } impl<T: Cast + PartialEq + ?Sized> PartialEq for MaybeNonFixedSize<T> { fn eq(&self, other: &Self) -> bool { self.to_option() == other.to_option() } } impl<T: Cast + Eq + ?Sized> Eq for MaybeNonFixedSize<T> {} impl<T: Cast + PartialEq> PartialEq<Option<&T>> for MaybeNonFixedSize<T> { fn eq(&self, other: &Option<&T>) -> bool { self.to_option() == *other } } impl<T: Cast + PartialEq> PartialEq<MaybeNonFixedSize<T>> for Option<&T> { fn eq(&self, other: &MaybeNonFixedSize<T>) -> bool { other == self } } impl<GvT: Cast + ?Sized, T: IntoIterator> SerializeTo<MaybeNonFixedSize<GvT>> for T where T::Item: SerializeTo<GvT>, { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { if let Some(x) = self.into_iter().next() { let len = x.serialize(f)?; f.write_all(b"\0")?; Ok(len + 1) } else { Ok(0) } } } /// Type with same representation as GVariant "b" type /// /// This is the type returned by: /// /// # use gvariant::{aligned_bytes::AsAligned, gv, Marker}; /// gv!("b").cast(b"\0".as_aligned()); /// /// Rust's built in [`bool`] doesn't have the same representation as GVariant's, /// so we need our own type here. Rust's must either be `0x00` (`false`) or /// `0x01` (`true`), while with GVariant any value in the range `0x01..=0xFF` is /// `true`. #[derive(RefCast, Eq, Copy, Clone)] #[repr(transparent)] pub struct Bool(u8); impl Bool { pub fn to_bool(self) -> bool { self.0 > 0 } } impl Cast for Bool { fn default_ref() -> &'static Self { &Bool(0u8) } fn try_from_aligned_slice( slice: &AlignedSlice<Self::AlignOf>, ) -> Result<&Self, casting::WrongSize> { casting::try_cast_slice_to::<Self>(slice) } fn try_from_aligned_slice_mut( slice: &mut AlignedSlice<Self::AlignOf>, ) -> Result<&mut Self, casting::WrongSize> { casting::try_cast_slice_to_mut::<Self>(slice) } } impl Debug for Bool { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::fmt::Debug::fmt(&self.to_bool(), f) } } unsafe impl AllBitPatternsValid for Bool {} unsafe impl AlignOf for Bool { type AlignOf = aligned_bytes::A1; } impl From<Bool> for bool { fn from(b: Bool) -> Self { b.to_bool() } } impl PartialEq for Bool { fn eq(&self, other: &Self) -> bool { self.to_bool() == other.to_bool() } } impl PartialEq<bool> for Bool { fn eq(&self, other: &bool) -> bool { self.to_bool() == *other } } impl PartialEq<Bool> for bool { fn eq(&self, other: &Bool) -> bool { other == self } } impl SerializeTo<Bool> for &Bool { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { self.to_bool().serialize(f) } } impl SerializeTo<Bool> for &bool { fn serialize(self, f: &mut impl Write) -> std::io::Result<usize> { f.write_all(if *self { b"\x01" } else { b"\x00" })?; Ok(1) } } /// A trait that all generated structure types implement. /// /// This exists mostly to document the interface of the generated types. Don't /// implement this for your own types. /// /// All structures also implement `Into<Self::RefTuple>`. pub trait Structure<'a>: Cast + Debug + casting::AlignOf + casting::AllBitPatternsValid { /// This a tuple of refs, one for each structure element /// /// For **(is)** this will be `(&'a i32, &'a Str)`. type RefTuple; /// Convert this struct to a rust tuple fn to_tuple(&'a self) -> Self::RefTuple; } #[inline] fn nth_last_frame_offset(data: &[u8], osz: crate::OffsetSize, n: usize) -> Option<usize> { if n == 0 { Some(0) } else if let Some(off) = usize::checked_sub(data.len(), n * osz as usize) { Some(read_uint(&data[off..], osz, 0)) } else { None } } #[inline] fn calc_offsets<ChildAlign: aligned_bytes::Alignment, B: aligned_bytes::Alignment>( data: &[u8], i: isize, a: usize, c: usize, size: Option<usize>, last_child: bool, n_frame_offsets: usize, ) -> Option<(aligned_bytes::AlignedOffset<ChildAlign>, usize)> where aligned_bytes::AlignedOffset<B>: Into<aligned_bytes::AlignedOffset<ChildAlign>>, { let osz = offset_size(data.len()); let fo = nth_last_frame_offset(data, osz, (i + 1) as usize)?; let start: aligned_bytes::AlignedOffset<ChildAlign> = align_offset::<B>(fo + a).into() | aligned_bytes::AlignedOffset::<ChildAlign>::try_new(c).unwrap(); let end = if let Some(size) = size { start.to_usize() + size } else if last_child { usize::checked_sub(data.len(), osz as usize * n_frame_offsets)? } else { nth_last_frame_offset(data, osz, (i + 2) as usize)? }; if start <= end && end <= data.len() { Some((start, end)) } else { None } } /// Used for getting children of structures /// /// `i`, `a`, `B` and `c` are described in the GVariant spec section "3.2.2 /// Computing the Table". See gvariant_macro::generate_impl::generate_table. /// /// This is not really public, it's only for use by the code generated by our /// macro. #[doc(hidden)] #[inline] pub fn get_child_elem<T: Cast + ?Sized, B: aligned_bytes::Alignment>( data: &AlignedSlice<<T as AlignOf>::AlignOf>, i: isize, a: usize, c: usize, child_size: Option<usize>, last_child: bool, n_frame_offsets: usize, ) -> &T where aligned_bytes::AlignedOffset<B>: Into<aligned_bytes::AlignedOffset<T::AlignOf>>, { match calc_offsets::<<T as AlignOf>::AlignOf, B>( data, i, a, c, child_size, last_child, n_frame_offsets, ) { Some((start, end)) => &T::from_aligned_slice(&data[..end][start..]), None => &T::default_ref(), } } #[cfg(test)] mod tests { use super::*; use aligned_bytes::{copy_to_align, AlignedSlice, AsAligned, A8}; #[test] fn test_numbers() { let data = copy_to_align(&[1, 2, 3, 4, 5, 6, 7, 8, 9]); let aligned_slice: &AlignedSlice<A8> = data.as_ref(); // If the size doesn't match exactly it should default to 0: assert_eq!( *i32::from_aligned_slice(&aligned_slice[..0].as_aligned()), 0 ); assert_eq!( *i32::from_aligned_slice(&aligned_slice[..3].as_aligned()), 0 ); assert_eq!( *i32::from_aligned_slice(&aligned_slice[..5].as_aligned()), 0 ); assert_eq!( *i32::from_aligned_slice(&aligned_slice[..8].as_aligned()), 0 ); // Common case (Little endian): assert_eq!( Bool::from_aligned_slice(&aligned_slice[..1].as_aligned()).to_bool(), true ); assert_eq!( *u8::from_aligned_slice(&aligned_slice[..1].as_aligned()), 0x01 ); assert_eq!( *i16::from_aligned_slice(&aligned_slice[..2].as_aligned()), 0x0201 ); assert_eq!( *u16::from_aligned_slice(&aligned_slice[..2].as_aligned()), 0x0201 ); assert_eq!( *i32::from_aligned_slice(&aligned_slice[..4].as_aligned()), 0x04030201 ); assert_eq!( *u32::from_aligned_slice(&aligned_slice[..4].as_aligned()), 0x04030201 ); assert_eq!( *i64::from_aligned_slice(&aligned_slice[..8]), 0x0807060504030201 ); assert_eq!( *u64::from_aligned_slice(&aligned_slice[..8]), 0x0807060504030201 ); assert_eq!( *f64::from_aligned_slice(&aligned_slice[..8]), f64::from_bits(0x0807060504030201) ); } #[test] fn test_non_fixed_width_maybe() { assert_eq!( MaybeNonFixedSize::<Str>::from_aligned_slice(b"".as_aligned()).to_option(), None ); assert_eq!( MaybeNonFixedSize::<Str>::from_aligned_slice(b"\0".as_aligned()) .to_option() .unwrap(), "" ); assert_eq!( MaybeNonFixedSize::<Str>::from_aligned_slice(b"hello world\0\0".as_aligned()) .to_option() .unwrap(), "hello world" ); } #[test] fn test_fixed_width_maybe() { assert_eq!( MaybeFixedSize::<u8>::from_aligned_slice(b"".as_aligned()), None ); assert_eq!( MaybeFixedSize::<u8>::from_aligned_slice(b"\x43".as_aligned()), Some(&0x43) ); assert_eq!( MaybeFixedSize::<u8>::from_aligned_slice(b"\x43\0".as_aligned()), None ); } #[test] fn test_non_fixed_width_array() { let a_s = NonFixedWidthArray::<Str>::from_aligned_slice(b"".as_aligned()); assert_eq!(a_s.len(), 0); assert!(a_s.is_empty()); assert_eq!(a_s.first(), None); assert_eq!(a_s.last(), None); assert!(a_s.into_iter().collect::<Vec<_>>().is_empty()); assert_eq!(a_s.iter().size_hint(), (0, Some(0))); let a_s = NonFixedWidthArray::<Str>::from_aligned_slice(b"hello\0world\0\x06\x0c".as_aligned()); assert_eq!(a_s.len(), 2); assert_eq!( a_s.into_iter().map(|x| x.to_str()).collect::<Vec<_>>(), &["hello", "world"] ); assert_eq!(&a_s[0], "hello"); assert_eq!(&a_s[1], "world"); assert!(!a_s.is_empty()); assert_eq!(a_s.first().unwrap(), "hello"); assert_eq!(a_s.last().unwrap(), "world"); let mut it = a_s.iter(); assert_eq!(it.size_hint(), (2, Some(2))); it.next(); assert_eq!(it.size_hint(), (1, Some(1))); it.next(); assert_eq!(it.size_hint(), (0, Some(0))); // Non-normal regression test found by fuzzing: let nfwa = NonFixedWidthArray::<[u8]>::from_aligned_slice(b"\x08".as_aligned()); let v = assert_array_self_consistent(nfwa); assert_eq!(v.as_slice(), &[] as &[&[u8]]); // Non-normal regression test found by fuzzing: let nfwa = NonFixedWidthArray::<[u8]>::from_aligned_slice(b"\x01\x00".as_aligned()); let v = assert_array_self_consistent(nfwa); assert_eq!(v, [&[] as &[u8], &[]]); // Non-normal regression test found by fuzzing. There are a non-integral // number of array elements indicated. 1.5 in this case: let mut data = [0u8; 258]; data[256..].copy_from_slice(&255u16.to_le_bytes()); let cow = copy_to_align(&data); let nfwa = NonFixedWidthArray::<[u8]>::from_aligned_slice(cow.as_ref()); let v = assert_array_self_consistent(nfwa); assert_eq!(v.as_slice(), &[] as &[&[u8]]); } fn assert_array_self_consistent<T: Cast + ?Sized>(a: &NonFixedWidthArray<T>) -> Vec<&T> { let v: Vec<_> = a.iter().collect(); assert_eq!(a.len(), v.len()); for (n, elem) in v.iter().enumerate() { assert_eq!(**elem, a[n]); } assert!(a.iter().len() == a.len()); v } #[test] #[should_panic] fn test_non_fixed_width_array_panic() { // Non-normal regression test found by fuzzing: let nfwa = NonFixedWidthArray::<[u8]>::from_aligned_slice(b"\x08".as_aligned()); &nfwa[0]; } #[test] fn test_spec_examples() { assert_eq!(&*gv!("s").from_bytes(b"hello world\0"), "hello world"); assert_eq!(gv!("s").serialize_to_vec("hello world"), b"hello world\0"); assert_eq!( gv!("ms") .from_bytes(b"hello world\0\0") .to_option() .unwrap(), "hello world" ); assert_eq!( gv!("ms").serialize_to_vec(&Some("hello world")), b"hello world\0\0" ); assert_eq!( gv!("ab").cast(b"\x01\x00\x00\x01\x01".as_aligned()), [true, false, false, true, true] ); assert_eq!( gv!("ab").serialize_to_vec(&[true, false, false, true, true][..]), b"\x01\x00\x00\x01\x01" ); // String Array Example // // With type 'as': let a = gv!("as").from_bytes(b"i\0can\0has\0strings?\0\x02\x06\x0a\x13"); assert_array_self_consistent(&*a); assert_eq!(*a, ["i", "can", "has", "strings?"][..]); assert_eq!( gv!("as") .serialize_to_vec(&["i", "can", "has", "strings?"][..]) .as_slice(), b"i\0can\0has\0strings?\0\x02\x06\x0a\x13" ); // Array of Bytes Example // // With type 'ay': let aob = gv!("ay").cast([0x04u8, 0x05, 0x06, 0x07].as_aligned()); assert_eq!(aob, &[0x04u8, 0x05, 0x06, 0x07]); assert_eq!( gv!("ay") .serialize_to_vec(&[0x04u8, 0x05, 0x06, 0x07]) .as_slice(), &[0x04u8, 0x05, 0x06, 0x07] ); // Array of Integers Example // // With type 'ai': assert_eq!(gv!("ai").from_bytes(b"\x04\0\0\0\x02\x01\0\0"), [4, 258]); assert_eq!( gv!("ai").serialize_to_vec(&[4, 258]).as_slice(), b"\x04\0\0\0\x02\x01\0\0" ); // Dictionary Entry Example // // With type '{si}': // 'a sp 'k 'e 'y \0 -- -- 02 02 00 00 06has a value of {'a key', 514} } #[test] fn test_gvariantstr() { assert_eq!(Str::from_aligned_slice(b"".as_aligned()).to_str(), ""); assert_eq!(Str::from_aligned_slice(b"\0".as_aligned()).to_str(), ""); assert_eq!( Str::from_aligned_slice(b"hello world\0".as_aligned()).to_str(), "hello world" ); assert_eq!( Str::from_aligned_slice(b"hello world\0".as_aligned()), "hello world" ); } #[test] fn test_variant() { let data = copy_to_align(b"\x04\x00\x00n"); let v = Variant::from_aligned_slice(data.as_ref()); match v.split() { (b"n", d) => assert_eq!(*i16::from_aligned_slice(d.as_aligned()), 4), (ty, _) => panic!("Incorrect type {:?}", ty), } assert_eq!(v, v); let data_1 = copy_to_align(b"\x00()"); let data_2 = copy_to_align(b""); assert_eq!( Variant::from_aligned_slice(data_1.as_ref()), Variant::from_aligned_slice(data_2.as_ref()) ); let non_normal = Variant::from_aligned_slice(data_1.as_ref()); assert_ne!(non_normal, v); } }