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
//! Traits that apply to types which can safely interact with Müsli's zero copy
//! system.
//!
//! Note that all of these traits are `unsafe`, and require care to implement.
//! Please see their corresponding safety documentation or use the
//! [`ZeroCopy`][derive@crate::ZeroCopy] derive.
//!
//! * [`ZeroCopy`] for types which can safely be coerced from a [`Buf`] to
//! `&Self` or `&mut Self`.
//! * [`UnsizedZeroCopy`] for types which can safely be coerced from an
//! [`Unsized<T>`] to `&T` or `&mut T`.
//! * [`ZeroSized`] for types which can be ingored when deriving
//! [`ZeroCopy`][derive@crate::ZeroCopy] using `#[zero_copy(ignore)]`.
//!
//! [`Unsized<T>`]: crate::pointer::Unsized
#![allow(clippy::missing_safety_doc)]
use core::marker::PhantomData;
use core::mem::{align_of, size_of, size_of_val};
use core::num::Wrapping;
use core::slice;
use core::str;
use crate::buf::{Buf, BufMut, Padder, Validator, Visit};
use crate::error::{Error, ErrorKind};
mod sealed {
pub trait Sealed {}
impl Sealed for str {}
}
/// Trait governing which `T` in [`Unsized<T>`] the wrapper can handle.
///
/// We only support slice-like, unaligned unsized types, such as `str` and
/// `[u8]`. We can't support types such as `dyn Debug` because metadata is a
/// vtable which can't be serialized.
///
/// For nested slices or arrays, use [`Slice<T>`] instead.
///
/// [`Unsized<T>`]: crate::pointer::Unsized
/// [`Slice<T>`]: crate::pointer::Slice
///
/// # Safety
///
/// This can only be implemented by types that:
/// * Can only be implemented for base types which can inhabit any bit-pattern.
/// All though custom validation can be performed during coercion (such as for
/// `str`).
/// * Must only be implemented for types which are not padded (as per
/// [`ZeroCopy::PADDED`]).
///
/// # Examples
///
/// ```
/// use musli_zerocopy::OwnedBuf;
///
/// let mut buf = OwnedBuf::with_alignment::<u8>();
///
/// let bytes = buf.store_unsized(&b"Hello World!"[..]);
/// let buf = buf.as_ref();
/// assert_eq!(buf.load(bytes)?, b"Hello World!");
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
pub unsafe trait UnsizedZeroCopy: self::sealed::Sealed {
/// Alignment of the pointed to data. We can only support unsized types
/// which have a known alignment.
///
/// # Safety
///
/// This must be a power of two.
const ALIGN: usize;
/// The size of the base type.
const SIZE: usize;
/// The size of the unsized value.
fn size(&self) -> usize;
/// The size in bytes of the unsized value.
fn bytes(&self) -> usize;
/// Write to the owned buffer.
///
/// This is usually called indirectly through methods such as
/// [`OwnedBuf::store_unsized`].
///
/// [`OwnedBuf::store_unsized`]: crate::buf::OwnedBuf::store_unsized
unsafe fn store(&self, buf: &mut BufMut<'_>);
/// Validate and coerce the buffer as this type.
///
/// # Safety
///
/// The caller is responsible for ensuring that the pointer is valid up to
/// the reported size of `Self`. If `Self` is `[T]` then `size` is the
/// length of the `T`-containing slice.
unsafe fn coerce(buf: *const u8, size: usize) -> Result<*const Self, Error>;
/// Validate and coerce the buffer as this type mutably.
///
/// # Safety
///
/// The caller is responsible for ensuring that the pointer is valid up to
/// the reported size of `Self`. If `Self` is `[T]` then `size` is the
/// length of the `T`-containing slice.
unsafe fn coerce_mut(buf: *mut u8, size: usize) -> Result<*mut Self, Error>;
}
/// This is a marker trait that must be implemented for a type in order to use
/// the `#[zero_copy(ignore)]` attribute when deriving the [`ZeroCopy`] trait.
///
/// Using the attribute incorrectly might lead to unsoundness.
///
/// # Safety
///
/// Any type implementing this trait must be zero-sized.
///
/// # Examples
///
/// Using `#[zero_copy(ignore)]`` on generic fields that implements
/// [`ZeroSized`]:
///
/// ```
/// use musli_zerocopy::ZeroCopy;
/// use musli_zerocopy::traits::ZeroSized;
///
/// #[derive(ZeroCopy)]
/// #[repr(transparent)]
/// struct Struct<T> where T: ZeroSized {
/// #[zero_copy(ignore)]
/// field: T,
/// }
/// ```
///
/// Types which derive [`ZeroCopy`] also implement [`ZeroSized`] if they are
/// zero-sized:
///
/// ```
/// use std::marker::PhantomData;
/// use std::mem::size_of;
/// use musli_zerocopy::ZeroCopy;
/// use musli_zerocopy::traits::ZeroSized;
///
/// #[derive(ZeroCopy)]
/// #[repr(transparent)]
/// struct Struct<T> where T: ZeroSized {
/// #[zero_copy(ignore)]
/// field: T,
/// }
///
/// #[derive(ZeroCopy)]
/// #[repr(transparent)]
/// struct OtherStruct {
/// #[zero_copy(ignore)]
/// field: Struct<()>,
/// }
///
/// fn assert_zero_sized<T: ZeroSized>() {
/// assert_eq!(size_of::<T>(), 0);
/// }
///
/// assert_zero_sized::<()>();
/// assert_zero_sized::<PhantomData<u32>>();
/// assert_zero_sized::<OtherStruct>();
/// assert_zero_sized::<Struct<OtherStruct>>();
/// ```
pub unsafe trait ZeroSized {}
/// [`ZeroCopy`] implementation for `Wrapping<T>`.
///
/// # Examples
///
/// ```
/// use std::num::Wrapping;
///
/// use musli_zerocopy::{buf, Ref, ZeroCopy};
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// struct Struct {
/// field: Wrapping<u32>,
/// }
///
/// let zero = u32::to_ne_bytes(0);
/// let zero = buf::aligned_buf::<u32>(&zero);
/// let one = u32::to_ne_bytes(1);
/// let one = buf::aligned_buf::<u32>(&one);
///
/// let st = zero.load(Ref::<Struct>::zero())?;
/// assert_eq!(st.field.0, 0);
///
/// let st = one.load(Ref::<Struct>::zero())?;
/// assert_eq!(st.field.0, 1);
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
// SAFETY: `Wrapping<T>` is repr-transparent.
unsafe impl<T> ZeroSized for Wrapping<T> where T: ZeroSized {}
unsafe impl<T> ZeroCopy for Wrapping<T>
where
T: Copy + ZeroCopy,
{
const ANY_BITS: bool = T::ANY_BITS;
const PADDED: bool = T::PADDED;
#[inline]
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>) {
T::store_unaligned(this.cast(), buf);
}
#[inline]
unsafe fn pad(this: *const Self, padder: &mut Padder<'_, Self>) {
padder.pad(this.cast::<T>());
}
#[inline]
unsafe fn validate(validator: &mut Validator<'_, Self>) -> Result<(), Error> {
validator.validate::<T>()
}
}
/// `()` can be ignored as a zero-sized field.
///
/// # Examples
///
/// ```
/// use musli_zerocopy::ZeroCopy;
///
/// #[derive(ZeroCopy)]
/// #[repr(transparent)]
/// struct Struct {
/// #[zero_copy(ignore)]
/// field: (),
/// }
/// ```
// SAFETY: `()` is zero-sized.
unsafe impl ZeroSized for () {}
/// `[T; 0]` can be ignored as a zero-sized field.
///
/// # Examples
///
/// ```
/// use musli_zerocopy::ZeroCopy;
///
/// #[derive(ZeroCopy)]
/// #[repr(transparent)]
/// struct Struct<T> {
/// #[zero_copy(ignore)]
/// field: [T; 0],
/// }
/// ```
// SAFETY: `[T; 0]` is zero-sized.
unsafe impl<T> ZeroSized for [T; 0] {}
/// `PhantomData<T>` can be ignored as a zero-sized field.
///
/// # Examples
///
/// ```
/// use std::marker::PhantomData;
/// use musli_zerocopy::ZeroCopy;
///
/// #[derive(ZeroCopy)]
/// #[repr(transparent)]
/// struct Struct<T> {
/// #[zero_copy(ignore)]
/// field: PhantomData<T>,
/// }
/// ```
// SAFETY: `PhantomData<T>` is zero-sized.
unsafe impl<T: ?Sized> ZeroSized for PhantomData<T> {}
/// Trait governing types can be safely coerced into a reference from a buffer.
///
/// It is not recommended to implement this trait manually, instead rely on the
/// [`ZeroCopy`] derive.
///
/// [`ZeroCopy`]: derive@crate::ZeroCopy
///
/// # Safety
///
/// This can only be implemented correctly by types under certain conditions:
/// * The type has a strict, well-defined layout like `repr(C)` or an enum with
/// `repr(u32)`.
/// * It's size and alignment must be known statically as per [`size_of`] and
/// [`align_of`]. This excludes enums which are `#[repr(C)]` because for
/// example their alignment depends on the range of values they can represent.
///
/// [`size_of`]: core::mem::size_of
///
/// # Notable types which cannot be `ZeroCopy`
///
/// Any type which does not have an explicit representation cannot implement
/// `ZeroCopy`. Most Rust types use the Rust. Or `#[repr(Rust)]`. The Rust as a
/// language is allowed to make arbitrary layout decisions for `#[repr(Rust)]`
/// types.
///
/// The following is a list of common Rust types which *cannot* implements
/// `ZeroCopy`, and the rationale for why:
///
/// * Non-zero sized tuples. Since tuples do not have a stable layout.
/// * `Option<T>` since that is a `#[repr(Rust)]` type, except where [specific
/// representation guarantees] are made such as with `Option<NonZero*>` types.
///
/// [specific representation guarantees]:
/// https://doc.rust-lang.org/std/option/index.html#representation
///
/// # Examples
///
/// ```
/// use musli_zerocopy::{OwnedBuf, ZeroCopy};
///
/// #[derive(Debug, PartialEq, ZeroCopy)]
/// #[repr(C)]
/// struct Custom { field: u32, #[zero_copy(ignore)] ignore: () }
///
/// let mut buf = OwnedBuf::new();
/// let ptr = buf.store(&Custom { field: 42, ignore: () });
/// let buf = buf.into_aligned();
/// assert_eq!(buf.load(ptr)?, &Custom { field: 42, ignore: () });
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
pub unsafe trait ZeroCopy: Sized {
/// Indicates if the type can inhabit all possible bit patterns within its
/// `size_of::<Self>()` bytes.
const ANY_BITS: bool;
/// Indicates that a type needs padding in case it is stored in an array
/// that is aligned to `align_of::<Self>()`.
const PADDED: bool;
/// Store the current unaligned value to the mutable buffer.
///
/// This is usually called indirectly through methods such as
/// [`OwnedBuf::store`].
///
/// [`OwnedBuf::store`]: crate::buf::OwnedBuf::store
///
/// # Safety
///
/// The implementor is responsible for ensuring that the struct has a
/// well-defined layout, and that the struct is stored in the passed in
/// `buf` correctly, which includes enumerating every field including hidden
/// ones.
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>);
/// Mark padding for the current type.
///
/// The `this` receiver takes the current type as pointer instead of a
/// reference, because it might not be aligned in the case of packed
/// types.
///
/// # Safety
///
/// The implementor is responsible for ensuring that every field is provided
/// to `padder`, including potentially hidden ones.
unsafe fn pad(this: *const Self, padder: &mut Padder<'_, Self>);
/// Validate the current type.
///
/// # Safety
///
/// This assumes that the provided validator is wrapping a buffer that is
/// appropriately sized and aligned.
unsafe fn validate(validator: &mut Validator<'_, Self>) -> Result<(), Error>;
}
unsafe impl UnsizedZeroCopy for str {
const ALIGN: usize = align_of::<u8>();
const SIZE: usize = size_of::<u8>();
#[inline]
fn size(&self) -> usize {
<str>::len(self)
}
#[inline]
fn bytes(&self) -> usize {
size_of_val(self)
}
#[inline]
unsafe fn store(&self, buf: &mut BufMut<'_>) {
buf.store_bytes(self.as_bytes());
}
#[inline]
unsafe fn coerce(buf: *const u8, size: usize) -> Result<*const Self, Error> {
let buf = slice::from_raw_parts(buf, size);
Ok(str::from_utf8(buf).map_err(|error| Error::new(ErrorKind::Utf8Error { error }))?)
}
#[inline]
unsafe fn coerce_mut(buf: *mut u8, size: usize) -> Result<*mut Self, Error> {
let buf = slice::from_raw_parts_mut(buf, size);
Ok(str::from_utf8_mut(buf).map_err(|error| Error::new(ErrorKind::Utf8Error { error }))?)
}
}
/// Macro to implement `UnsizedZeroCopy`.
///
/// Its requirements are the following:
/// * Can only be implemented for types which can inhabit any bit-pattern.
/// * Must only be implemented for types which are not padded (as per
/// [`ZeroCopy::PADDED`]).
macro_rules! impl_unsized_primitive {
({$($param:ident)?}, $ty:ty, $example_ty:ty, $example:expr $(, $import:path)?) => {
impl $(<$param>)* self::sealed::Sealed for [$ty] {}
#[doc = concat!("[`UnsizedZeroCopy`] implementation for [", stringify!($ty), "]")]
///
/// # Examples
///
/// ```
$(#[doc = concat!("use ", stringify!($import), ";")])*
/// use musli_zerocopy::{OwnedBuf, ZeroCopy};
/// use musli_zerocopy::pointer::Unsized;
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
#[doc = concat!("struct Custom", stringify!($(<$param>)*) ," { field: Unsized<[", stringify!($ty) ,"]> }")]
///
/// let mut buf = OwnedBuf::new();
#[doc = concat!("let unsize: Unsized<[", stringify!($example_ty), "]> = buf.store_unsized(&", stringify!($example), ");")]
/// let buf = buf.into_aligned();
#[doc = concat!("assert_eq!(buf.load(unsize)?, &", stringify!($example), ");")]
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
#[allow(rustdoc::invalid_html_tags)]
unsafe impl $(<$param>)* UnsizedZeroCopy for [$ty] {
const ALIGN: usize = align_of::<$ty>();
const SIZE: usize = size_of::<$ty>();
#[inline]
fn size(&self) -> usize {
self.len()
}
#[inline]
fn bytes(&self) -> usize {
size_of_val(self)
}
#[inline]
unsafe fn store(&self, buf: &mut BufMut<'_>) {
buf.store_bytes(self);
}
#[inline]
unsafe fn coerce(buf: *const u8, size: usize) -> Result<*const Self, Error> {
Ok(slice::from_raw_parts(buf.cast(), size))
}
#[inline]
unsafe fn coerce_mut(buf: *mut u8, size: usize) -> Result<*mut Self, Error> {
Ok(slice::from_raw_parts_mut(buf.cast(), size))
}
}
};
}
impl_unsized_primitive!({}, u8, u8, [u8::MIN, 1, 2, 3, 4, u8::MAX]);
impl_unsized_primitive!({}, i8, i8, [i8::MIN, -1, 2, -3, 4, i8::MAX]);
impl_unsized_primitive!({}, u16, u16, [u16::MIN, 1, 2, 3, 4, u16::MAX]);
impl_unsized_primitive!({}, i16, i16, [i16::MIN, -1, 2, -3, 4, i16::MAX]);
impl_unsized_primitive!({}, u32, u32, [u32::MIN, 1, 2, 3, 4, u32::MAX]);
impl_unsized_primitive!({}, i32, i32, [i32::MIN, -1, 2, -3, 4, i32::MAX]);
impl_unsized_primitive!({}, u64, u64, [u64::MIN, 1, 2, 3, 4, u64::MAX]);
impl_unsized_primitive!({}, i64, i64, [i64::MIN, -1, 2, -3, 4, i64::MAX]);
impl_unsized_primitive!({}, u128, u128, [u128::MIN, 1, 2, 3, 4, u128::MAX]);
impl_unsized_primitive!({}, i128, i128, [i128::MIN, -1, 2, -3, 4, i128::MAX]);
impl_unsized_primitive!({}, (), (), [(), ()]);
impl_unsized_primitive!(
{ T },
PhantomData<T>,
PhantomData<u32>,
[PhantomData, PhantomData],
std::marker::PhantomData
);
macro_rules! impl_number {
($ty:ty) => {
#[doc = concat!(" [`ZeroCopy`] implementation for `", stringify!($ty), "`")]
///
/// # Examples
///
/// ```
/// use std::slice;
/// use std::mem::size_of;
/// use musli_zerocopy::{buf, Ref, ZeroCopy};
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// struct Struct {
#[doc = concat!(" field: ", stringify!($ty), ",")]
/// }
///
#[doc = concat!("let zero: ", stringify!($ty), " = 0;")]
#[doc = concat!("let one: ", stringify!($ty), " = 1;")]
///
#[doc = concat!("let zero = ", stringify!($ty), "::to_ne_bytes(0);")]
#[doc = concat!("let zero = buf::aligned_buf::<", stringify!($ty), ">(&zero);")]
#[doc = concat!("let one = ", stringify!($ty), "::to_ne_bytes(1);")]
#[doc = concat!("let one = buf::aligned_buf::<", stringify!($ty), ">(&one);")]
///
/// let st = zero.load(Ref::<Struct>::new(0))?;
/// assert_eq!(st.field, 0);
///
/// let st = one.load(Ref::<Struct>::new(0))?;
/// assert_eq!(st.field, 1);
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
unsafe impl ZeroCopy for $ty {
const ANY_BITS: bool = true;
const PADDED: bool = false;
#[inline]
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>) {
buf.store_unaligned(this);
}
#[inline]
unsafe fn pad(_: *const Self, _: &mut Padder<'_, Self>) {}
#[inline]
unsafe fn validate(_: &mut Validator<'_, Self>) -> Result<(), Error> {
Ok(())
}
}
impl Visit for $ty {
type Target = $ty;
#[inline]
fn visit<V, O>(&self, _: &Buf, visitor: V) -> Result<O, Error>
where
V: FnOnce(&Self::Target) -> O,
{
Ok(visitor(self))
}
}
};
}
impl_number!(usize);
impl_number!(isize);
impl_number!(u8);
impl_number!(u16);
impl_number!(u32);
impl_number!(u64);
impl_number!(u128);
impl_number!(i8);
impl_number!(i16);
impl_number!(i32);
impl_number!(i64);
impl_number!(i128);
macro_rules! impl_float {
($ty:ty) => {
unsafe impl ZeroCopy for $ty {
const ANY_BITS: bool = true;
const PADDED: bool = false;
#[inline]
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>) {
buf.store_unaligned(this);
}
#[inline]
unsafe fn pad(_: *const Self, _: &mut Padder<'_, Self>) {}
#[inline]
unsafe fn validate(_: &mut Validator<'_, Self>) -> Result<(), Error> {
Ok(())
}
}
impl Visit for $ty {
type Target = $ty;
#[inline]
fn visit<V, O>(&self, _: &Buf, visitor: V) -> Result<O, Error>
where
V: FnOnce(&Self::Target) -> O,
{
Ok(visitor(self))
}
}
};
}
impl_float!(f32);
impl_float!(f64);
unsafe impl ZeroCopy for char {
const ANY_BITS: bool = false;
const PADDED: bool = false;
#[inline]
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>) {
buf.store_unaligned(this.cast::<u32>());
}
#[inline]
unsafe fn pad(_: *const Self, _: &mut Padder<'_, Self>) {}
#[allow(clippy::missing_safety_doc)]
#[inline]
unsafe fn validate(validator: &mut Validator<'_, Self>) -> Result<(), Error> {
let repr = validator.load_unaligned::<u32>()?;
if char::try_from(repr).is_err() {
return Err(Error::new(ErrorKind::IllegalChar { repr }));
}
Ok(())
}
}
impl Visit for char {
type Target = char;
#[inline]
fn visit<V, O>(&self, _: &Buf, visitor: V) -> Result<O, Error>
where
V: FnOnce(&Self::Target) -> O,
{
Ok(visitor(self))
}
}
unsafe impl ZeroCopy for bool {
const ANY_BITS: bool = false;
const PADDED: bool = false;
#[inline]
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>) {
buf.store_unaligned(this.cast::<u8>());
}
#[inline]
unsafe fn pad(_: *const Self, _: &mut Padder<'_, Self>) {}
#[allow(clippy::missing_safety_doc)]
#[inline]
unsafe fn validate(validator: &mut Validator<'_, Self>) -> Result<(), Error> {
match validator.byte() {
0 | 1 => (),
repr => return Err(Error::new(ErrorKind::IllegalBool { repr })),
}
Ok(())
}
}
impl Visit for bool {
type Target = bool;
#[inline]
fn visit<V, O>(&self, _: &Buf, visitor: V) -> Result<O, Error>
where
V: FnOnce(&Self::Target) -> O,
{
Ok(visitor(self))
}
}
macro_rules! impl_nonzero_number {
($ty:ident, $inner:ty) => {
#[doc = concat!(" [`ZeroCopy`] implementation for `", stringify!($ty), "`")]
///
/// # Examples
///
/// ```
#[doc = concat!("use std::num::", stringify!($ty), ";")]
/// use std::slice;
/// use std::mem::size_of;
/// use musli_zerocopy::{buf, Ref, ZeroCopy};
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// struct Struct {
#[doc = concat!(" field: ", stringify!($ty), ",")]
/// }
///
#[doc = concat!("let zero = ", stringify!($inner), "::to_ne_bytes(0);")]
#[doc = concat!("let zero = buf::aligned_buf::<", stringify!($ty), ">(&zero);")]
#[doc = concat!("let one = ", stringify!($inner), "::to_ne_bytes(1);")]
#[doc = concat!("let one = buf::aligned_buf::<", stringify!($ty), ">(&one);")]
///
/// // Non-zero buffer works as expected.
/// let st = one.load(Ref::<Struct>::zero())?;
/// assert_eq!(st.field.get(), 1);
///
/// // Trying to use a zeroed buffer with a non-zero type.
/// assert!(zero.load(Ref::<Struct>::zero()).is_err());
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
unsafe impl ZeroCopy for ::core::num::$ty {
const ANY_BITS: bool = false;
const PADDED: bool = false;
#[inline]
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>) {
buf.store_unaligned(this.cast::<$inner>());
}
#[inline]
unsafe fn pad(_: *const Self, _: &mut Padder<'_, Self>) {}
#[inline]
unsafe fn validate(validator: &mut Validator<'_, Self>) -> Result<(), Error> {
if validator.load_unaligned::<$inner>()? == 0 {
return Err(Error::new(ErrorKind::NonZeroZeroed {
range: validator.range::<::core::num::$ty>(),
}));
}
Ok(())
}
}
impl Visit for ::core::num::$ty {
type Target = ::core::num::$ty;
#[inline]
fn visit<V, O>(&self, _: &Buf, visitor: V) -> Result<O, Error>
where
V: FnOnce(&Self::Target) -> O,
{
Ok(visitor(self))
}
}
#[doc = concat!(" [`ZeroCopy`] implementation for `Option<", stringify!($ty), ">`")]
///
/// # Examples
///
/// ```
#[doc = concat!("use std::num::", stringify!($ty), ";")]
/// use std::slice;
/// use std::mem::size_of;
/// use musli_zerocopy::{buf, Ref, ZeroCopy};
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// struct Struct {
#[doc = concat!(" field: Option<", stringify!($ty), ">,")]
/// }
///
#[doc = concat!("let zero = ", stringify!($inner), "::to_ne_bytes(0);")]
#[doc = concat!("let zero = buf::aligned_buf::<", stringify!($ty), ">(&zero);")]
#[doc = concat!("let one = ", stringify!($inner), "::to_ne_bytes(1);")]
#[doc = concat!("let one = buf::aligned_buf::<", stringify!($ty), ">(&one);")]
///
/// let st = zero.load(Ref::<Struct>::new(0))?;
/// assert_eq!(st.field, None);
///
/// let st = one.load(Ref::<Struct>::new(0))?;
#[doc = concat!("assert_eq!(st.field, ", stringify!($ty), "::new(1));")]
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
unsafe impl ZeroCopy for Option<::core::num::$ty> {
const ANY_BITS: bool = true;
const PADDED: bool = false;
#[inline]
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>) {
buf.store_unaligned(this.cast::<$inner>());
}
#[inline]
unsafe fn pad(_: *const Self, _: &mut Padder<'_, Self>) {}
#[inline]
unsafe fn validate(_: &mut Validator<'_, Self>) -> Result<(), Error> {
Ok(())
}
}
impl Visit for Option<::core::num::$ty> {
type Target = Option<::core::num::$ty>;
#[inline]
fn visit<V, O>(&self, _: &Buf, visitor: V) -> Result<O, Error>
where
V: FnOnce(&Self::Target) -> O,
{
Ok(visitor(self))
}
}
};
}
impl_nonzero_number!(NonZeroUsize, usize);
impl_nonzero_number!(NonZeroIsize, isize);
impl_nonzero_number!(NonZeroU8, u8);
impl_nonzero_number!(NonZeroU16, u16);
impl_nonzero_number!(NonZeroU32, u32);
impl_nonzero_number!(NonZeroU64, u64);
impl_nonzero_number!(NonZeroU128, u128);
impl_nonzero_number!(NonZeroI8, i8);
impl_nonzero_number!(NonZeroI16, i16);
impl_nonzero_number!(NonZeroI32, i32);
impl_nonzero_number!(NonZeroI64, i64);
impl_nonzero_number!(NonZeroI128, i128);
macro_rules! impl_zst {
($({$($bounds:tt)*},)? $ty:ty, $expr:expr , {$example:ty $(, $import:path)?}) => {
#[doc = concat!(" [`ZeroCopy`] implementation for `", stringify!($ty), "`")]
///
/// # Examples
///
/// ```
$(#[doc = concat!("use ", stringify!($import), ";")])*
/// use musli_zerocopy::{ZeroCopy, OwnedBuf};
///
/// #[derive(Default, Clone, Copy, ZeroCopy)]
/// #[repr(C)]
/// struct Struct {
#[doc = concat!(" field: ", stringify!($example), ",")]
/// }
///
/// let mut empty = OwnedBuf::new();
/// let values = [Struct::default(); 100];
/// let slice = empty.store_slice(&values[..]);
/// let buf = empty.into_aligned();
/// assert_eq!(buf.len(), 0);
///
/// let slice = buf.load(slice)?;
/// assert_eq!(slice.len(), 100);
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
unsafe impl $(<$($bounds)*>)* ZeroCopy for $ty {
const ANY_BITS: bool = true;
const PADDED: bool = false;
#[inline]
unsafe fn store_unaligned(_: *const Self, _: &mut BufMut) {
}
#[inline]
unsafe fn pad(_: *const Self, _: &mut Padder<'_, Self>) {
}
#[inline]
unsafe fn validate(_: &mut Validator<'_, Self>) -> Result<(), Error> {
Ok(())
}
}
impl $(<$($bounds)*>)* Visit for $ty {
type Target = $ty;
#[inline]
fn visit<V, O>(&self, _: &Buf, visitor: V) -> Result<O, Error>
where
V: FnOnce(&Self::Target) -> O,
{
Ok(visitor(self))
}
}
};
}
impl_zst!((), (), { () });
impl_zst!({T}, PhantomData<T>, PhantomData, {PhantomData<u32>, std::marker::PhantomData});
/// [`ZeroCopy`] implementation for `[T; 0]`.
///
/// # Examples
///
/// ```
/// use std::mem::align_of;
///
/// use musli_zerocopy::{ZeroCopy, OwnedBuf};
///
/// #[derive(Default, Clone, Copy, ZeroCopy)]
/// #[repr(C)]
/// struct Struct<T> {
/// #[zero_copy(ignore)]
/// field: [T; 0],
/// }
///
/// let mut empty = OwnedBuf::with_alignment::<u128>();
/// let values = [Struct::<u128>::default(); 100];
/// let slice = empty.store_slice(&values[..]);
/// let buf = empty.into_aligned();
/// assert_eq!(buf.len(), 0);
///
/// let slice = buf.load(slice)?;
/// assert_eq!(slice.len(), 100);
/// # Ok::<_, musli_zerocopy::Error>(())
/// ```
unsafe impl<T, const N: usize> ZeroCopy for [T; N]
where
T: ZeroCopy,
{
const ANY_BITS: bool = T::ANY_BITS;
const PADDED: bool = T::PADDED;
#[inline]
unsafe fn store_unaligned(this: *const Self, buf: &mut BufMut<'_>) {
// SAFETY: The buffer where we store the struct has been allocated for
// the `[T; N]`.
unsafe {
let mut padder = buf.store_struct(this);
Self::pad(this, &mut padder);
}
}
#[inline]
unsafe fn pad(this: *const Self, padder: &mut Padder<'_, Self>) {
if T::PADDED {
// Cast the array to a pointer of the first element.
let mut first = this.cast::<T>();
for _ in 0..N {
padder.pad::<T>(first);
first = first.add(1);
}
}
}
#[allow(clippy::missing_safety_doc)]
#[inline]
unsafe fn validate(validator: &mut Validator<'_, Self>) -> Result<(), Error> {
crate::buf::validate_array::<_, T>(validator, N)?;
Ok(())
}
}
impl<T> Visit for [T; 0] {
type Target = [T; 0];
#[inline]
fn visit<V, O>(&self, _: &Buf, visitor: V) -> Result<O, Error>
where
V: FnOnce(&Self::Target) -> O,
{
Ok(visitor(self))
}
}