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
//! # rkyv
//!
//! rkyv (*archive*) is a zero-copy deserialization framework for Rust.
//!
//! It's similar to other zero-copy deserialization frameworks such as
//! [Cap'n Proto](https://capnproto.org) and
//! [FlatBuffers](https://google.github.io/flatbuffers). However, while the
//! former have external schemas and heavily restricted data types, rkyv allows
//! all serialized types to be defined in code and can serialize a wide variety
//! of types that the others cannot. Additionally, rkyv is designed to have
//! little to no overhead, and in most cases will perform exactly the same as
//! native types.
//!
//! rkyv has a hashmap implementation that is built for zero-copy
//! deserialization, so you can serialize your hashmaps with abandon! The
//! implementation is based off of the standard library's `hashbrown` crate and
//! should have nearly identical performance.
//!
//! One of the most impactful features made possible by rkyv is the ability to
//! serialize trait objects and use them *as trait objects* without
//! deserialization. See the `archive_dyn` crate for more details.
//!
//! ## Design
//!
//! Like [serde](https://serde.rs), rkyv uses Rust's powerful trait system to
//! serialize data without the need for reflection. Despite having a wide array
//! of features, you also only pay for what you use. If your data checks out,
//! the serialization process can be as simple as a `memcpy`! Like serde, this
//! allows rkyv to perform at speeds similar to handwritten serializers.
//!
//! Unlike serde, rkyv produces data that is guaranteed deserialization free. If
//! you wrote your data to disk, you can just `mmap` your file into memory, cast
//! a pointer, and your data is ready to use. This makes it ideal for
//! high-performance and IO-limited applications.
//!
//! ## Tradeoffs
//!
//! rkyv is designed primarily for loading bulk game data as efficiently as
//! possible. While rkyv is a great format for final data, it lacks a full
//! schema system and isn't well equipped for data migration. Using a
//! serialization library like serde can help fill these gaps, and you can use
//! serde with the same types as rkyv conflict-free.
//!
//! ## Features
//!
//! - `const_generics`: Improves the implementations for some traits and
//!   provides an [`Archive`] implementation for slices with elements that
//!   implement [`ArchiveSelf`]. Ideal for `#![no_std]` environments.
//! - `inline_more`: Performs more aggressive function inlining.
//! - `long_rel_ptrs`: Increases the size of relative pointers to 64 bits for
//!   large archive support
//! - `more_portable`: Avoids using sse2-optimized intrinsics since they may
//!   cause alignment issues across machines. This feature may go away once any
//!   portability bugs are identified and fixed.
//! - `nightly`: Enables some nightly features, such as
//!   [`likely`](std::intrinsics::likely).
//! - `specialization`: Enables the unfinished specialization feature and
//!   provides more efficient implementations of some functions when working
//!   with [`ArchiveSelf`] types.
//! - `std`: Enables standard library support.
//! - `strict`: Guarantees that types will have the same representations across
//!   platforms and compilations. This is already the case in practice, but this
//!   feature provides a guarantee.
//! - `validation`: Enables validation support through `bytecheck`.
//!
//! By default, the `inline_more` and `std` features are enabled.

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(
    any(feature = "const_generics", feature = "specialization"),
    allow(incomplete_features)
)]
#![cfg_attr(feature = "const_generics", feature(const_generics))]
#![cfg_attr(feature = "nightly", feature(core_intrinsics))]
#![cfg_attr(feature = "specialization", feature(specialization))]

pub mod core_impl;
#[cfg(feature = "std")]
pub mod std_impl;
#[cfg(feature = "validation")]
pub mod validation;

use core::{
    marker::PhantomPinned,
    mem,
    ops::{Deref, DerefMut},
    pin::Pin,
    ptr, slice,
};
#[cfg(feature = "std")]
use std::io;

pub use memoffset::offset_of;
pub use rkyv_derive::Archive;
#[cfg(feature = "validation")]
pub use validation::{check_archive, ArchiveContext};

/// A `#![no_std]` compliant writer that knows where it is.
///
/// A type that is [`io::Write`](std::io::Write) can be wrapped in an
/// ArchiveWriter to equip it with `Write`. It's important that the memory for
/// archived objects is properly aligned before attempting to read objects out
/// of it, use the [`Aligned`] wrapper if it's appropriate.
pub trait Write {
    /// The errors that may occur while writing.
    type Error: 'static;

    /// Returns the current position of the writer.
    fn pos(&self) -> usize;

    /// Attempts to write the given bytes to the writer.
    fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error>;

    /// Advances the given number of bytes as padding.
    fn pad(&mut self, mut padding: usize) -> Result<(), Self::Error> {
        const ZEROES_LEN: usize = 16;
        const ZEROES: [u8; ZEROES_LEN] = [0; ZEROES_LEN];

        while padding > 0 {
            let len = usize::min(ZEROES_LEN, padding);
            self.write(&ZEROES[0..len])?;
            padding -= len;
        }

        Ok(())
    }
}

/// Helper functions on [`Write`] objects.
pub trait WriteExt: Write {
    /// Aligns the position of the writer to the given alignment.
    fn align(&mut self, align: usize) -> Result<usize, Self::Error> {
        debug_assert!(align & (align - 1) == 0);

        let offset = self.pos() & (align - 1);
        if offset != 0 {
            self.pad(align - offset)?;
        }
        Ok(self.pos())
    }

    /// Aligns the position of the writer to be suitable to write the given
    /// type.
    fn align_for<T>(&mut self) -> Result<usize, Self::Error> {
        self.align(mem::align_of::<T>())
    }

    /// Resolves the given resolver and writes its archived type, returning the
    /// position of the written archived type.
    ///
    /// # Safety
    ///
    /// This is only safe to call when the writer is already aligned for the
    /// archived version of the given type.
    unsafe fn resolve_aligned<T: ?Sized, R: Resolve<T>>(
        &mut self,
        value: &T,
        resolver: R,
    ) -> Result<usize, Self::Error> {
        let pos = self.pos();
        debug_assert!(pos & (mem::align_of::<R::Archived>() - 1) == 0);
        let archived = &resolver.resolve(pos, value);
        let data = (archived as *const R::Archived).cast::<u8>();
        let len = mem::size_of::<R::Archived>();
        self.write(slice::from_raw_parts(data, len))?;
        Ok(pos)
    }

    /// Archives the given object and returns the position it was archived at.
    fn archive<T: Archive>(&mut self, value: &T) -> Result<usize, Self::Error> {
        let resolver = value.archive(self)?;
        self.align_for::<T::Archived>()?;
        unsafe { self.resolve_aligned(value, resolver) }
    }

    /// Archives a reference to the given object and returns the position it was
    /// archived at.
    fn archive_ref<T: ArchiveRef + ?Sized>(&mut self, value: &T) -> Result<usize, Self::Error> {
        let resolver = value.archive_ref(self)?;
        self.align_for::<T::Reference>()?;
        unsafe { self.resolve_aligned(value, resolver) }
    }
}

impl<W: Write + ?Sized> WriteExt for W {}

/// A writer that can seek to an absolute position.
pub trait Seek: Write {
    /// Seeks the writer to the given absolute position.
    fn seek(&mut self, pos: usize) -> Result<(), Self::Error>;
}

/// Helper functions on [`Seek`] objects.
pub trait SeekExt: Seek {
    /// Archives the given value at the nearest available position. If the
    /// writer is already aligned, it will archive it at the current position.
    fn archive_root<T: Archive>(&mut self, value: &T) -> Result<usize, Self::Error> {
        self.align_for::<Archived<T>>()?;
        let pos = self.pos();
        self.seek(pos + mem::size_of::<Archived<T>>())?;
        let resolver = value.archive(self)?;
        self.seek(pos)?;
        unsafe {
            self.resolve_aligned(value, resolver)?;
        }
        Ok(pos)
    }

    /// Archives a reference to the given value at the nearest available
    /// position. If the writer is already aligned, it will archive it at the
    /// current position.
    fn archive_ref_root<T: ArchiveRef + ?Sized>(
        &mut self,
        value: &T,
    ) -> Result<usize, Self::Error> {
        self.align_for::<Reference<T>>()?;
        let pos = self.pos();
        self.seek(pos + mem::size_of::<Reference<T>>())?;
        let resolver = value.archive_ref(self)?;
        self.seek(pos)?;
        unsafe {
            self.resolve_aligned(value, resolver)?;
        }
        Ok(pos)
    }
}

impl<W: Seek + ?Sized> SeekExt for W {}

/// Creates an archived value when given a value and position.
///
/// Resolvers are passed the original value, so any information that is already
/// in them doesn't have to be stored in the resolver.
pub trait Resolve<T: ?Sized> {
    /// The type that this resolver resolves to.
    type Archived;

    /// Creates the archived version of the given value at the given position.
    fn resolve(self, pos: usize, value: &T) -> Self::Archived;
}

/// Writes a type to a [`Writer`](Write) so it can be used without
/// deserializing.
///
/// Archiving is done depth-first, writing any data owned by a type before
/// writing the data for the type itself. The [`Resolver`](Resolve) must be able
/// to create the archived type from only its own data and the value being
/// archived.
///
/// ## Examples
///
/// Most of the time, `#[derive(Archive)]` will create an acceptable
/// implementation. You can use the `#[archive(...)]` attribute to control how
/// the implementation is generated. See the [`Archive`](macro@Archive) derive
/// macro for more details.
///
/// ```
/// use rkyv::{Aligned, Archive, ArchiveBuffer, Archived, archived_value, WriteExt};
///
/// #[derive(Archive)]
/// struct Test {
///     int: u8,
///     string: String,
///     option: Option<Vec<i32>>,
/// }
///
/// let mut writer = ArchiveBuffer::new(Aligned([0u8; 256]));
/// let value = Test {
///     int: 42,
///     string: "hello world".to_string(),
///     option: Some(vec![1, 2, 3, 4]),
/// };
/// let pos = writer.archive(&value)
///     .expect("failed to archive test");
/// let buf = writer.into_inner();
/// let archived = unsafe { archived_value::<Test>(buf.as_ref(), pos) };
/// assert_eq!(archived.int, value.int);
/// assert_eq!(archived.string, value.string);
/// assert_eq!(archived.option, value.option);
/// ```
///
/// Many of the core and standard library types already have Archive
/// implementations available, but you may need to implement `Archive` for your
/// own types in some cases the derive macro cannot handle.
///
/// In this example, we add our own wrapper that serializes a `&'static str` as
/// if it's owned. Normally you can lean on the archived version of `String` to
/// do most of the work, but this example does everything to demonstrate how to
/// implement `Archive` for your own types.
///
/// ```
/// use core::{slice, str};
/// use rkyv::{
///     Aligned,
///     Archive,
///     ArchiveBuffer,
///     Archived,
///     archived_value,
///     offset_of,
///     RelPtr,
///     Resolve,
///     Write,
///     WriteExt,
/// };
///
/// struct OwnedStr {
///     inner: &'static str,
/// }
///
/// struct ArchivedOwnedStr {
///     // This will be a relative pointer to the bytes of our string.
///     ptr: RelPtr,
///     // The length of the archived version must be explicitly sized for
///     // 32/64-bit compatibility. Archive is not implemented for usize and
///     // isize to help you avoid making this mistake.
///     len: u32,
/// }
///
/// impl ArchivedOwnedStr {
///     // This will help us get the bytes of our type as a str again.
///     fn as_str(&self) -> &str {
///         unsafe {
///             // The as_ptr() function of RelPtr will get a pointer
///             // to its memory.
///             let bytes = slice::from_raw_parts(self.ptr.as_ptr(), self.len as usize);
///             str::from_utf8_unchecked(bytes)
///         }
///     }
/// }
///
/// struct OwnedStrResolver {
///     // This will be the position that the bytes of our string are stored at.
///     // We'll use this to make the relative pointer of our ArchivedOwnedStr.
///     bytes_pos: usize,
/// }
///
/// impl Resolve<OwnedStr> for OwnedStrResolver {
///     // This is essentially the output type of the resolver. It must match
///     // the Archived associated type in our impl of Archive for OwnedStr.
///     type Archived = ArchivedOwnedStr;
///
///     // The resolve function consumes the resolver and produces the archived
///     // value at the given position.
///     fn resolve(self, pos: usize, value: &OwnedStr) -> Self::Archived {
///         Self::Archived {
///             // We have to be careful to add the offset of the ptr field,
///             // otherwise we'll be using the position of the ArchivedOwnedStr
///             // instead of the position of the ptr. That's the reason why
///             // RelPtr::new is unsafe.
///             ptr: unsafe {
///                 RelPtr::new(pos + offset_of!(ArchivedOwnedStr, ptr), self.bytes_pos)
///             },
///             len: value.inner.len() as u32,
///         }
///     }
/// }
///
/// impl Archive for OwnedStr {
///     type Archived = ArchivedOwnedStr;
///     /// This is the resolver we'll return from archive.
///     type Resolver = OwnedStrResolver;
///
///     fn archive<W: Write + ?Sized>(&self, writer: &mut W) -> Result<Self::Resolver, W::Error> {
///         // This is where we want to write the bytes of our string and return
///         // a resolver that knows where those bytes were written.
///         let bytes_pos = writer.pos();
///         writer.write(self.inner.as_bytes())?;
///         Ok(Self::Resolver { bytes_pos })
///     }
/// }
///
/// let mut writer = ArchiveBuffer::new(Aligned([0u8; 256]));
/// const STR_VAL: &'static str = "I'm in an OwnedStr!";
/// let value = OwnedStr { inner: STR_VAL };
/// // It works!
/// let pos = writer.archive(&value)
///     .expect("failed to archive test");
/// let buf = writer.into_inner();
/// let archived = unsafe { archived_value::<OwnedStr>(buf.as_ref(), pos) };
/// // Let's make sure our data got written correctly
/// assert_eq!(archived.as_str(), STR_VAL);
/// ```
pub trait Archive {
    /// The archived version of this type.
    type Archived;
    /// The resolver for this type. It must contain all the information needed
    /// to make the archived type from the unarchived type.
    type Resolver: Resolve<Self, Archived = Self::Archived>;

    /// Writes the dependencies for the object and returns a resolver that can
    /// create the archived type.
    fn archive<W: Write + ?Sized>(&self, writer: &mut W) -> Result<Self::Resolver, W::Error>;
}

/// This trait is a counterpart of [`Archive`] that's suitable for unsized
/// types.
///
/// Instead of archiving its value directly, `ArchiveRef` archives a type that
/// dereferences to its archived type. As a consequence, its `Resolver` resolves
/// to a `Reference` instead of the archived type.
///
/// `ArchiveRef` is automatically implemented for all types that implement
/// [`Archive`], and uses a [`RelPtr`] as the reference type.
///
/// `ArchiveRef` is already implemented for slices and string slices. Use the
/// `rkyv_dyn` crate to archive trait objects. Unfortunately, you'll have to
/// manually implement `ArchiveRef` for your other unsized types.
pub trait ArchiveRef {
    /// The archived version of this type.
    type Archived: ?Sized;
    /// The reference to the archived version of this type.
    type Reference: Deref<Target = Self::Archived> + DerefMut<Target = Self::Archived>;
    /// The resolver for the reference of this type.
    type Resolver: Resolve<Self, Archived = Self::Reference>;

    /// Writes the object and returns a resolver that can create the reference
    /// to the archived type.
    fn archive_ref<W: Write + ?Sized>(&self, writer: &mut W) -> Result<Self::Resolver, W::Error>;
}

/// A trait that indicates that some [`Archive`] type can be copied directly to
/// an archive without additional processing.
///
/// You can derive an implementation of `ArchiveSelf` by adding
/// `#[archive(self)]` to the struct or enum. Types that implement `ArchiveSelf`
/// must also implement [`Copy`](core::marker::Copy).
///
/// Types that implement `ArchiveSelf` are not guaranteed to have `archive`
/// called on them to archive their value. Most or all implementations that
/// leverage `ArchiveSelf` will require the `specialization` feature.
///
/// `ArchiveSelf` must be manually implemented even if a type implements
/// [`Archive`] and [`Copy`](core::marker::Copy) because some types may
/// transform their data when writing to an archive.
///
/// ## Examples
/// ```
/// use rkyv::{Aligned, Archive, ArchiveBuffer, archived_value, Write, WriteExt};
///
/// #[derive(Archive, Clone, Copy, Debug, PartialEq)]
/// #[archive(self)]
/// struct Vector4<T>(T, T, T, T);
///
/// let mut writer = ArchiveBuffer::new(Aligned([0u8; 256]));
/// let value = Vector4(1f32, 2f32, 3f32, 4f32);
/// let pos = writer.archive(&value)
///     .expect("failed to archive Vector4");
/// let buf = writer.into_inner();
/// let archived_value = unsafe { archived_value::<Vector4<f32>>(buf.as_ref(), pos) };
/// assert_eq!(&value, archived_value);
/// ```
pub unsafe trait ArchiveSelf: Archive<Archived = Self> + Copy {}

/// A resolver that always resolves to the unarchived value. This can be useful
/// while implementing [`ArchiveSelf`].
pub struct SelfResolver;

impl<T: ArchiveSelf> Resolve<T> for SelfResolver {
    type Archived = T;

    fn resolve(self, _pos: usize, value: &T) -> T {
        *value
    }
}

/// The type used for offsets in relative pointers.
#[cfg(not(feature = "long_rel_ptrs"))]
pub type Offset = i32;

/// The type used for offsets in relative pointers.
#[cfg(feature = "long_rel_ptrs")]
pub type Offset = i64;

/// A pointer which resolves to relative to its position in memory.
///
/// See [`Archive`] for an example of creating one.
#[repr(transparent)]
#[derive(Debug)]
pub struct RelPtr {
    offset: Offset,
    _phantom: PhantomPinned,
}

impl RelPtr {
    /// Creates a relative pointer from one position to another.
    ///
    /// # Safety
    ///
    /// `from` must be the position of the relative pointer and `to` must be the
    /// position of some valid memory.
    pub unsafe fn new(from: usize, to: usize) -> Self {
        Self {
            offset: (to as isize - from as isize) as Offset,
            _phantom: PhantomPinned,
        }
    }

    /// Gets the offset of the relative pointer.
    pub fn offset(&self) -> isize {
        self.offset as isize
    }

    /// Calculates the memory address being pointed to by this relative pointer.
    pub fn as_ptr<T>(&self) -> *const T {
        unsafe {
            (self as *const Self)
                .cast::<u8>()
                .offset(self.offset as isize)
                .cast::<T>()
        }
    }

    /// Returns an unsafe mutable pointer to the memory address being pointed to
    /// by this relative pointer.
    pub fn as_mut_ptr<T>(&mut self) -> *mut T {
        unsafe {
            (self as *mut Self)
                .cast::<u8>()
                .offset(self.offset as isize)
                .cast::<T>()
        }
    }
}

/// Alias for the archived version of some [`Archive`] type.
pub type Archived<T> = <T as Archive>::Archived;
/// Alias for the resolver for some [`Archive`] type.
pub type Resolver<T> = <T as Archive>::Resolver;
/// Alias for the resolver of the reference for some [`ArchiveRef`] type.
pub type ReferenceResolver<T> = <T as ArchiveRef>::Resolver;
/// Alias for the reference for some [`ArchiveRef`] type.
pub type Reference<T> = <T as ArchiveRef>::Reference;

/// Wraps a type and aligns it to at least 16 bytes. Mainly used to align byte
/// buffers for [ArchiveBuffer].
///
/// ## Examples
/// ```
/// use core::mem;
/// use rkyv::Aligned;
///
/// assert_eq!(mem::align_of::<u8>(), 1);
/// assert_eq!(mem::align_of::<Aligned<u8>>(), 16);
/// ```
#[repr(align(16))]
pub struct Aligned<T>(pub T);

impl<T: Deref> Deref for Aligned<T> {
    type Target = T::Target;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

impl<T: DerefMut> DerefMut for Aligned<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.0
    }
}

impl<T: AsRef<[U]>, U> AsRef<[U]> for Aligned<T> {
    fn as_ref(&self) -> &[U] {
        self.0.as_ref()
    }
}

impl<T: AsMut<[U]>, U> AsMut<[U]> for Aligned<T> {
    fn as_mut(&mut self) -> &mut [U] {
        self.0.as_mut()
    }
}

/// Wraps a byte buffer and writes into it.
///
/// Common uses include archiving in `#![no_std]` environments and archiving
/// small objects without allocating.
///
/// ## Examples
/// ```
/// use rkyv::{Aligned, Archive, ArchiveBuffer, Archived, archived_value, WriteExt};
///
/// #[derive(Archive)]
/// enum Event {
///     Spawn,
///     Speak(String),
///     Die,
/// }
///
/// let mut writer = ArchiveBuffer::new(Aligned([0u8; 256]));
/// let pos = writer.archive(&Event::Speak("Help me!".to_string()))
///     .expect("failed to archive event");
/// let buf = writer.into_inner();
/// let archived = unsafe { archived_value::<Event>(buf.as_ref(), pos) };
/// if let Archived::<Event>::Speak(message) = archived {
///     assert_eq!(message.as_str(), "Help me!");
/// } else {
///     panic!("archived event was of the wrong type");
/// }
/// ```
pub struct ArchiveBuffer<T> {
    inner: T,
    pos: usize,
}

impl<T> ArchiveBuffer<T> {
    /// Creates a new archive buffer from a byte buffer.
    pub fn new(inner: T) -> Self {
        Self::with_pos(inner, 0)
    }

    /// Creates a new archive buffer from a byte buffer. The buffer will start
    /// writing at the given position, but the buffer must contain all bytes
    /// (otherwise the alignments of types may not be correct).
    pub fn with_pos(inner: T, pos: usize) -> Self {
        Self { inner, pos }
    }

    /// Consumes the buffer and returns the internal buffer used to create it.
    pub fn into_inner(self) -> T {
        self.inner
    }
}

/// The error type returned by an [`ArchiveBuffer`].
#[derive(Debug)]
pub enum ArchiveBufferError {
    /// Writing has overflowed the internal buffer.
    Overflow {
        pos: usize,
        bytes_needed: usize,
        archive_len: usize,
    },
    /// The writer sought past the end of the internal buffer.
    SoughtPastEnd {
        seek_position: usize,
        archive_len: usize,
    },
}

impl<T: AsRef<[u8]> + AsMut<[u8]>> Write for ArchiveBuffer<T> {
    type Error = ArchiveBufferError;

    fn pos(&self) -> usize {
        self.pos
    }

    fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
        let end_pos = self.pos + bytes.len();
        let archive_len = self.inner.as_ref().len();
        if end_pos > archive_len {
            Err(ArchiveBufferError::Overflow {
                pos: self.pos,
                bytes_needed: bytes.len(),
                archive_len,
            })
        } else {
            unsafe {
                ptr::copy_nonoverlapping(
                    bytes.as_ptr(),
                    self.inner.as_mut().as_mut_ptr().add(self.pos),
                    bytes.len(),
                );
            }
            self.pos = end_pos;
            Ok(())
        }
    }

    fn pad(&mut self, padding: usize) -> Result<(), Self::Error> {
        let end_pos = self.pos + padding;
        let archive_len = self.inner.as_ref().len();
        if end_pos > archive_len {
            Err(ArchiveBufferError::Overflow {
                pos: self.pos,
                bytes_needed: padding,
                archive_len,
            })
        } else {
            self.pos = end_pos;
            Ok(())
        }
    }
}

impl<T: AsRef<[u8]> + AsMut<[u8]>> Seek for ArchiveBuffer<T> {
    fn seek(&mut self, pos: usize) -> Result<(), Self::Error> {
        let len = self.inner.as_ref().len();
        if pos > len {
            Err(ArchiveBufferError::SoughtPastEnd {
                seek_position: pos,
                archive_len: len,
            })
        } else {
            self.pos = pos;
            Ok(())
        }
    }
}

/// Wraps a type that implements [`io::Write`](std::io::Write) and equips it
/// with [`Write`].
///
/// ## Examples
/// ```
/// use rkyv::{ArchiveWriter, Write};
///
/// let mut writer = ArchiveWriter::new(Vec::new());
/// assert_eq!(writer.pos(), 0);
/// writer.write(&[0u8, 1u8, 2u8, 3u8]);
/// assert_eq!(writer.pos(), 4);
/// let buf = writer.into_inner();
/// assert_eq!(buf.len(), 4);
/// assert_eq!(buf, vec![0u8, 1u8, 2u8, 3u8]);
/// ```
#[cfg(feature = "std")]
pub struct ArchiveWriter<W: io::Write> {
    inner: W,
    pos: usize,
}

#[cfg(feature = "std")]
impl<W: io::Write> ArchiveWriter<W> {
    /// Creates a new archive writer from a writer.
    pub fn new(inner: W) -> Self {
        Self::with_pos(inner, 0)
    }

    /// Creates a new archive writer from a writer, and assumes that the
    /// underlying writer is currently at the given position.
    pub fn with_pos(inner: W, pos: usize) -> Self {
        Self { inner, pos }
    }

    /// Consumes the writer and returns the internal writer used to create it.
    pub fn into_inner(self) -> W {
        self.inner
    }
}

#[cfg(feature = "std")]
impl<W: io::Write> Write for ArchiveWriter<W> {
    type Error = io::Error;

    fn pos(&self) -> usize {
        self.pos
    }

    fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
        self.pos += self.inner.write(bytes)?;
        Ok(())
    }
}

#[cfg(feature = "std")]
impl<W: io::Write + io::Seek> Seek for ArchiveWriter<W> {
    fn seek(&mut self, offset: usize) -> Result<(), Self::Error> {
        self.inner.seek(io::SeekFrom::Start(offset as u64))?;
        self.pos = offset;
        Ok(())
    }
}

/// Casts an archived value from the given byte array at the given position.
///
/// This helps avoid situations where lifetimes get inappropriately assigned and
/// allow buffer mutation after getting archived value references.
///
/// # Safety
///
/// This is only safe to call if the value is archived at the given position in
/// the byte array.
#[inline]
pub unsafe fn archived_value<T: Archive + ?Sized>(bytes: &[u8], pos: usize) -> &Archived<T> {
    &*bytes.as_ptr().add(pos).cast()
}

/// Casts a mutable archived value from the given byte array at the given
/// position.
///
/// This helps avoid situations where lifetimes get inappropriately assigned and
/// allow buffer mutation after getting archived value references.
///
/// # Safety
///
/// This is only safe to call if the value is archived at the given position in
/// the byte array.
#[inline]
pub unsafe fn archived_value_mut<T: Archive + ?Sized>(
    bytes: Pin<&mut [u8]>,
    pos: usize,
) -> Pin<&mut Archived<T>> {
    Pin::new_unchecked(&mut *bytes.get_unchecked_mut().as_mut_ptr().add(pos).cast())
}

/// Casts an archived reference from the given byte array at the given position.
///
/// This helps avoid situations where lifetimes get inappropriately assigned and
/// allow buffer mutation after getting archived value references.
///
/// # Safety
///
/// This is only safe to call if the reference is archived at the given position
/// in the byte array.
#[inline]
pub unsafe fn archived_ref<T: ArchiveRef + ?Sized>(bytes: &[u8], pos: usize) -> &Reference<T> {
    &*bytes.as_ptr().add(pos).cast()
}

/// Casts a mutable archived reference from the given byte array at the given
/// position.
///
/// This helps avoid situations where lifetimes get inappropriately assigned and
/// allow buffer mutation after getting archived value references.
///
/// # Safety
///
/// This is only safe to call if the reference is archived at the given position
/// in the byte array.
#[inline]
pub unsafe fn archived_ref_mut<T: ArchiveRef + ?Sized>(
    bytes: Pin<&mut [u8]>,
    pos: usize,
) -> Pin<&mut Reference<T>> {
    Pin::new_unchecked(&mut *bytes.get_unchecked_mut().as_mut_ptr().add(pos).cast())
}