Skip to content

Commit

Permalink
gate Alloc, Mem, Ptr, Slice, Str unsafe fns with unsafe··
Browse files Browse the repository at this point in the history
This way they can be more flexibly used from other modules.

- fix build with just `unsafe_layout`.
- misc. doc updates.
  • Loading branch information
joseluis committed Jan 15, 2025
1 parent f6115a6 commit 1de3070
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/sys/env/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Env {
/// # Safety
/// See [remove_var].
#[cfg(all(not(feature = "safe_sys"), feature = "unsafe_thread"))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_thread")))]
pub unsafe fn remove_var<K: AsRef<OsStr>>(key: K) {
unsafe { remove_var(key) }
}
Expand All @@ -87,6 +88,7 @@ impl Env {
/// # Safety
/// See [set_var].
#[cfg(all(not(feature = "safe_sys"), feature = "unsafe_thread"))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_thread")))]
pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
unsafe { set_var(key, value) }
}
Expand Down
8 changes: 6 additions & 2 deletions src/sys/mem/alloc/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ impl Alloc {
}
}

#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_layout")))]
#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_layout"))]
/// # Unsafe methods
///
/// ## Features
/// They depend on enabling any `unsafe*` feature, and not enabling `safe_mem`.
#[cfg_attr(feature = "nightly_doc", doc(cfg(unsafe··)))]
#[cfg(all(not(feature = "safe_mem"), unsafe··))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
#[rustfmt::skip]
Expand Down
9 changes: 6 additions & 3 deletions src/sys/mem/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ impl Mem {
}
}

/// # Unsafe methods gated by `unsafe_layout`
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_layout")))]
#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_layout"))]
/// # Unsafe methods
///
/// ## Features
/// They depend on enabling any `unsafe*` feature, and not enabling `safe_mem`.
#[cfg_attr(feature = "nightly_doc", doc(cfg(unsafe··)))]
#[cfg(all(not(feature = "safe_mem"), unsafe··))]
impl Mem {
// NOTE: can't compile, errors with: error[E0512]:
// cannot transmute between types of different sizes, or dependently-sized types
Expand Down
9 changes: 6 additions & 3 deletions src/sys/mem/ptr/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,13 @@ impl Ptr {
}
}

/// # Unsafe methods gated by `unsafe_ptr`
/// # Unsafe methods
///
/// ## Features
/// They depend on enabling any `unsafe*` feature, and not enabling `safe_mem`.
#[rustfmt::skip]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_ptr")))]
#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_ptr"))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(unsafe··)))]
#[cfg(all(not(feature = "safe_mem"), unsafe··))]
impl Ptr {
/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. Can overlap.
///
Expand Down
8 changes: 4 additions & 4 deletions src/sys/mem/slice/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl<T> Slice<T> {
/// See `core::slice::`[`from_raw_parts`]
///
/// See also `Ptr::`[`slice_from_raw_parts`][crate::Ptr::slice_from_raw_parts].
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_slice")))]
#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_slice"))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(unsafe··)))]
#[cfg(all(not(feature = "safe_mem"), unsafe··))]
pub const unsafe fn from_raw_parts<'a>(data: *const T, len: usize) -> &'a [T] {
// SAFETY: Caller must uphold the safety contract.
unsafe { from_raw_parts(data, len) }
Expand All @@ -63,8 +63,8 @@ impl<T> Slice<T> {
/// See `core::slice::`[`from_raw_parts_mut`].
///
/// See also `Ptr::`[`slice_from_raw_parts_mut`][crate::Ptr::slice_from_raw_parts_mut].
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_slice")))]
#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_slice"))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(unsafe··)))]
#[cfg(all(not(feature = "safe_mem"), unsafe··))]
pub const unsafe fn from_raw_parts_mut<'a>(data: *mut T, len: usize) -> &'a mut [T] {
// SAFETY: Caller must uphold the safety contract.
unsafe { from_raw_parts_mut(data, len) }
Expand Down
16 changes: 8 additions & 8 deletions src/text/str/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::{
iif, Ascii, InvalidUtf8, Slice,
_core::str::{from_utf8, from_utf8_mut},
};
#[allow(unused_imports, reason = "unsafe_str only")]
#[allow(unused_imports, reason = "unsafe")]
#[cfg(feature = "alloc")]
use crate::{Box, _dep::_alloc::str::from_boxed_utf8_unchecked};
#[allow(unused_imports, reason = "unsafe_str only")]
#[allow(unused_imports, reason = "unsafe")]
use crate::{
_core::str::{from_utf8_unchecked, from_utf8_unchecked_mut},
sf, unwrap,
Expand Down Expand Up @@ -53,8 +53,8 @@ impl Str {
/// # Safety
/// The bytes passed in must be valid UTF-8.
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_str")))]
#[cfg(all(not(feature = "safe_text"), unsafe··))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(unsafe··)))]
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
// SAFETY: Caller must uphold the safety contract.
unsafe { from_utf8_unchecked(v) }
Expand All @@ -67,8 +67,8 @@ impl Str {
/// # Safety
/// The bytes passed in must be valid UTF-8.
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_str")))]
#[cfg(all(not(feature = "safe_text"), unsafe··))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(unsafe··)))]
pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
// SAFETY: Caller must uphold the safety contract.
unsafe { from_utf8_unchecked_mut(v) }
Expand All @@ -82,8 +82,8 @@ impl Str {
/// The bytes passed in must be valid UTF-8.
#[must_use]
#[cfg(feature = "alloc")]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = "alloc", feature = "unsafe_str"))))]
#[cfg(all(not(feature = "safe_text"), unsafe··))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = "alloc", unsafe··))))]
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
// SAFETY: Caller must uphold the safety contract.
unsafe { from_boxed_utf8_unchecked(v) }
Expand Down

0 comments on commit 1de3070

Please sign in to comment.