Skip to content

Commit b525f38

Browse files
committed
Add const index functions under a new very_unstable feature
1 parent 9f7b015 commit b525f38

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ repository = "https://github.com/rust-osdev/volatile"
1313
[features]
1414
# Enable unstable features; requires Rust nightly; might break on compiler updates
1515
unstable = []
16+
# Enable unstable and experimental features; requires Rust nightly; might break on compiler updates
17+
very_unstable = ["unstable"]
1618

1719
[dev-dependencies]
1820
rand = "0.8.3"
1921

2022
[package.metadata.release]
2123
no-dev-version = true
2224
pre-release-replacements = [
23-
{ file="Changelog.md", search="# Unreleased", replace="# Unreleased\n\n# {{version}} – {{date}}", exactly=1 },
25+
{ file = "Changelog.md", search = "# Unreleased", replace = "# Unreleased\n\n# {{version}} – {{date}}", exactly = 1 },
2426
]
2527
pre-release-commit-message = "Release version {{version}}"
2628

src/lib.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
#![cfg_attr(feature = "unstable", feature(slice_range))]
1111
#![cfg_attr(feature = "unstable", feature(slice_ptr_get))]
1212
#![cfg_attr(feature = "unstable", feature(slice_ptr_len))]
13-
#![cfg_attr(feature = "unstable", allow(incomplete_features))]
13+
#![cfg_attr(feature = "very_unstable", feature(const_slice_ptr_len))]
14+
#![cfg_attr(feature = "very_unstable", feature(const_panic))]
15+
#![cfg_attr(feature = "very_unstable", feature(const_fn_trait_bound))]
16+
#![cfg_attr(feature = "very_unstable", feature(const_fn_fn_ptr_basics))]
17+
#![cfg_attr(feature = "very_unstable", feature(const_trait_impl))]
18+
#![cfg_attr(feature = "very_unstable", feature(const_mut_refs))]
19+
#![cfg_attr(feature = "very_unstable", allow(incomplete_features))]
1420
#![cfg_attr(all(feature = "unstable", test), feature(slice_as_chunks))]
1521
#![warn(missing_docs)]
1622
#![deny(unsafe_op_in_unsafe_fn)]
@@ -353,13 +359,34 @@ where
353359
unsafe { VolatilePtr::new_generic(f(self.pointer)) }
354360
}
355361

362+
#[cfg(feature = "very_unstable")]
363+
pub const unsafe fn map_const<'a, F, U>(
364+
&'a self,
365+
f: F,
366+
) -> VolatilePtr<'a, U, Access<R, access::NoAccess>>
367+
where
368+
F: FnOnce(NonNull<T>) -> NonNull<U>,
369+
U: ?Sized,
370+
{
371+
unsafe { VolatilePtr::new_generic(f(self.pointer)) }
372+
}
373+
356374
pub unsafe fn map_mut<F, U>(&mut self, f: F) -> VolatilePtr<U, Access<R, W>>
357375
where
358376
F: FnOnce(NonNull<T>) -> NonNull<U>,
359377
U: ?Sized,
360378
{
361379
unsafe { VolatilePtr::new_generic(f(self.pointer)) }
362380
}
381+
382+
#[cfg(feature = "very_unstable")]
383+
pub const unsafe fn map_mut_const<F, U>(&mut self, f: F) -> VolatilePtr<U, Access<R, W>>
384+
where
385+
F: FnOnce(NonNull<T>) -> NonNull<U>,
386+
U: ?Sized,
387+
{
388+
unsafe { VolatilePtr::new_generic(f(self.pointer)) }
389+
}
363390
}
364391

365392
/// Methods for volatile slices
@@ -417,6 +444,16 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
417444
unsafe { self.map(|slice| slice.get_unchecked_mut(index)) }
418445
}
419446

447+
#[cfg(feature = "very_unstable")]
448+
pub const fn index_const(&self, index: usize) -> VolatilePtr<T, Access<R, access::NoAccess>> {
449+
assert!(index < self.pointer.len(), "index out of bounds");
450+
unsafe {
451+
self.map_const(|slice| {
452+
NonNull::new_unchecked(slice.as_non_null_ptr().as_ptr().add(index))
453+
})
454+
}
455+
}
456+
420457
pub fn index_mut<I>(
421458
&mut self,
422459
index: I,
@@ -429,6 +466,16 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
429466
unsafe { self.map_mut(|slice| slice.get_unchecked_mut(index)) }
430467
}
431468

469+
#[cfg(feature = "very_unstable")]
470+
pub const fn index_mut_const(&mut self, index: usize) -> VolatilePtr<T, Access<R, W>> {
471+
assert!(index < self.pointer.len(), "index out of bounds");
472+
unsafe {
473+
self.map_mut_const(|slice| {
474+
NonNull::new_unchecked(slice.as_non_null_ptr().as_ptr().add(index))
475+
})
476+
}
477+
}
478+
432479
/// Copies all elements from `self` into `dst`, using a volatile memcpy.
433480
///
434481
/// The length of `dst` must be the same as `self`.

0 commit comments

Comments
 (0)