@@ -284,6 +284,19 @@ pub trait ZeroizeOnDrop {}
284
284
/// Marker trait for types whose [`Default`] is the desired zeroization result
285
285
pub trait DefaultIsZeroes : Copy + Default + Sized { }
286
286
287
+ /// Fallible trait for representing cases where zeroization may or may not be
288
+ /// possible.
289
+ ///
290
+ /// This is primarily useful for scenarios like reference counted data, where
291
+ /// zeroization is only possible when the last reference is dropped.
292
+ pub trait TryZeroize {
293
+ /// Try to zero out this object from memory using Rust intrinsics which
294
+ /// ensure the zeroization operation is not "optimized away" by the
295
+ /// compiler.
296
+ #[ must_use]
297
+ fn try_zeroize ( & mut self ) -> bool ;
298
+ }
299
+
287
300
impl < Z > Zeroize for Z
288
301
where
289
302
Z : DefaultIsZeroes ,
@@ -598,19 +611,6 @@ impl Zeroize for CString {
598
611
}
599
612
}
600
613
601
- /// Fallible trait for representing cases where zeroization may or may not be
602
- /// possible.
603
- ///
604
- /// This is primarily useful for scenarios like reference counted data, where
605
- /// zeroization is only possible when the last reference is dropped.
606
- pub trait TryZeroize {
607
- /// Try to zero out this object from memory using Rust intrinsics which
608
- /// ensure the zeroization operation is not "optimized away" by the
609
- /// compiler.
610
- #[ must_use]
611
- fn try_zeroize ( & mut self ) -> bool ;
612
- }
613
-
614
614
/// `Zeroizing` is a a wrapper for any `Z: Zeroize` type which implements a
615
615
/// `Drop` handler which zeroizes dropped values.
616
616
#[ derive( Debug , Default , Eq , PartialEq ) ]
@@ -622,16 +622,19 @@ where
622
622
{
623
623
/// Move value inside a `Zeroizing` wrapper which ensures it will be
624
624
/// zeroized when it's dropped.
625
+ #[ inline( always) ]
625
626
pub fn new ( value : Z ) -> Self {
626
- value . into ( )
627
+ Self ( value )
627
628
}
628
629
}
629
630
630
631
impl < Z : Zeroize + Clone > Clone for Zeroizing < Z > {
632
+ #[ inline( always) ]
631
633
fn clone ( & self ) -> Self {
632
634
Self ( self . 0 . clone ( ) )
633
635
}
634
636
637
+ #[ inline( always) ]
635
638
fn clone_from ( & mut self , source : & Self ) {
636
639
self . 0 . zeroize ( ) ;
637
640
self . 0 . clone_from ( & source. 0 ) ;
@@ -642,6 +645,7 @@ impl<Z> From<Z> for Zeroizing<Z>
642
645
where
643
646
Z : Zeroize ,
644
647
{
648
+ #[ inline( always) ]
645
649
fn from ( value : Z ) -> Zeroizing < Z > {
646
650
Zeroizing ( value)
647
651
}
@@ -653,6 +657,7 @@ where
653
657
{
654
658
type Target = Z ;
655
659
660
+ #[ inline( always) ]
656
661
fn deref ( & self ) -> & Z {
657
662
& self . 0
658
663
}
@@ -662,6 +667,7 @@ impl<Z> ops::DerefMut for Zeroizing<Z>
662
667
where
663
668
Z : Zeroize ,
664
669
{
670
+ #[ inline( always) ]
665
671
fn deref_mut ( & mut self ) -> & mut Z {
666
672
& mut self . 0
667
673
}
@@ -672,6 +678,7 @@ where
672
678
T : ?Sized ,
673
679
Z : AsRef < T > + Zeroize ,
674
680
{
681
+ #[ inline( always) ]
675
682
fn as_ref ( & self ) -> & T {
676
683
self . 0 . as_ref ( )
677
684
}
@@ -682,6 +689,7 @@ where
682
689
T : ?Sized ,
683
690
Z : AsMut < T > + Zeroize ,
684
691
{
692
+ #[ inline( always) ]
685
693
fn as_mut ( & mut self ) -> & mut T {
686
694
self . 0 . as_mut ( )
687
695
}
@@ -710,13 +718,13 @@ where
710
718
/// Use fences to prevent accesses from being reordered before this
711
719
/// point, which should hopefully help ensure that all accessors
712
720
/// see zeroes after this point.
713
- #[ inline]
721
+ #[ inline( always ) ]
714
722
fn atomic_fence ( ) {
715
723
atomic:: compiler_fence ( atomic:: Ordering :: SeqCst ) ;
716
724
}
717
725
718
726
/// Perform a volatile write to the destination
719
- #[ inline]
727
+ #[ inline( always ) ]
720
728
fn volatile_write < T : Copy + Sized > ( dst : & mut T , src : T ) {
721
729
unsafe { ptr:: write_volatile ( dst, src) }
722
730
}
@@ -729,7 +737,7 @@ fn volatile_write<T: Copy + Sized>(dst: &mut T, src: T) {
729
737
/// `count` must not be larger than an `isize`.
730
738
/// `dst` being offset by `mem::size_of::<T> * count` bytes must not wrap around the address space.
731
739
/// Also `dst` must be properly aligned.
732
- #[ inline]
740
+ #[ inline( always ) ]
733
741
unsafe fn volatile_set < T : Copy + Sized > ( dst : * mut T , src : T , count : usize ) {
734
742
// TODO(tarcieri): use `volatile_set_memory` when stabilized
735
743
for i in 0 ..count {
0 commit comments