Skip to content

Commit 0018d87

Browse files
committed
init modules cstr8 and cstr16 (plus dependencies)
1 parent 80cf03e commit 0018d87

File tree

11 files changed

+1165
-7
lines changed

11 files changed

+1165
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ bit_field = "0.10"
1616
[features]
1717
default = []
1818
alloc = []
19+
unstable = []

src/encoding/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ macro_rules! ucs2_cstr {
8282
// Use `const` values here to force errors to happen at compile
8383
// time.
8484

85-
const NUM_CHARS: usize = match $crate::str_num_ucs2_chars($s) {
85+
const NUM_CHARS: usize = match $crate::encoding::str_num_ucs2_chars($s) {
8686
// Add one for the null char.
8787
Ok(num) => num + 1,
8888
Err(_) => panic!("input contains a character which cannot be represented in UCS-2"),
8989
};
9090

91-
const VAL: [u16; NUM_CHARS] = match $crate::str_to_ucs2($s) {
91+
const VAL: [u16; NUM_CHARS] = match $crate::encoding::str_to_ucs2($s) {
9292
Ok(val) => val,
9393
// The string was already checked by `str_num_ucs2_chars`,
9494
// so this error is unreachable.

src/encoding/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
mod macros;
44

5+
pub use crate::ucs2_cstr;
56
/// These need to be public for the `ucs2_cstr!` macro, but are not
67
/// intended to be called directly.
78
#[doc(hidden)]

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
extern crate alloc;
99

1010
pub mod encoding;
11+
pub mod polyfill;
1112
pub mod types;

src/polyfill.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Polyfills for functions in the standard library that are currently gated
2+
//! behind unstable features.
3+
4+
use core::mem::MaybeUninit;
5+
#[cfg(feature = "alloc")]
6+
use {alloc::vec::Vec, core::mem::ManuallyDrop};
7+
8+
/// Polyfill for the unstable `MaybeUninit::slice_assume_init_ref` function.
9+
///
10+
/// See <https://github.com/rust-lang/rust/issues/63569>.
11+
pub const unsafe fn maybe_uninit_slice_assume_init_ref<T>(s: &[MaybeUninit<T>]) -> &[T] {
12+
unsafe { &*(s as *const [MaybeUninit<T>] as *const [T]) }
13+
}
14+
15+
/// Polyfill for the unstable `MaybeUninit::slice_as_mut_ptr` function.
16+
///
17+
/// See <https://github.com/rust-lang/rust/issues/63569>.
18+
pub fn maybe_uninit_slice_as_mut_ptr<T>(s: &mut [MaybeUninit<T>]) -> *mut T {
19+
s.as_mut_ptr().cast::<T>()
20+
}
21+
22+
/// Polyfill for the unstable `Vec::into_raw_parts` function.
23+
///
24+
/// See <https://github.com/rust-lang/rust/issues/65816>.
25+
#[cfg(feature = "alloc")]
26+
pub fn vec_into_raw_parts<T>(v: Vec<T>) -> (*mut T, usize, usize) {
27+
let mut v = ManuallyDrop::new(v);
28+
(v.as_mut_ptr(), v.len(), v.capacity())
29+
}

0 commit comments

Comments
 (0)