aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Galenson <jgalenson@google.com>2021-04-08 07:30:18 -0700
committerJoel Galenson <jgalenson@google.com>2021-04-08 07:37:12 -0700
commit4ba1537d75289a0f796ebf9b3ab44233804455df (patch)
tree462e922c86b61b4985943638b6929a602d512791
parent666af1f2c190afef6c1e7e42611486e4289097bc (diff)
downloadmemoffset-4ba1537d75289a0f796ebf9b3ab44233804455df.tar.gz
Upgrade rust/crates/memoffset to 0.6.3
Test: make Change-Id: I9277d354259896b50e617b7822ffeeedcaff4928
-rw-r--r--.cargo_vcs_info.json2
-rw-r--r--.travis.yml3
-rw-r--r--Android.bp4
-rw-r--r--Cargo.toml3
-rw-r--r--Cargo.toml.orig3
-rw-r--r--METADATA10
-rw-r--r--README.md5
-rw-r--r--TEST_MAPPING5
-rw-r--r--build.rs3
-rw-r--r--patches/std.diff24
-rw-r--r--src/lib.rs21
-rw-r--r--src/offset_of.rs20
-rw-r--r--src/raw_field.rs20
-rw-r--r--src/span_of.rs42
14 files changed, 88 insertions, 77 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 3621e2c..fcba615 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,5 @@
{
"git": {
- "sha1": "da57ff39e1007a5cb2b50380bb235d067b648846"
+ "sha1": "9846cd17f381a5a9b478267ca5c684ab59346283"
}
}
diff --git a/.travis.yml b/.travis.yml
index 1bf5ede..d3c4d65 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -11,8 +11,11 @@ matrix:
- sh ci/miri.sh
- rust: 1.19.0 # Oldest supported (first version with numeric fields in struct patterns)
+ - rust: 1.20.0 # Oldest supported with tuple_ty
- rust: 1.31.0 # Oldest supported with allow(clippy)
- rust: 1.36.0 # Oldest supported with MaybeUninit
+ - rust: 1.40.0 # Oldest supported with cfg(doctest)
+ - rust: 1.51.0 # Oldest supported with ptr::addr_of!
- rust: stable
- rust: beta
- rust: nightly
diff --git a/Android.bp b/Android.bp
index 5b98902..ae913ce 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,4 +1,5 @@
// This file is generated by cargo2android.py --run --device --tests --dependencies --patch=patches/Android.bp.patch.
+// Do not modify this file as changes will be overridden on upgrade.
package {
default_applicable_licenses: ["external_rust_crates_memoffset_license"],
@@ -59,6 +60,9 @@ rust_defaults {
rust_test_host {
name: "memoffset_host_test_src_lib",
defaults: ["memoffset_defaults"],
+ test_options: {
+ unit_test: true,
+ },
}
rust_test {
diff --git a/Cargo.toml b/Cargo.toml
index 9e2874f..e273757 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,7 +12,7 @@
[package]
name = "memoffset"
-version = "0.6.1"
+version = "0.6.3"
authors = ["Gilad Naaman <gilad.naaman@gmail.com>"]
description = "offset_of functionality for Rust structs."
readme = "README.md"
@@ -28,4 +28,3 @@ version = "1"
[features]
default = []
unstable_const = []
-unstable_raw = []
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 61e20d9..9299a64 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
[package]
name = "memoffset"
-version = "0.6.1"
+version = "0.6.3"
authors = ["Gilad Naaman <gilad.naaman@gmail.com>"]
description = "offset_of functionality for Rust structs."
license = "MIT"
@@ -18,4 +18,3 @@ doc-comment = "0.3"
[features]
default = []
unstable_const = []
-unstable_raw = []
diff --git a/METADATA b/METADATA
index c803657..3e6b088 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@ third_party {
}
url {
type: ARCHIVE
- value: "https://static.crates.io/crates/memoffset/memoffset-0.6.1.crate"
+ value: "https://static.crates.io/crates/memoffset/memoffset-0.6.3.crate"
}
- version: "0.6.1"
+ version: "0.6.3"
license_type: NOTICE
last_upgrade_date {
- year: 2020
- month: 12
- day: 15
+ year: 2021
+ month: 4
+ day: 8
}
}
diff --git a/README.md b/README.md
index 67ce9eb..a60f288 100644
--- a/README.md
+++ b/README.md
@@ -73,8 +73,3 @@ Your crate root: (`lib.rs`/`main.rs`)
```
If you intend to use `offset_of!` inside a `const fn`, also add the `const_fn` compiler feature.
-
-### Raw references ###
-Recent nightlies support [a way to create raw pointers](https://github.com/rust-lang/rust/issues/73394) that avoids creating intermediate safe references.
-`memoffset` can make use of that feature to avoid what is technically Undefined Behavior.
-Use the `unstable_raw` feature to enable this.
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 9649938..71c00fc 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -1,7 +1,10 @@
-// Generated by cargo2android.py for tests in Android.bp
+// Generated by update_crate_tests.py for tests that depend on this crate.
{
"presubmit": [
{
+ "name": "crossbeam-epoch_device_test_src_lib"
+ },
+ {
"name": "memoffset_device_test_src_lib"
}
]
diff --git a/build.rs b/build.rs
index c83bff2..0604c19 100644
--- a/build.rs
+++ b/build.rs
@@ -16,4 +16,7 @@ fn main() {
if ac.probe_rustc_version(1, 40) {
println!("cargo:rustc-cfg=doctests");
}
+ if ac.probe_rustc_version(1, 51) {
+ println!("cargo:rustc-cfg=raw_ref_macros");
+ }
}
diff --git a/patches/std.diff b/patches/std.diff
index 8238bdd..fc4bdbc 100644
--- a/patches/std.diff
+++ b/patches/std.diff
@@ -7,23 +7,23 @@ index c85fb01..25b0444 100644
//! ```
-#![no_std]
-+// ANDROID: include standard library to build as a dynlib
++// ANDROID: include standard library to build as a dylib
+//#![no_std]
#![cfg_attr(
feature = "unstable_const",
feature(
-@@ -80,9 +81,9 @@ doctest!("../README.md");
- // This `use` statement enables the macros to use `$crate::mem`.
- // Doing this enables this crate to function under both std and no-std crates.
+@@ -78,9 +79,9 @@
#[doc(hidden)]
--pub use core::mem;
-+pub use std::mem; // ANDROID: use std instead of core, since we're not building wiht no-std.
- #[doc(hidden)]
--pub use core::ptr;
-+pub use std::ptr; // ANDROID: use std instead of core, since we're not building wiht no-std.
+ pub mod __priv {
+ #[doc(hidden)]
+- pub use core::mem;
++ pub use std::mem; // ANDROID: use std instead of core, since we're not building with no-std.
+ #[doc(hidden)]
+- pub use core::ptr;
++ pub use std::ptr; // ANDROID: use std instead of core, since we're not building with no-std.
- #[macro_use]
- mod raw_field;
+ /// Use type inference to obtain the size of the pointee (without actually using the pointer).
+ #[doc(hidden)]
diff --git a/src/span_of.rs b/src/span_of.rs
index 0592dbd..369e5c6 100644
--- a/src/span_of.rs
@@ -37,4 +37,4 @@ index 0592dbd..369e5c6 100644
+ use std::mem;
#[test]
- fn span_simple() {
+ fn span_simple() { \ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
index 25b0444..50d3e10 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -56,7 +56,7 @@
//! let checksum = crc16(checksum_range);
//! ```
-// ANDROID: include standard library to build as a dynlib
+// ANDROID: include standard library to build as a dylib
//#![no_std]
#![cfg_attr(
feature = "unstable_const",
@@ -68,7 +68,6 @@
const_raw_ptr_deref,
)
)]
-#![cfg_attr(feature = "unstable_raw", feature(raw_ref_macros))]
#[macro_use]
#[cfg(doctests)]
@@ -78,12 +77,20 @@ extern crate doc_comment;
#[cfg(doctest)]
doctest!("../README.md");
-// This `use` statement enables the macros to use `$crate::mem`.
-// Doing this enables this crate to function under both std and no-std crates.
+/// Hiden module for things the macros need to access.
#[doc(hidden)]
-pub use std::mem; // ANDROID: use std instead of core, since we're not building wiht no-std.
-#[doc(hidden)]
-pub use std::ptr; // ANDROID: use std instead of core, since we're not building wiht no-std.
+pub mod __priv {
+ #[doc(hidden)]
+ pub use std::mem; // ANDROID: use std instead of core, since we're not building with no-std.
+ #[doc(hidden)]
+ pub use std::ptr; // ANDROID: use std instead of core, since we're not building with no-std.
+
+ /// Use type inference to obtain the size of the pointee (without actually using the pointer).
+ #[doc(hidden)]
+ pub fn size_of_pointee<T>(_ptr: *const T) -> usize {
+ mem::size_of::<T>()
+ }
+}
#[macro_use]
mod raw_field;
diff --git a/src/offset_of.rs b/src/offset_of.rs
index 2ffb79c..a363d30 100644
--- a/src/offset_of.rs
+++ b/src/offset_of.rs
@@ -30,7 +30,7 @@ macro_rules! _memoffset__let_base_ptr {
// so it has to be in the same scope as `$name`. That's why
// `let_base_ptr` declares a variable (several, actually)
// instead of returning one.
- let uninit = $crate::mem::MaybeUninit::<$type>::uninit();
+ let uninit = $crate::__priv::mem::MaybeUninit::<$type>::uninit();
let $name: *const $type = uninit.as_ptr();
};
}
@@ -41,7 +41,7 @@ macro_rules! _memoffset__let_base_ptr {
($name:ident, $type:ty) => {
// No UB right here, but we will later dereference this pointer to
// offset into a field, and that is UB because the pointer is dangling.
- let $name = $crate::mem::align_of::<$type>() as *const $type;
+ let $name = $crate::__priv::mem::align_of::<$type>() as *const $type;
};
}
@@ -49,17 +49,19 @@ macro_rules! _memoffset__let_base_ptr {
#[cfg(feature = "unstable_const")]
#[macro_export]
#[doc(hidden)]
-macro_rules! _memoffset_offset_from {
- ($field:expr, $base:expr) => {
+macro_rules! _memoffset_offset_from_unsafe {
+ ($field:expr, $base:expr) => {{
+ let field = $field; // evaluate $field outside the `unsafe` block
+ let base = $base; // evaluate $base outside the `unsafe` block
// Compute offset, with unstable `offset_from` for const-compatibility.
// (Requires the pointers to not dangle, but we already need that for `raw_field!` anyway.)
- unsafe { ($field as *const u8).offset_from($base as *const u8) as usize }
- };
+ unsafe { (field as *const u8).offset_from(base as *const u8) as usize }
+ }};
}
#[cfg(not(feature = "unstable_const"))]
#[macro_export]
#[doc(hidden)]
-macro_rules! _memoffset_offset_from {
+macro_rules! _memoffset_offset_from_unsafe {
($field:expr, $base:expr) => {
// Compute offset.
($field as usize) - ($base as usize)
@@ -93,7 +95,7 @@ macro_rules! offset_of {
// Get field pointer.
let field_ptr = raw_field!(base_ptr, $parent, $field);
// Compute offset.
- _memoffset_offset_from!(field_ptr, base_ptr)
+ _memoffset_offset_from_unsafe!(field_ptr, base_ptr)
}};
}
@@ -117,7 +119,7 @@ macro_rules! offset_of_tuple {
// Get field pointer.
let field_ptr = raw_field_tuple!(base_ptr, $parent, $field);
// Compute offset.
- _memoffset_offset_from!(field_ptr, base_ptr)
+ _memoffset_offset_from_unsafe!(field_ptr, base_ptr)
}};
}
diff --git a/src/raw_field.rs b/src/raw_field.rs
index 16d01bb..a8dd2b3 100644
--- a/src/raw_field.rs
+++ b/src/raw_field.rs
@@ -18,22 +18,22 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
-/// `raw_const!`, or just ref-then-cast when that is not available.
-#[cfg(feature = "unstable_raw")]
+/// `addr_of!`, or just ref-then-cast when that is not available.
+#[cfg(raw_ref_macros)]
#[macro_export]
#[doc(hidden)]
-macro_rules! _memoffset__raw_const {
+macro_rules! _memoffset__addr_of {
($path:expr) => {{
- $crate::ptr::raw_const!($path)
+ $crate::__priv::ptr::addr_of!($path)
}};
}
-#[cfg(not(feature = "unstable_raw"))]
+#[cfg(not(raw_ref_macros))]
#[macro_export]
#[doc(hidden)]
-macro_rules! _memoffset__raw_const {
+macro_rules! _memoffset__addr_of {
($path:expr) => {{
// This is UB because we create an intermediate reference to uninitialized memory.
- // Nothing we can do about that without `raw_const!` though.
+ // Nothing we can do about that without `addr_of!` though.
&$path as *const _
}};
}
@@ -82,13 +82,14 @@ macro_rules! _memoffset__field_check_tuple {
macro_rules! raw_field {
($base:expr, $parent:path, $field:tt) => {{
_memoffset__field_check!($parent, $field);
+ let base = $base; // evaluate $base outside the `unsafe` block
// Get the field address.
// Crucially, we know that this will not trigger a deref coercion because
// of the field check we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
- _memoffset__raw_const!((*($base as *const $parent)).$field)
+ _memoffset__addr_of!((*(base as *const $parent)).$field)
}
}};
}
@@ -103,13 +104,14 @@ macro_rules! raw_field {
macro_rules! raw_field_tuple {
($base:expr, $parent:ty, $field:tt) => {{
_memoffset__field_check_tuple!($parent, $field);
+ let base = $base; // evaluate $base outside the `unsafe` block
// Get the field address.
// Crucially, we know that this will not trigger a deref coercion because
// of the field check we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
- _memoffset__raw_const!((*($base as *const $parent)).$field)
+ _memoffset__addr_of!((*(base as *const $parent)).$field)
}
}};
}
diff --git a/src/span_of.rs b/src/span_of.rs
index 369e5c6..5fa11ae 100644
--- a/src/span_of.rs
+++ b/src/span_of.rs
@@ -95,23 +95,21 @@ macro_rules! span_of {
};
// No explicit begin for range.
(@helper $root:ident, $parent:path, [] ..) => {{
- // UB due to taking references to uninitialized fields for `size_of_val`.
($root as usize,
- $root as usize + $crate::mem::size_of_val(&(*$root)))
+ $root as usize + $crate::__priv::size_of_pointee($root))
}};
- (@helper $root:ident, $parent:path, [] ..= $field:tt) => {{
- // UB due to taking references to uninitialized fields for `size_of_val`.
- ($root as usize,
- raw_field!($root, $parent, $field) as usize + $crate::mem::size_of_val(&(*$root).$field))
+ (@helper $root:ident, $parent:path, [] ..= $end:tt) => {{
+ let end = raw_field!($root, $parent, $end);
+ ($root as usize, end as usize + $crate::__priv::size_of_pointee(end))
}};
- (@helper $root:ident, $parent:path, [] .. $field:tt) => {{
- ($root as usize, raw_field!($root, $parent, $field) as usize)
+ (@helper $root:ident, $parent:path, [] .. $end:tt) => {{
+ ($root as usize, raw_field!($root, $parent, $end) as usize)
}};
// Explicit begin and end for range.
(@helper $root:ident, $parent:path, # $begin:tt [] ..= $end:tt) => {{
- // UB due to taking references to uninitialized fields for `size_of_val`.
- (raw_field!($root, $parent, $begin) as usize,
- raw_field!($root, $parent, $end) as usize + $crate::mem::size_of_val(&(*$root).$end))
+ let begin = raw_field!($root, $parent, $begin);
+ let end = raw_field!($root, $parent, $end);
+ (begin as usize, end as usize + $crate::__priv::size_of_pointee(end))
}};
(@helper $root:ident, $parent:path, # $begin:tt [] .. $end:tt) => {{
(raw_field!($root, $parent, $begin) as usize,
@@ -119,19 +117,17 @@ macro_rules! span_of {
}};
// No explicit end for range.
(@helper $root:ident, $parent:path, # $begin:tt [] ..) => {{
- // UB due to taking references to uninitialized fields for `size_of_val`.
(raw_field!($root, $parent, $begin) as usize,
- $root as usize + $crate::mem::size_of_val(&*$root))
+ $root as usize + $crate::__priv::size_of_pointee($root))
}};
(@helper $root:ident, $parent:path, # $begin:tt [] ..=) => {{
_memoffset__compile_error!(
"Found inclusive range to the end of a struct. Did you mean '..' instead of '..='?")
}};
// Just one field.
- (@helper $root:ident, $parent:path, # $begin:tt []) => {{
- // UB due to taking references to uninitialized fields for `size_of_val`.
- (raw_field!($root, $parent, $begin) as usize,
- raw_field!($root, $parent, $begin) as usize + $crate::mem::size_of_val(&(*$root).$begin))
+ (@helper $root:ident, $parent:path, # $field:tt []) => {{
+ let field = raw_field!($root, $parent, $field);
+ (field as usize, field as usize + $crate::__priv::size_of_pointee(field))
}};
// Parsing.
(@helper $root:ident, $parent:path, $(# $begin:tt)+ [] $tt:tt $($rest:tt)*) => {{
@@ -143,13 +139,11 @@ macro_rules! span_of {
// Entry point.
($sty:path, $($exp:tt)+) => ({
- unsafe {
- // Get a base pointer.
- _memoffset__let_base_ptr!(root, $sty);
- let base = root as usize;
- let (begin, end) = span_of!(@helper root, $sty, [] $($exp)*);
- begin-base..end-base
- }
+ // Get a base pointer.
+ _memoffset__let_base_ptr!(root, $sty);
+ let base = root as usize;
+ let (begin, end) = span_of!(@helper root, $sty, [] $($exp)*);
+ begin-base..end-base
});
}