Skip to content

Commit 85f5be2

Browse files
committed
Make ZeroVec ULE-clean
1 parent 66769be commit 85f5be2

File tree

11 files changed

+36
-15
lines changed

11 files changed

+36
-15
lines changed

components/casemap/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5+
#![feature(ptr_metadata)]
6+
57
//! Case mapping for Unicode characters and strings.
68
//!
79
//! This module is published as its own crate ([`icu_casemap`](https://docs.rs/icu_casemap/latest/icu_casemap/))

components/collections/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
)
3333
)]
3434
#![warn(missing_docs)]
35+
#![feature(ptr_metadata)]
3536

3637
#[cfg(feature = "alloc")]
3738
extern crate alloc;

components/datetime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5+
#![cfg(ptr_metadata)]
6+
57
//! Localized formatting of dates, times, and time zones.
68
//!
79
//! This module is published as its own crate ([`icu_datetime`](https://docs.rs/icu_datetime/latest/icu_datetime/))

components/decimal/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5+
#![feature(ptr_metadata)]
6+
57
//! Formatting basic decimal numbers.
68
//!
79
//! This module is published as its own crate ([`icu_decimal`](https://docs.rs/icu_decimal/latest/icu_decimal/))

components/locale/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5+
#![feature(ptr_metadata)]
6+
57
//! Canonicalization of locale identifiers based on [`CLDR`] data.
68
//!
79
//! This module is published as its own crate ([`icu_locale`](https://docs.rs/icu_locale/latest/icu_locale/))

components/plurals/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
)
7474
)]
7575
#![warn(missing_docs)]
76+
#![feature(ptr_metadata)]
7677

7778
extern crate alloc;
7879

utils/zerovec/derive/src/varule.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,9 @@ pub fn derive_impl(
117117
// Safety: The invariants of this function allow us to assume bytes is valid, and
118118
// having at least #ule_size bytes is a validity constraint for the ULE type.
119119
let unsized_bytes = bytes.get_unchecked(#ule_size..);
120-
let unsized_ref = <#unsized_field as zerovec::ule::VarULE>::from_bytes_unchecked(unsized_bytes);
121-
// We should use the pointer metadata APIs here when they are stable: https://github.com/rust-lang/rust/issues/81513
122-
// For now we rely on all DST metadata being a usize to extract it via a fake slice pointer
123-
let (_ptr, metadata): (usize, usize) = ::core::mem::transmute(unsized_ref);
124-
let entire_struct_as_slice: *const [u8] = ::core::slice::from_raw_parts(bytes.as_ptr(), metadata);
125-
&*(entire_struct_as_slice as *const Self)
120+
let metadata = core::ptr::metadata(unsized_bytes);
121+
122+
&*core::ptr::from_raw_parts::<Self>(bytes as *const [u8] as *const u8, metadata)
126123
}
127124
}
128125
}

utils/zerovec/src/cow.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,14 @@ impl<'a, V: VarULE + ?Sized> VarZeroCow<'a, V> {
185185
/// Construct a new borrowed version of this
186186
#[cfg(feature = "alloc")]
187187
pub fn new_owned(val: Box<V>) -> Self {
188-
let val = ManuallyDrop::new(val);
189-
let buf: NonNull<[u8]> = val.as_bytes().into();
188+
let len = val.as_bytes().len();
189+
let raw_v = Box::into_raw(val) as *mut V;
190+
// disallowed?
191+
// let raw_u8: *mut u8 = raw_v as *mut [u8] as *mut u8;
192+
let raw_u8: *mut u8 = raw_v as *mut () as *mut u8;
193+
194+
let buf: NonNull<[u8]> =
195+
unsafe { NonNull::new_unchecked(core::ptr::slice_from_raw_parts_mut(raw_u8, len)) };
190196
let raw = RawVarZeroCow {
191197
// Invariants upheld:
192198
// 1 & 3: The bytes came from `val` so they're a valid value and byte slice

utils/zerovec/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5+
#![feature(ptr_metadata)]
56
//! Zero-copy vector abstractions for arbitrary types, backed by byte slices.
67
//!
78
//! `zerovec` enables a far wider range of types — beyond just `&[u8]` and `&str` — to participate in

utils/zerovec/src/ule/encode.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,18 @@ pub fn encode_varule_to_box<S: EncodeAsVarULE<T> + ?Sized, T: VarULE + ?Sized>(x
9797
// zero-fill the vector to avoid uninitialized data UB
9898
let mut vec: Vec<u8> = vec![0; x.encode_var_ule_len()];
9999
x.encode_var_ule_write(&mut vec);
100-
let boxed = mem::ManuallyDrop::new(vec.into_boxed_slice());
100+
let boxed = vec.into_boxed_slice();
101101
unsafe {
102102
// Safety: `ptr` is a box, and `T` is a VarULE which guarantees it has the same memory layout as `[u8]`
103103
// and can be recouped via from_bytes_unchecked()
104-
let ptr: *mut T = T::from_bytes_unchecked(&boxed) as *const T as *mut T;
104+
let ptr = T::from_bytes_unchecked(&boxed) as *const T;
105+
let metadata = core::ptr::metadata(ptr);
105106

106107
// Safety: we can construct an owned version since we have mem::forgotten the older owner
107-
Box::from_raw(ptr)
108+
Box::from_raw(core::ptr::from_raw_parts_mut(
109+
Box::into_raw(boxed) as *mut [u8] as *mut u8,
110+
metadata,
111+
))
108112
}
109113
}
110114

0 commit comments

Comments
 (0)