mirror of
https://github.com/Lokathor/bytemuck.git
synced 2025-02-16 08:52:25 +00:00
Fix a Miri failure
This commit is contained in:
parent
79143a6295
commit
3fd763c764
@ -4,7 +4,7 @@ git:
|
||||
quiet: true
|
||||
|
||||
rust:
|
||||
- 1.36.0
|
||||
- 1.38.0
|
||||
- stable
|
||||
- beta
|
||||
- nightly
|
||||
|
@ -1,5 +1,5 @@
|
||||
[![License:BlueOak-1.0.0](https://img.shields.io/badge/License-BlueOak_1.0.0-brightgreen.svg)](https://blueoakcouncil.org/license/1.0.0)
|
||||
![Minimum Rust Version](https://img.shields.io/badge/Min%20Rust-1.36-green.svg)
|
||||
![Minimum Rust Version](https://img.shields.io/badge/Min%20Rust-1.38-green.svg)
|
||||
[![travis.ci](https://travis-ci.org/Lokathor/bytemuck.svg?branch=master)](https://travis-ci.org/Lokathor/bytemuck)
|
||||
[![AppVeyor](https://ci.appveyor.com/api/projects/status/hgr4if0snmkmqj88/branch/master?svg=true)](https://ci.appveyor.com/project/Lokathor/bytemuck/branch/master)
|
||||
[![crates.io](https://img.shields.io/crates/v/bytemuck.svg)](https://crates.io/crates/bytemuck)
|
||||
|
@ -14,13 +14,13 @@ matrix:
|
||||
environment:
|
||||
matrix:
|
||||
# Stable
|
||||
- channel: 1.36.0
|
||||
- channel: 1.38.0
|
||||
target: i686-pc-windows-msvc
|
||||
- channel: 1.36.0
|
||||
- channel: 1.38.0
|
||||
target: i686-pc-windows-gnu
|
||||
- channel: 1.36.0
|
||||
- channel: 1.38.0
|
||||
target: x86_64-pc-windows-msvc
|
||||
- channel: 1.36.0
|
||||
- channel: 1.38.0
|
||||
target: x86_64-pc-windows-gnu
|
||||
# Beta and Nightly are checked by TravisCI since builds there run in
|
||||
# parallel.
|
||||
|
@ -33,7 +33,7 @@ pub fn try_cast_box<A: Pod, B: Pod>(input: Box<A>) -> Result<Box<B>, (PodCastErr
|
||||
Err((PodCastError::SizeMismatch, input))
|
||||
} else {
|
||||
// Note(Lokathor): This is much simpler than with the Vec casting!
|
||||
let ptr: *mut B = Box::into_raw(input) as *mut B;
|
||||
let ptr: *mut B = Box::into_raw(input).cast::<B>();
|
||||
Ok(unsafe { Box::from_raw(ptr) })
|
||||
}
|
||||
}
|
||||
@ -59,7 +59,7 @@ pub fn try_zeroed_box<T: Zeroable>() -> Result<Box<T>, ()> {
|
||||
// we don't know what the error is because `alloc_zeroed` is a dumb API
|
||||
Err(())
|
||||
} else {
|
||||
Ok(unsafe { Box::<T>::from_raw(ptr as *mut T) })
|
||||
Ok(unsafe { Box::<T>::from_raw(ptr.cast::<T>()) })
|
||||
}
|
||||
}
|
||||
|
||||
@ -111,7 +111,7 @@ pub fn try_cast_vec<A: Pod, B: Pod>(input: Vec<A>) -> Result<Vec<B>, (PodCastErr
|
||||
// type, and then make a new Vec to return. This works all the way back to
|
||||
// 1.7, if you're on 1.37 or later you can use `Vec::as_mut_ptr` directly.
|
||||
let vec_ptr: *mut A = Vec::as_mut_slice(&mut *manual_drop_vec).as_mut_ptr();
|
||||
let ptr: *mut B = vec_ptr as *mut B;
|
||||
let ptr: *mut B = vec_ptr.cast::<B>();
|
||||
Ok(unsafe { Vec::from_raw_parts(ptr, length, capacity) })
|
||||
}
|
||||
}
|
||||
|
18
src/lib.rs
18
src/lib.rs
@ -136,8 +136,8 @@ pub fn try_cast<A: Pod, B: Pod>(a: A) -> Result<B, PodCastError> {
|
||||
let mut b = B::zeroed();
|
||||
// Note(Lokathor): We copy in terms of `u8` because that allows us to bypass
|
||||
// any potential alignment difficulties.
|
||||
let ap = &a as *const A as *const u8;
|
||||
let bp = &mut b as *mut B as *mut u8;
|
||||
let ap = (&a as *const A).cast::<u8>();
|
||||
let bp = (&mut b as *mut B).cast::<u8>();
|
||||
unsafe { ap.copy_to_nonoverlapping(bp, size_of::<A>()) };
|
||||
Ok(b)
|
||||
} else {
|
||||
@ -158,7 +158,7 @@ pub fn try_cast_ref<A: Pod, B: Pod>(a: &A) -> Result<&B, PodCastError> {
|
||||
if align_of::<B>() > align_of::<A>() && (a as *const A as usize) % align_of::<B>() != 0 {
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else if size_of::<B>() == size_of::<A>() {
|
||||
Ok(unsafe { &*(a as *const A as *const B) })
|
||||
Ok(unsafe { &*(a as *const A).cast::<B>() })
|
||||
} else {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
}
|
||||
@ -174,7 +174,7 @@ pub fn try_cast_mut<A: Pod, B: Pod>(a: &mut A) -> Result<&mut B, PodCastError> {
|
||||
if align_of::<B>() > align_of::<A>() && (a as *mut A as usize) % align_of::<B>() != 0 {
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else if size_of::<B>() == size_of::<A>() {
|
||||
Ok(unsafe { &mut *(a as *mut A as *mut B) })
|
||||
Ok(unsafe { &mut *(a as *mut A).cast::<B>() })
|
||||
} else {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
}
|
||||
@ -203,12 +203,12 @@ pub fn try_cast_slice<A: Pod, B: Pod>(a: &[A]) -> Result<&[B], PodCastError> {
|
||||
if align_of::<B>() > align_of::<A>() && (a.as_ptr() as usize) % align_of::<B>() != 0 {
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else if size_of::<B>() == size_of::<A>() {
|
||||
Ok(unsafe { core::slice::from_raw_parts(a.as_ptr() as *const B, a.len()) })
|
||||
Ok(unsafe { core::slice::from_raw_parts(a.as_ptr().cast::<B>(), a.len()) })
|
||||
} else if size_of::<A>() == 0 || size_of::<B>() == 0 {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
} else if core::mem::size_of_val(a) % size_of::<B>() == 0 {
|
||||
let new_len = core::mem::size_of_val(a) / size_of::<B>();
|
||||
Ok(unsafe { core::slice::from_raw_parts(a.as_ptr() as *const B, new_len) })
|
||||
Ok(unsafe { core::slice::from_raw_parts(a.as_ptr().cast::<B>(), new_len) })
|
||||
} else {
|
||||
Err(PodCastError::OutputSliceWouldHaveSlop)
|
||||
}
|
||||
@ -221,15 +221,15 @@ pub fn try_cast_slice<A: Pod, B: Pod>(a: &[A]) -> Result<&[B], PodCastError> {
|
||||
pub fn try_cast_slice_mut<A: Pod, B: Pod>(a: &mut [A]) -> Result<&mut [B], PodCastError> {
|
||||
// Note(Lokathor): everything with `align_of` and `size_of` will optimize away
|
||||
// after monomorphization.
|
||||
if align_of::<B>() > align_of::<A>() && (a.as_ptr() as usize) % align_of::<B>() != 0 {
|
||||
if align_of::<B>() > align_of::<A>() && (a.as_mut_ptr() as usize) % align_of::<B>() != 0 {
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else if size_of::<B>() == size_of::<A>() {
|
||||
Ok(unsafe { core::slice::from_raw_parts_mut(a.as_ptr() as *mut B, a.len()) })
|
||||
Ok(unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr().cast::<B>(), a.len()) })
|
||||
} else if size_of::<A>() == 0 || size_of::<B>() == 0 {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
} else if core::mem::size_of_val(a) % size_of::<B>() == 0 {
|
||||
let new_len = core::mem::size_of_val(a) / size_of::<B>();
|
||||
Ok(unsafe { core::slice::from_raw_parts_mut(a.as_ptr() as *mut B, new_len) })
|
||||
Ok(unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr().cast::<B>(), new_len) })
|
||||
} else {
|
||||
Err(PodCastError::OutputSliceWouldHaveSlop)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user