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
//! Provides the wrapper type `Volatile`, which wraps a reference to any copy-able type and allows
//! for volatile memory access to wrapped value. Volatile memory accesses are never optimized away
//! by the compiler, and are useful in many low-level systems programming and concurrent contexts.
//!
//! The wrapper types *do not* enforce any atomicity guarantees; to also get atomicity, consider
//! looking at the `Atomic` wrapper types found in `libcore` or `libstd`.
#![no_std]
#![cfg_attr(feature = "unstable", feature(core_intrinsics))]
#![cfg_attr(feature = "unstable", feature(slice_range))]
#![cfg_attr(feature = "unstable", allow(incomplete_features))]
#![cfg_attr(all(feature = "unstable", test), feature(slice_as_chunks))]
#![warn(missing_docs)]
use access::{ReadOnly, ReadWrite, Readable, Writable, WriteOnly};
use core::{
fmt,
marker::PhantomData,
ops::{Deref, DerefMut, Index, IndexMut},
ptr,
slice::SliceIndex,
};
#[cfg(feature = "unstable")]
use core::{
intrinsics,
ops::{Range, RangeBounds},
slice::range,
};
/// Allows creating read-only and write-only `Volatile` values.
pub mod access;
/// Wraps a reference to make accesses to the referenced value volatile.
///
/// Allows volatile reads and writes on the referenced value. The referenced value needs to
/// be `Copy` for reading and writing, as volatile reads and writes take and return copies
/// of the value.
///
/// Since not all volatile resources (e.g. memory mapped device registers) are both readable
/// and writable, this type supports limiting the allowed access types through an optional second
/// generic parameter `A` that can be one of `ReadWrite`, `ReadOnly`, or `WriteOnly`. It defaults
/// to `ReadWrite`, which allows all operations.
///
/// The size of this struct is the same as the size of the contained reference.
#[derive(Clone)]
#[repr(transparent)]
pub struct Volatile<R, A = ReadWrite> {
reference: R,
access: PhantomData<A>,
}
/// Constructor functions for creating new values
///
/// These functions allow to construct a new `Volatile` instance from a reference type. While
/// the `new` function creates a `Volatile` instance with unrestricted access, there are also
/// functions for creating read-only or write-only instances.
impl<R> Volatile<R> {
/// Constructs a new volatile instance wrapping the given reference.
///
/// While it is possible to construct `Volatile` instances from arbitrary values (including
/// non-reference values), most of the methods are only available when the wrapped type is
/// a reference. The only reason that we don't forbid non-reference types in the constructor
/// functions is that the Rust compiler does not support trait bounds on generic `const`
/// functions yet. When this becomes possible, we will release a new version of this library
/// with removed support for non-references. For these reasons it is recommended to use
/// the `Volatile` type only with references.
///
/// ## Example
///
/// ```rust
/// use volatile::Volatile;
///
/// let mut value = 0u32;
///
/// let mut volatile = Volatile::new(&mut value);
/// volatile.write(1);
/// assert_eq!(volatile.read(), 1);
/// ```
pub const fn new(reference: R) -> Volatile<R> {
Volatile {
reference,
access: PhantomData,
}
}
/// Constructs a new read-only volatile instance wrapping the given reference.
///
/// This is equivalent to the `new` function with the difference that the returned
/// `Volatile` instance does not permit write operations. This is for example useful
/// with memory-mapped hardware registers that are defined as read-only by the hardware.
///
/// ## Example
///
/// Reading is allowed:
///
/// ```rust
/// use volatile::Volatile;
///
/// let value = 0u32;
///
/// let volatile = Volatile::new_read_only(&value);
/// assert_eq!(volatile.read(), 0);
/// ```
///
/// But writing is not:
///
/// ```compile_fail
/// use volatile::Volatile;
///
/// let mut value = 0u32;
///
/// let mut volatile = Volatile::new_read_only(&mut value);
/// volatile.write(1);
/// //ERROR: ^^^^^ the trait `volatile::access::Writable` is not implemented
/// // for `volatile::access::ReadOnly`
/// ```
pub const fn new_read_only(reference: R) -> Volatile<R, ReadOnly> {
Volatile {
reference,
access: PhantomData,
}
}
/// Constructs a new write-only volatile instance wrapping the given reference.
///
/// This is equivalent to the `new` function with the difference that the returned
/// `Volatile` instance does not permit read operations. This is for example useful
/// with memory-mapped hardware registers that are defined as write-only by the hardware.
///
/// ## Example
///
/// Writing is allowed:
///
/// ```rust
/// use volatile::Volatile;
///
/// let mut value = 0u32;
///
/// let mut volatile = Volatile::new_write_only(&mut value);
/// volatile.write(1);
/// ```
///
/// But reading is not:
///
/// ```compile_fail
/// use volatile::Volatile;
///
/// let value = 0u32;
///
/// let volatile = Volatile::new_write_only(&value);
/// volatile.read();
/// //ERROR: ^^^^ the trait `volatile::access::Readable` is not implemented
/// // for `volatile::access::WriteOnly`
/// ```
pub const fn new_write_only(reference: R) -> Volatile<R, WriteOnly> {
Volatile {
reference,
access: PhantomData,
}
}
}
/// Methods for references to `Copy` types
impl<R, T, A> Volatile<R, A>
where
R: Deref<Target = T>,
T: Copy,
{
/// Performs a volatile read of the contained value.
///
/// Returns a copy of the read value. Volatile reads are guaranteed not to be optimized
/// away by the compiler, but by themselves do not have atomic ordering
/// guarantees. To also get atomicity, consider looking at the `Atomic` wrapper types of
/// the standard/`core` library.
///
/// ## Examples
///
/// ```rust
/// use volatile::Volatile;
///
/// let value = 42;
/// let shared_reference = Volatile::new(&value);
/// assert_eq!(shared_reference.read(), 42);
///
/// let mut value = 50;
/// let mut_reference = Volatile::new(&mut value);
/// assert_eq!(mut_reference.read(), 50);
/// ```
pub fn read(&self) -> T
where
A: Readable,
{
// UNSAFE: Safe, as we know that our internal value exists.
unsafe { ptr::read_volatile(&*self.reference) }
}
/// Performs a volatile write, setting the contained value to the given `value`.
///
/// Volatile writes are guaranteed to not be optimized away by the compiler, but by
/// themselves do not have atomic ordering guarantees. To also get atomicity, consider
/// looking at the `Atomic` wrapper types of the standard/`core` library.
///
/// ## Example
///
/// ```rust
/// use volatile::Volatile;
///
/// let mut value = 42;
/// let mut volatile = Volatile::new(&mut value);
/// volatile.write(50);
///
/// assert_eq!(volatile.read(), 50);
/// ```
pub fn write(&mut self, value: T)
where
A: Writable,
R: DerefMut,
{
// UNSAFE: Safe, as we know that our internal value exists.
unsafe { ptr::write_volatile(&mut *self.reference, value) };
}
/// Updates the contained value using the given closure and volatile instructions.
///
/// Performs a volatile read of the contained value, passes a mutable reference to it to the
/// function `f`, and then performs a volatile write of the (potentially updated) value back to
/// the contained value.
///
/// ```rust
/// use volatile::Volatile;
///
/// let mut value = 42;
/// let mut volatile = Volatile::new(&mut value);
/// volatile.update(|val| *val += 1);
///
/// assert_eq!(volatile.read(), 43);
/// ```
pub fn update<F>(&mut self, f: F)
where
A: Readable + Writable,
R: DerefMut,
F: FnOnce(&mut T),
{
let mut value = self.read();
f(&mut value);
self.write(value);
}
}
/// Method for extracting the wrapped value.
impl<R, A> Volatile<R, A> {
/// Extracts the inner value stored in the wrapper type.
///
/// This method gives direct access to the wrapped reference and thus allows
/// non-volatile access again. This is seldom what you want since there is usually
/// a reason that a reference is wrapped in `Volatile`. However, in some cases it might
/// be required or useful to use the `read_volatile`/`write_volatile` pointer methods of
/// the standard library directly, which this method makes possible.
///
/// Since no memory safety violation can occur when accessing the referenced value using
/// non-volatile operations, this method is safe. However, it _can_ lead to bugs at the
/// application level, so this method should be used with care.
///
/// ## Example
///
/// ```
/// use volatile::Volatile;
///
/// let mut value = 42;
/// let mut volatile = Volatile::new(&mut value);
/// volatile.write(50);
/// let unwrapped: &mut i32 = volatile.extract_inner();
///
/// assert_eq!(*unwrapped, 50); // non volatile access, be careful!
/// ```
pub fn extract_inner(self) -> R {
self.reference
}
}
/// Transformation methods for accessing struct fields
impl<R, T, A> Volatile<R, A>
where
R: Deref<Target = T>,
T: ?Sized,
{
/// Constructs a new `Volatile` reference by mapping the wrapped value.
///
/// This method is useful for accessing individual fields of volatile structs.
///
/// Note that this method gives temporary access to the wrapped reference, which allows
/// accessing the value in a non-volatile way. This is normally not what you want, so
/// **this method should only be used for reference-to-reference transformations**.
///
/// ## Examples
///
/// Accessing a struct field:
///
/// ```
/// use volatile::Volatile;
///
/// struct Example { field_1: u32, field_2: u8, }
/// let mut value = Example { field_1: 15, field_2: 255 };
/// let mut volatile = Volatile::new(&mut value);
///
/// // construct a volatile reference to a field
/// let field_2 = volatile.map(|example| &example.field_2);
/// assert_eq!(field_2.read(), 255);
/// ```
///
/// Don't misuse this method to do a non-volatile read of the referenced value:
///
/// ```
/// use volatile::Volatile;
///
/// let mut value = 5;
/// let mut volatile = Volatile::new(&mut value);
///
/// // DON'T DO THIS:
/// let mut readout = 0;
/// volatile.map(|value| {
/// readout = *value; // non-volatile read, might lead to bugs
/// value
/// });
/// ```
pub fn map<'a, F, U>(&'a self, f: F) -> Volatile<&'a U, A>
where
F: FnOnce(&'a T) -> &'a U,
U: ?Sized,
T: 'a,
{
Volatile {
reference: f(self.reference.deref()),
access: self.access,
}
}
/// Constructs a new mutable `Volatile` reference by mapping the wrapped value.
///
/// This method is useful for accessing individual fields of volatile structs.
///
/// Note that this method gives temporary access to the wrapped reference, which allows
/// accessing the value in a non-volatile way. This is normally not what you want, so
/// **this method should only be used for reference-to-reference transformations**.
///
/// ## Examples
///
/// Accessing a struct field:
///
/// ```
/// use volatile::Volatile;
///
/// struct Example { field_1: u32, field_2: u8, }
/// let mut value = Example { field_1: 15, field_2: 255 };
/// let mut volatile = Volatile::new(&mut value);
///
/// // construct a volatile reference to a field
/// let mut field_2 = volatile.map_mut(|example| &mut example.field_2);
/// field_2.write(128);
/// assert_eq!(field_2.read(), 128);
/// ```
///
/// Don't misuse this method to do a non-volatile read or write of the referenced value:
///
/// ```
/// use volatile::Volatile;
///
/// let mut value = 5;
/// let mut volatile = Volatile::new(&mut value);
///
/// // DON'T DO THIS:
/// volatile.map_mut(|value| {
/// *value = 10; // non-volatile write, might lead to bugs
/// value
/// });
/// ```
pub fn map_mut<'a, F, U>(&'a mut self, f: F) -> Volatile<&'a mut U, A>
where
F: FnOnce(&mut T) -> &mut U,
R: DerefMut,
U: ?Sized,
T: 'a,
{
Volatile {
reference: f(&mut self.reference),
access: self.access,
}
}
}
/// Methods for volatile slices
impl<T, R, A> Volatile<R, A>
where
R: Deref<Target = [T]>,
{
/// Applies the index operation on the wrapped slice.
///
/// Returns a shared `Volatile` reference to the resulting subslice.
///
/// This is a convenience method for the `map(|slice| slice.index(index))` operation, so it
/// has the same behavior as the indexing operation on slice (e.g. panic if index is
/// out-of-bounds).
///
/// ## Examples
///
/// Accessing a single slice element:
///
/// ```
/// use volatile::Volatile;
///
/// let array = [1, 2, 3];
/// let slice = &array[..];
/// let volatile = Volatile::new(slice);
/// assert_eq!(volatile.index(1).read(), 2);
/// ```
///
/// Accessing a subslice:
///
/// ```
/// use volatile::Volatile;
///
/// let array = [1, 2, 3];
/// let slice = &array[..];
/// let volatile = Volatile::new(slice);
/// let subslice = volatile.index(1..);
/// assert_eq!(subslice.index(0).read(), 2);
/// ```
pub fn index<'a, I>(&'a self, index: I) -> Volatile<&'a I::Output, A>
where
I: SliceIndex<[T]>,
T: 'a,
{
self.map(|slice| slice.index(index))
}
/// Applies the mutable index operation on the wrapped slice.
///
/// Returns a mutable `Volatile` reference to the resulting subslice.
///
/// This is a convenience method for the `map_mut(|slice| slice.index_mut(index))`
/// operation, so it has the same behavior as the indexing operation on slice
/// (e.g. panic if index is out-of-bounds).
///
/// ## Examples
///
/// Accessing a single slice element:
///
/// ```
/// use volatile::Volatile;
///
/// let mut array = [1, 2, 3];
/// let slice = &mut array[..];
/// let mut volatile = Volatile::new(slice);
/// volatile.index_mut(1).write(6);
/// assert_eq!(volatile.index(1).read(), 6);
/// ```
///
/// Accessing a subslice:
///
/// ```
/// use volatile::Volatile;
///
/// let mut array = [1, 2, 3];
/// let slice = &mut array[..];
/// let mut volatile = Volatile::new(slice);
/// let mut subslice = volatile.index_mut(1..);
/// subslice.index_mut(0).write(6);
/// assert_eq!(subslice.index(0).read(), 6);
/// ```
pub fn index_mut<'a, I>(&'a mut self, index: I) -> Volatile<&mut I::Output, A>
where
I: SliceIndex<[T]>,
R: DerefMut,
T: 'a,
{
self.map_mut(|slice| slice.index_mut(index))
}
/// Copies all elements from `self` into `dst`, using a volatile memcpy.
///
/// The length of `dst` must be the same as `self`.
///
/// The method is only available with the `unstable` feature enabled (requires a nightly
/// Rust compiler).
///
/// ## Panics
///
/// This function will panic if the two slices have different lengths.
///
/// ## Examples
///
/// Copying two elements from a volatile slice:
///
/// ```
/// use volatile::Volatile;
///
/// let src = [1, 2];
/// // the `Volatile` type does not work with arrays, so convert `src` to a slice
/// let slice = &src[..];
/// let volatile = Volatile::new(slice);
/// let mut dst = [5, 0, 0];
///
/// // Because the slices have to be the same length,
/// // we slice the destination slice from three elements
/// // to two. It will panic if we don't do this.
/// volatile.copy_into_slice(&mut dst[1..]);
///
/// assert_eq!(src, [1, 2]);
/// assert_eq!(dst, [5, 1, 2]);
/// ```
#[cfg(feature = "unstable")]
pub fn copy_into_slice(&self, dst: &mut [T])
where
T: Copy,
{
let src = self.reference.deref();
assert_eq!(
src.len(),
dst.len(),
"destination and source slices have different lengths"
);
unsafe {
intrinsics::volatile_copy_nonoverlapping_memory(
dst.as_mut_ptr(),
src.as_ptr(),
src.len(),
);
}
}
/// Copies all elements from `src` into `self`, using a volatile memcpy.
///
/// The length of `src` must be the same as `self`.
///
/// This method is similar to the `slice::copy_from_slice` method of the standard library. The
/// difference is that this method performs a volatile copy.
///
/// The method is only available with the `unstable` feature enabled (requires a nightly
/// Rust compiler).
///
/// ## Panics
///
/// This function will panic if the two slices have different lengths.
///
/// ## Examples
///
/// Copying two elements from a slice into a volatile slice:
///
/// ```
/// use volatile::Volatile;
///
/// let src = [1, 2, 3, 4];
/// let mut dst = [0, 0];
/// // the `Volatile` type does not work with arrays, so convert `dst` to a slice
/// let slice = &mut dst[..];
/// let mut volatile = Volatile::new(slice);
///
/// // Because the slices have to be the same length,
/// // we slice the source slice from four elements
/// // to two. It will panic if we don't do this.
/// volatile.copy_from_slice(&src[2..]);
///
/// assert_eq!(src, [1, 2, 3, 4]);
/// assert_eq!(dst, [3, 4]);
/// ```
#[cfg(feature = "unstable")]
pub fn copy_from_slice(&mut self, src: &[T])
where
T: Copy,
R: DerefMut,
{
let dest = self.reference.deref_mut();
assert_eq!(
dest.len(),
src.len(),
"destination and source slices have different lengths"
);
unsafe {
intrinsics::volatile_copy_nonoverlapping_memory(
dest.as_mut_ptr(),
src.as_ptr(),
dest.len(),
);
}
}
/// Copies elements from one part of the slice to another part of itself, using a
/// volatile `memmove`.
///
/// `src` is the range within `self` to copy from. `dest` is the starting index of the
/// range within `self` to copy to, which will have the same length as `src`. The two ranges
/// may overlap. The ends of the two ranges must be less than or equal to `self.len()`.
///
/// This method is similar to the `slice::copy_within` method of the standard library. The
/// difference is that this method performs a volatile copy.
///
/// This method is only available with the `unstable` feature enabled (requires a nightly
/// Rust compiler).
///
/// ## Panics
///
/// This function will panic if either range exceeds the end of the slice, or if the end
/// of `src` is before the start.
///
/// ## Examples
///
/// Copying four bytes within a slice:
///
/// ```
/// use volatile::Volatile;
///
/// let mut byte_array = *b"Hello, World!";
/// let mut slice: &mut [u8] = &mut byte_array[..];
/// let mut volatile = Volatile::new(slice);
///
/// volatile.copy_within(1..5, 8);
///
/// assert_eq!(&byte_array, b"Hello, Wello!");
#[cfg(feature = "unstable")]
pub fn copy_within(&mut self, src: impl RangeBounds<usize>, dest: usize)
where
T: Copy,
R: DerefMut,
{
let slice = self.reference.deref_mut();
// implementation taken from https://github.com/rust-lang/rust/blob/683d1bcd405727fcc9209f64845bd3b9104878b8/library/core/src/slice/mod.rs#L2726-L2738
let Range {
start: src_start,
end: src_end,
} = range(src, ..slice.len());
let count = src_end - src_start;
assert!(dest <= slice.len() - count, "dest is out of bounds");
// SAFETY: the conditions for `volatile_copy_memory` have all been checked above,
// as have those for `ptr::add`.
unsafe {
intrinsics::volatile_copy_memory(
slice.as_mut_ptr().add(dest),
slice.as_ptr().add(src_start),
count,
);
}
}
}
/// Methods for volatile byte slices
impl<R, A> Volatile<R, A>
where
R: Deref<Target = [u8]>,
{
/// Sets all elements of the byte slice to the given `value` using a volatile `memset`.
///
/// This method is similar to the `slice::fill` method of the standard library, with the
/// difference that this method performs a volatile write operation. Another difference
/// is that this method is only available for byte slices (not general `&mut [T]` slices)
/// because there currently isn't a instrinsic function that allows non-`u8` values.
///
/// This method is only available with the `unstable` feature enabled (requires a nightly
/// Rust compiler).
///
/// ## Example
///
/// ```rust
/// use volatile::Volatile;
///
/// let mut buf = Volatile::new(vec![0; 10]);
/// buf.fill(1);
/// assert_eq!(buf.extract_inner(), vec![1; 10]);
/// ```
#[cfg(feature = "unstable")]
pub fn fill(&mut self, value: u8)
where
R: DerefMut,
{
let dest = self.reference.deref_mut();
unsafe {
intrinsics::volatile_set_memory(dest.as_mut_ptr(), value, dest.len());
}
}
}
/// Methods for converting arrays to slices
impl<R, A, T, const N: usize> Volatile<R, A>
where
R: Deref<Target = [T; N]>,
{
/// Converts an array reference to a shared slice.
///
/// This makes it possible to use the methods defined on slices.
///
/// ## Example
///
/// Reading a subslice from a volatile array reference using `index`:
///
/// ```
/// use volatile::Volatile;
///
/// let src = [1, 2, 3, 4];
/// let volatile = Volatile::new(&src);
///
/// // convert the `Volatile<&[i32; 4]>` array reference to a `Volatile<&[i32]>` slice
/// let volatile_slice = volatile.as_slice();
/// // we can now use the slice methods
/// let subslice = volatile_slice.index(2..);
///
/// assert_eq!(subslice.index(0).read(), 3);
/// assert_eq!(subslice.index(1).read(), 4);
/// ```
pub fn as_slice(&self) -> Volatile<&[T], A> {
self.map(|array| &array[..])
}
/// Converts a mutable array reference to a mutable slice.
///
/// This makes it possible to use the methods defined on slices.
///
/// ## Example
///
/// Writing to an index of a mutable array reference:
///
/// ```
/// use volatile::Volatile;
///
/// let mut dst = [0, 0];
/// let mut volatile = Volatile::new(&mut dst);
///
/// // convert the `Volatile<&mut [i32; 2]>` array reference to a `Volatile<&mut [i32]>` slice
/// let mut volatile_slice = volatile.as_mut_slice();
/// // we can now use the slice methods
/// volatile_slice.index_mut(1).write(1);
///
/// assert_eq!(dst, [0, 1]);
/// ```
pub fn as_mut_slice(&mut self) -> Volatile<&mut [T], A>
where
R: DerefMut,
{
self.map_mut(|array| &mut array[..])
}
}
/// Methods for restricting access.
impl<R> Volatile<R> {
/// Restricts access permissions to read-only.
///
/// ## Example
///
/// ```
/// use volatile::Volatile;
///
/// let mut value: i16 = -4;
/// let mut volatile = Volatile::new(&mut value);
///
/// let read_only = volatile.read_only();
/// assert_eq!(read_only.read(), -4);
/// // read_only.write(10); // compile-time error
/// ```
pub fn read_only(self) -> Volatile<R, ReadOnly> {
Volatile {
reference: self.reference,
access: PhantomData,
}
}
/// Restricts access permissions to write-only.
///
/// ## Example
///
/// Creating a write-only reference to a struct field:
///
/// ```
/// use volatile::Volatile;
///
/// struct Example { field_1: u32, field_2: u8, }
/// let mut value = Example { field_1: 15, field_2: 255 };
/// let mut volatile = Volatile::new(&mut value);
///
/// // construct a volatile write-only reference to `field_2`
/// let mut field_2 = volatile.map_mut(|example| &mut example.field_2).write_only();
/// field_2.write(14);
/// // field_2.read(); // compile-time error
/// ```
pub fn write_only(self) -> Volatile<R, WriteOnly> {
Volatile {
reference: self.reference,
access: PhantomData,
}
}
}
impl<R, T, A> fmt::Debug for Volatile<R, A>
where
R: Deref<Target = T>,
T: Copy + fmt::Debug,
A: Readable,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Volatile").field(&self.read()).finish()
}
}
impl<R> fmt::Debug for Volatile<R, WriteOnly>
where
R: Deref,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Volatile").field(&"[write-only]").finish()
}
}
#[cfg(test)]
mod tests {
use super::Volatile;
#[test]
fn test_read() {
let val = 42;
assert_eq!(Volatile::new(&val).read(), 42);
}
#[test]
fn test_write() {
let mut val = 50;
let mut volatile = Volatile::new(&mut val);
volatile.write(50);
assert_eq!(val, 50);
}
#[test]
fn test_update() {
let mut val = 42;
let mut volatile = Volatile::new(&mut val);
volatile.update(|v| *v += 1);
assert_eq!(val, 43);
}
#[test]
fn test_slice() {
let mut val = [1, 2, 3];
let mut volatile = Volatile::new(&mut val[..]);
volatile.index_mut(0).update(|v| *v += 1);
assert_eq!(val, [2, 2, 3]);
}
#[test]
fn test_struct() {
struct S {
field_1: u32,
field_2: bool,
}
let mut val = S {
field_1: 60,
field_2: true,
};
let mut volatile = Volatile::new(&mut val);
volatile.map_mut(|s| &mut s.field_1).update(|v| *v += 1);
let mut field_2 = volatile.map_mut(|s| &mut s.field_2);
assert!(field_2.read());
field_2.write(false);
assert_eq!(volatile.map(|s| &s.field_1).read(), 61);
assert_eq!(volatile.map(|s| &s.field_2).read(), false);
}
#[cfg(feature = "unstable")]
#[test]
fn test_chunks() {
let mut val = [1, 2, 3, 4, 5, 6];
let mut volatile = Volatile::new(&mut val[..]);
let mut chunks = volatile.map_mut(|s| s.as_chunks_mut().0);
chunks.index_mut(1).write([10, 11, 12]);
assert_eq!(chunks.index(0).read(), [1, 2, 3]);
assert_eq!(chunks.index(1).read(), [10, 11, 12]);
}
}