mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-30 08:12:05 +00:00
Bug 1897088 - Add a bytemuck::Pod bound to Device::update_vbo_data. r=gfx-reviewers,supply-chain-reviewers,nical
This unveiled a couple UB-ish uses (missing repr(C) or use of enums). Differential Revision: https://phabricator.services.mozilla.com/D210752
This commit is contained in:
parent
769f66bc78
commit
e77d680ea2
23
Cargo.lock
generated
23
Cargo.lock
generated
@ -591,6 +591,26 @@ version = "3.15.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa"
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15"
|
||||
dependencies = [
|
||||
"bytemuck_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck_derive"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4da9a32f3fed317401fa3c862968128267c3106685286e15d5aaa3d7389c2f60"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
@ -1709,6 +1729,7 @@ version = "0.22.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b52c2ef4a78da0ba68fbe1fd920627411096d2ac478f7f4c9f3a54ba6705bade"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"num-traits",
|
||||
"serde",
|
||||
]
|
||||
@ -6531,6 +6552,7 @@ dependencies = [
|
||||
"bincode",
|
||||
"bitflags 2.5.0",
|
||||
"build-parallel",
|
||||
"bytemuck",
|
||||
"byteorder",
|
||||
"derive_more 0.99.999",
|
||||
"etagere",
|
||||
@ -6567,6 +6589,7 @@ version = "0.62.0"
|
||||
dependencies = [
|
||||
"app_units",
|
||||
"bitflags 2.5.0",
|
||||
"bytemuck",
|
||||
"byteorder",
|
||||
"crossbeam-channel",
|
||||
"euclid",
|
||||
|
29
gfx/wr/Cargo.lock
generated
29
gfx/wr/Cargo.lock
generated
@ -224,9 +224,23 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899"
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.2.0"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37fa13df2292ecb479ec23aa06f4507928bef07839be9ef15281411076629431"
|
||||
checksum = "78834c15cb5d5efe3452d58b1e8ba890dd62d21907f867f383358198e56ebca5"
|
||||
dependencies = [
|
||||
"bytemuck_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck_derive"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4da9a32f3fed317401fa3c862968128267c3106685286e15d5aaa3d7389c2f60"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.25",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
@ -793,10 +807,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "euclid"
|
||||
version = "0.22.6"
|
||||
version = "0.22.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da96828553a086d7b18dcebfc579bd9628b016f86590d7453c115e490fa74b80"
|
||||
checksum = "87f253bc5c813ca05792837a0ff4b3a580336b224512d48f7eda1d7dd9210787"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"num-traits",
|
||||
"serde",
|
||||
]
|
||||
@ -1818,9 +1833,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.11"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
@ -3093,6 +3108,7 @@ dependencies = [
|
||||
"bincode",
|
||||
"bitflags 2.4.2",
|
||||
"build-parallel",
|
||||
"bytemuck",
|
||||
"byteorder",
|
||||
"derive_more",
|
||||
"etagere",
|
||||
@ -3147,6 +3163,7 @@ version = "0.62.0"
|
||||
dependencies = [
|
||||
"app_units",
|
||||
"bitflags 2.4.2",
|
||||
"bytemuck",
|
||||
"byteorder",
|
||||
"crossbeam-channel",
|
||||
"euclid",
|
||||
|
@ -29,8 +29,9 @@ webrender_build = { version = "0.0.2", path = "../webrender_build" }
|
||||
[dependencies]
|
||||
bincode = "1.0"
|
||||
bitflags = { version = "2", features = ["serde"] }
|
||||
bytemuck = { version = "1.4", features = ["derive"] }
|
||||
byteorder = "1.0"
|
||||
euclid = { version = "0.22.0", features = ["serde"] }
|
||||
euclid = { version = "0.22.7", features = ["bytemuck", "serde"] }
|
||||
fxhash = "0.2.1"
|
||||
gleam = "0.15.0"
|
||||
lazy_static = "1"
|
||||
|
@ -7,6 +7,7 @@ use api::{ImageDescriptor, ImageFormat, Parameter, BoolParameter, IntParameter,
|
||||
use api::{MixBlendMode, ImageBufferKind, VoidPtrToSizeFn};
|
||||
use api::{CrashAnnotator, CrashAnnotation, CrashAnnotatorGuard};
|
||||
use api::units::*;
|
||||
use bytemuck::Pod;
|
||||
use euclid::default::Transform3D;
|
||||
use gleam::gl;
|
||||
use crate::render_api::MemoryReport;
|
||||
@ -3507,7 +3508,7 @@ impl Device {
|
||||
);
|
||||
}
|
||||
|
||||
fn update_vbo_data<V>(
|
||||
fn update_vbo_data<V: Pod>(
|
||||
&mut self,
|
||||
vbo: VBOId,
|
||||
vertices: &[V],
|
||||
@ -3539,7 +3540,7 @@ impl Device {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn update_vao_main_vertices<V>(
|
||||
pub fn update_vao_main_vertices<V: Pod>(
|
||||
&mut self,
|
||||
vao: &VAO,
|
||||
vertices: &[V],
|
||||
@ -3549,7 +3550,7 @@ impl Device {
|
||||
self.update_vbo_data(vao.main_vbo_id, vertices, usage_hint)
|
||||
}
|
||||
|
||||
pub fn update_vao_instances<V: Clone>(
|
||||
pub fn update_vao_instances<V: Pod + Clone>(
|
||||
&mut self,
|
||||
vao: &VAO,
|
||||
instances: &[V],
|
||||
|
@ -28,6 +28,7 @@ use api::{DebugFlags, DocumentId, PremultipliedColorF};
|
||||
#[cfg(test)]
|
||||
use api::IdNamespace;
|
||||
use api::units::*;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use euclid::{HomogeneousVector, Box2D};
|
||||
use crate::internal_types::{FastHashMap, FastHashSet, FrameStamp, FrameId};
|
||||
use crate::profiler::{self, TransactionProfile};
|
||||
@ -159,7 +160,8 @@ impl GpuCacheHandle {
|
||||
// A unique address in the GPU cache. These are uploaded
|
||||
// as part of the primitive instances, to allow the vertex
|
||||
// shader to fetch the specific data.
|
||||
#[derive(Copy, Debug, Clone, MallocSizeOf, Eq, PartialEq)]
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Debug, Clone, MallocSizeOf, Eq, PartialEq, Pod, Zeroable)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
pub struct GpuCacheAddress {
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
use api::{AlphaType, PremultipliedColorF, YuvFormat, YuvRangedColorSpace};
|
||||
use api::units::*;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use crate::composite::CompositeFeatures;
|
||||
use crate::segment::EdgeAaSegmentMask;
|
||||
use crate::spatial_tree::{SpatialTree, SpatialNodeIndex};
|
||||
@ -58,7 +59,7 @@ impl ZBufferIdGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
@ -95,17 +96,26 @@ pub enum BlurDirection {
|
||||
Vertical,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
impl BlurDirection {
|
||||
pub fn as_int(self) -> i32 {
|
||||
match self {
|
||||
BlurDirection::Horizontal => 0,
|
||||
BlurDirection::Vertical => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
pub struct BlurInstance {
|
||||
pub task_address: RenderTaskAddress,
|
||||
pub src_task_address: RenderTaskAddress,
|
||||
pub blur_direction: BlurDirection,
|
||||
pub blur_direction: i32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
@ -114,7 +124,7 @@ pub struct ScalingInstance {
|
||||
pub source_rect: DeviceRect,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
@ -129,7 +139,7 @@ pub struct SvgFilterInstance {
|
||||
pub extra_data_address: GpuCacheAddress,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
@ -159,7 +169,7 @@ pub enum BorderSegment {
|
||||
Bottom,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
@ -174,7 +184,7 @@ pub struct BorderInstance {
|
||||
pub clip_params: [f32; 8],
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
@ -187,7 +197,7 @@ pub struct ClipMaskInstanceCommon {
|
||||
pub prim_transform_id: TransformPaletteId,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
@ -197,7 +207,7 @@ pub struct ClipMaskInstanceRect {
|
||||
pub clip_data: ClipData,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
@ -209,7 +219,7 @@ pub struct BoxShadowData {
|
||||
pub dest_rect: LayoutRect,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
@ -221,7 +231,7 @@ pub struct ClipMaskInstanceBoxShadow {
|
||||
|
||||
// 16 bytes per instance should be enough for anyone!
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
pub struct PrimitiveInstanceData {
|
||||
@ -234,7 +244,7 @@ const UV_TYPE_NORMALIZED: u32 = 0;
|
||||
const UV_TYPE_UNNORMALIZED: u32 = 1;
|
||||
|
||||
/// A GPU-friendly representation of the `ScaleOffset` type
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct CompositorTransform {
|
||||
pub sx: f32,
|
||||
@ -268,7 +278,7 @@ impl From<ScaleOffset> for CompositorTransform {
|
||||
/// Vertex format for picture cache composite shader.
|
||||
/// When editing the members, update desc::COMPOSITE
|
||||
/// so its list of instance_attributes matches:
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct CompositeInstance {
|
||||
// Picture space destination rectangle of surface
|
||||
@ -377,7 +387,7 @@ impl CompositeInstance {
|
||||
}
|
||||
|
||||
/// Vertex format for issuing colored quads.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct ClearInstance {
|
||||
pub rect: [f32; 4],
|
||||
@ -582,15 +592,24 @@ pub enum ClipSpace {
|
||||
Primitive = 1,
|
||||
}
|
||||
|
||||
impl ClipSpace {
|
||||
pub fn as_int(self) -> u32 {
|
||||
match self {
|
||||
ClipSpace::Raster => 0,
|
||||
ClipSpace::Primitive => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
pub struct MaskInstance {
|
||||
pub prim: PrimitiveInstanceData,
|
||||
pub clip_transform_id: TransformPaletteId,
|
||||
pub clip_address: i32,
|
||||
pub clip_space: ClipSpace,
|
||||
pub clip_space: u32,
|
||||
pub unused: i32,
|
||||
}
|
||||
|
||||
@ -698,7 +717,7 @@ impl ImageBrushData {
|
||||
// only flag currently used determines whether the
|
||||
// transform is axis-aligned (and this should have
|
||||
// pixel snapping applied).
|
||||
#[derive(Copy, Debug, Clone, PartialEq)]
|
||||
#[derive(Copy, Debug, Clone, PartialEq, Pod, Zeroable)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
|
@ -11,6 +11,7 @@
|
||||
use euclid::vec2;
|
||||
use api::{ExtendMode, GradientStop, PremultipliedColorF};
|
||||
use api::units::*;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use crate::pattern::{Pattern, PatternKind, PatternShaderInput};
|
||||
use crate::scene_building::IsVisible;
|
||||
use crate::frame_builder::FrameBuildingState;
|
||||
@ -370,7 +371,7 @@ impl ConicGradientTask {
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct ConicGradientInstance {
|
||||
pub task_rect: DeviceRect,
|
||||
pub center: DevicePoint,
|
||||
@ -436,4 +437,4 @@ pub fn conic_gradient_pattern(
|
||||
base_color: PremultipliedColorF::WHITE,
|
||||
is_opaque,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ use euclid::approxeq::ApproxEq;
|
||||
use euclid::{point2, vec2, size2};
|
||||
use api::{ExtendMode, GradientStop, LineOrientation, PremultipliedColorF, ColorF, ColorU};
|
||||
use api::units::*;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use crate::scene_building::IsVisible;
|
||||
use crate::frame_builder::FrameBuildingState;
|
||||
use crate::intern::{Internable, InternDebug, Handle as InternHandle};
|
||||
@ -685,7 +686,7 @@ pub type FastLinearGradientCacheKey = FastLinearGradientTask;
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct FastLinearGradientInstance {
|
||||
pub task_rect: DeviceRect,
|
||||
pub color0: PremultipliedColorF,
|
||||
@ -723,7 +724,7 @@ impl LinearGradientTask {
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct LinearGradientInstance {
|
||||
pub task_rect: DeviceRect,
|
||||
pub start: DevicePoint,
|
||||
|
@ -11,6 +11,7 @@
|
||||
use euclid::{vec2, size2};
|
||||
use api::{ExtendMode, GradientStop, PremultipliedColorF, ColorU};
|
||||
use api::units::*;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use crate::pattern::{Pattern, PatternKind, PatternShaderInput};
|
||||
use crate::scene_building::IsVisible;
|
||||
use crate::frame_builder::FrameBuildingState;
|
||||
@ -336,7 +337,7 @@ impl RadialGradientTask {
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct RadialGradientInstance {
|
||||
pub task_rect: DeviceRect,
|
||||
pub center: DevicePoint,
|
||||
@ -569,4 +570,4 @@ pub fn radial_gradient_pattern(
|
||||
base_color: PremultipliedColorF::WHITE,
|
||||
is_opaque,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ use api::{ImageRendering, RepeatMode, PrimitiveFlags};
|
||||
use api::{PremultipliedColorF, PropertyBinding, Shadow};
|
||||
use api::{PrimitiveKeyKind, FillRule, POLYGON_CLIP_VERTEX_MAX};
|
||||
use api::units::*;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use euclid::{SideOffsets2D, Size2D};
|
||||
use malloc_size_of::MallocSizeOf;
|
||||
use crate::composite::CompositorSurfaceKind;
|
||||
@ -729,7 +730,7 @@ impl BrushSegment {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
@ -738,7 +739,7 @@ struct ClipRect {
|
||||
mode: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
@ -762,7 +763,7 @@ impl ClipCorner {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
use api::{units::*, PremultipliedColorF, ClipMode};
|
||||
use api::{ColorF, ImageFormat, LineOrientation, BorderStyle};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use crate::batch::{AlphaBatchBuilder, AlphaBatchContainer, BatchTextures};
|
||||
use crate::batch::{ClipBatcher, BatchBuilder, INVALID_SEGMENT_INDEX, ClipMaskInstanceList};
|
||||
use crate::command_buffer::{CommandBufferList, QuadFlags};
|
||||
@ -830,7 +831,7 @@ fn add_blur_instances(
|
||||
let instance = BlurInstance {
|
||||
task_address,
|
||||
src_task_address: src_task_id.into(),
|
||||
blur_direction,
|
||||
blur_direction: blur_direction.as_int(),
|
||||
};
|
||||
|
||||
instances
|
||||
@ -1134,7 +1135,8 @@ pub struct BlitJob {
|
||||
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
#[derive(Clone, Debug)]
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct LineDecorationJob {
|
||||
pub task_rect: DeviceRect,
|
||||
pub local_size: LayoutSize,
|
||||
@ -1363,7 +1365,7 @@ fn build_mask_tasks(
|
||||
prim,
|
||||
clip_transform_id,
|
||||
clip_address: clip_address.as_int(),
|
||||
clip_space,
|
||||
clip_space: clip_space.as_int(),
|
||||
unused: 0,
|
||||
};
|
||||
|
||||
|
@ -6,6 +6,7 @@ use api::{CompositeOperator, FilterPrimitive, FilterPrimitiveInput, FilterPrimit
|
||||
use api::{LineStyle, LineOrientation, ClipMode, MixBlendMode, ColorF, ColorSpace, FilterOpGraphPictureBufferId};
|
||||
use api::MAX_RENDER_TASK_SIZE;
|
||||
use api::units::*;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use crate::box_shadow::BLUR_SAMPLE_SCALE;
|
||||
use crate::clip::{ClipDataStore, ClipItemKind, ClipStore, ClipNodeRange};
|
||||
use crate::command_buffer::{CommandBufferIndex, QuadFlags};
|
||||
@ -45,7 +46,7 @@ fn render_task_sanity_check(size: &DeviceIntSize) {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
#[cfg_attr(feature = "capture", derive(Serialize))]
|
||||
#[cfg_attr(feature = "replay", derive(Deserialize))]
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
use api::{ColorU, ImageFormat, ImageBufferKind};
|
||||
use api::units::*;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use crate::debug_font_data;
|
||||
use crate::device::{Device, Program, Texture, TextureSlot, VertexDescriptor, ShaderError, VAO};
|
||||
use crate::device::{TextureFilter, VertexAttribute, VertexAttributeKind, VertexUsageHint};
|
||||
@ -62,6 +63,7 @@ const DESC_COLOR: VertexDescriptor = VertexDescriptor {
|
||||
};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||
pub struct DebugFontVertex {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
@ -77,6 +79,7 @@ impl DebugFontVertex {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||
pub struct DebugColorVertex {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
|
@ -46,6 +46,7 @@ use api::FramePublishId;
|
||||
use api::units::*;
|
||||
use api::channel::{Sender, Receiver};
|
||||
pub use api::DebugFlags;
|
||||
use bytemuck::Pod;
|
||||
use core::time::Duration;
|
||||
|
||||
use crate::pattern::PatternKind;
|
||||
@ -1999,7 +2000,7 @@ impl Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_instanced_batch<T: Clone>(
|
||||
fn draw_instanced_batch<T: Pod + Clone>(
|
||||
&mut self,
|
||||
data: &[T],
|
||||
vertex_array_kind: VertexArrayKind,
|
||||
|
@ -16,8 +16,9 @@ display_list_stats = []
|
||||
[dependencies]
|
||||
app_units = "0.7.3"
|
||||
bitflags = { version = "2", features = ["serde"] }
|
||||
bytemuck = { version = "1.4", features = ["derive"] }
|
||||
byteorder = "1.2.1"
|
||||
euclid = { version = "0.22.6", features = ["serde"] }
|
||||
euclid = { version = "0.22.7", features = ["bytemuck", "serde"] }
|
||||
malloc_size_of_derive = "0.1"
|
||||
serde = { version = "1.0", features = ["rc"] }
|
||||
serde_derive = "1.0"
|
||||
|
@ -2,6 +2,7 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use peek_poke::PeekPoke;
|
||||
use std::cmp;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@ -14,7 +15,7 @@ use std::hash::{Hash, Hasher};
|
||||
/// In premultiplied colors transitions to transparent always look "nice"
|
||||
/// therefore they are used in CSS gradients.
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
|
||||
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize, Pod, Zeroable)]
|
||||
pub struct PremultipliedColorF {
|
||||
pub r: f32,
|
||||
pub g: f32,
|
||||
@ -112,7 +113,7 @@ impl Hash for PremultipliedColorF {
|
||||
/// If the alpha value `a` is 255 the color is opaque.
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Hash, Eq, Debug, Deserialize, MallocSizeOf, PartialEq)]
|
||||
#[derive(PartialOrd, Ord, Serialize, PeekPoke, Default)]
|
||||
#[derive(PartialOrd, Ord, Serialize, PeekPoke, Default, Pod, Zeroable)]
|
||||
pub struct ColorU {
|
||||
pub r: u8,
|
||||
pub g: u8,
|
||||
|
@ -13,6 +13,7 @@
|
||||
//! in the context of coordinate systems.
|
||||
|
||||
pub use app_units::Au;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use euclid::{Length, Rect, Scale, Size2D, Transform3D, Translation2D};
|
||||
use euclid::{Point2D, Point3D, Vector2D, Vector3D, SideOffsets2D, Box2D};
|
||||
use euclid::HomogeneousVector;
|
||||
@ -155,7 +156,8 @@ pub type BlobToDeviceTranslation = Translation2D<i32, LayoutPixel, DevicePixel>;
|
||||
/// may grow. Storing them as texel coords and normalizing
|
||||
/// the UVs in the vertex shader means nothing needs to be
|
||||
/// updated on the CPU when the texture size changes.
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct TexelRect {
|
||||
pub uv0: DevicePoint,
|
||||
pub uv1: DevicePoint,
|
||||
|
@ -1182,6 +1182,30 @@ criteria = "safe-to-deploy"
|
||||
delta = "2.4.2 -> 2.5.0"
|
||||
aggregated-from = "https://chromium.googlesource.com/chromium/src/+/main/third_party/rust/chromium_crates_io/supply-chain/audits.toml?format=TEXT"
|
||||
|
||||
[[audits.google.audits.bytemuck]]
|
||||
who = "Lukasz Anforowicz <lukasza@chromium.org>"
|
||||
criteria = "safe-to-deploy"
|
||||
version = "1.14.3"
|
||||
notes = "Additional review notes may be found in https://crrev.com/c/5362675."
|
||||
aggregated-from = "https://chromium.googlesource.com/chromium/src/+/main/third_party/rust/chromium_crates_io/supply-chain/audits.toml?format=TEXT"
|
||||
|
||||
[[audits.google.audits.bytemuck]]
|
||||
who = "Adrian Taylor <adetaylor@chromium.org>"
|
||||
criteria = "safe-to-deploy"
|
||||
delta = "1.14.3 -> 1.15.0"
|
||||
aggregated-from = "https://chromium.googlesource.com/chromium/src/+/main/third_party/rust/chromium_crates_io/supply-chain/audits.toml?format=TEXT"
|
||||
|
||||
[[audits.google.audits.bytemuck_derive]]
|
||||
who = "Lukasz Anforowicz <lukasza@chromium.org>"
|
||||
criteria = "safe-to-deploy"
|
||||
version = "1.6.0"
|
||||
notes = """
|
||||
Grepped for \"unsafe\", \"crypt\", \"cipher\", \"fs\", \"net\" - there were no
|
||||
hits except for 8 occurrences of `unsafe`. Additional `unsafe` review comments
|
||||
can be found in https://crrev.com/c/5445719.
|
||||
"""
|
||||
aggregated-from = "https://chromium.googlesource.com/chromium/src/+/main/third_party/rust/chromium_crates_io/supply-chain/audits.toml?format=TEXT"
|
||||
|
||||
[[audits.google.audits.equivalent]]
|
||||
who = "George Burgess IV <gbiv@google.com>"
|
||||
criteria = "safe-to-deploy"
|
||||
|
1
third_party/rust/bytemuck/.cargo-checksum.json
vendored
Normal file
1
third_party/rust/bytemuck/.cargo-checksum.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"files":{"Cargo.toml":"695a6f6acca3567c6bfd2c9130c5f0ae9703a10fbb917a5545d8f365627adef5","LICENSE-APACHE":"870e20c217d15bcfcbe53d7c5867cd8fac44a4ca0b41fc1eb843557e16063eba","LICENSE-MIT":"0b2d108c9c686a74ac312990ee8377902756a2a081a7af3b0f9d68abf0a8f1a1","LICENSE-ZLIB":"682b4c81b85e83ce6cc6e1ace38fdd97aeb4de0e972bd2b44aa0916c54af8c96","README.md":"167493de1f1ad16d13c778494ae344cd71306622c89d19002eaf7f4185c1f728","changelog.md":"ee1cec3147cb82f540841653edba28d90d726af0413e42979b95f00a22af2c05","rustfmt.toml":"f4c215534437936f924c937dbb1677f614761589300d6b389f3b518b3eb551b8","src/allocation.rs":"996f500fd89e19c8f44bc7b7c6d097efaf8ea0b659c4e6f6a506b49fd47fba1b","src/anybitpattern.rs":"0053be9c471e76d32acf237cb94dce49074a4711d4b0a199cf257e5de8b93f77","src/checked.rs":"311c268a8afd7006ad7bd6331f5e661dead186c3b7ab490859cee157b18d7ba3","src/contiguous.rs":"867e162651b435aa0298caad1d81f46877c22c74a2766d9e79be0ab3c615ce46","src/internal.rs":"ec4ed032d82bdb8e4039a648e7282dec14606d4175c7eea3f66a60e543c1c8ff","src/lib.rs":"e3982cc16eb38a10bd8c0179e48ee61be3c54d268255d34adf5ca91df9f0599c","src/must.rs":"20a4077f8fbdb0d2660bc754a874d05d007167a687d8ea9baf8411c4a751b73d","src/no_uninit.rs":"4ab2f5ed29bff0b33630661154eb548f3e55581bfcf576a90397b7f8d5323201","src/offset_of.rs":"2afd190ef0462b30ade786fe813a91e7bf41cc2fa99a1d79002cbafab5964f37","src/pod.rs":"0dd26433c0ad9c9a4882f175d5f056d54b5fcec905eb0df907c9ba4d8c828597","src/pod_in_option.rs":"73bbe1d69f32d909695ce26d131aa2d81eaa31e2b4532256ebfe1a6ba68675c1","src/transparent.rs":"0704a14de6af47c39c79b45ee3e63b28ba6500534cf7629f578f15b1ebf46f6d","src/zeroable.rs":"3897421dcfd66808a23c3c15114efc9be901c002355d365e889997cba8f80703","src/zeroable_in_option.rs":"f74799ac3eee50116ec63a0ae4d3e351e0ab7ac807d01b4b59027bf6a68d6de6","tests/array_tests.rs":"98ca7a0dcd93e65f70d4db19643e707cafae5a249561ab151998cedb89b2e036","tests/cast_slice_tests.rs":"83310a834e75214f711466b119729ed2b3f53b5c9714bc7a2fe3fd9e7a48f993","tests/checked_tests.rs":"27965acf20e46482b09ee56aaa2536868821be651a3b95052f40e554ecde9917","tests/derive.rs":"93b5ab70ecdd726811af9dee1702e23e964b8ceac59f727889f6a2678ad90d65","tests/doc_tests.rs":"f20708319fde62d8957909d51ee976fce394ad0891ebc4bbcf336ab026a34092","tests/offset_of_tests.rs":"fb5f91e17f984050969f8b06f1de58b5c1e80802c5deb992d3188f5ec274690f","tests/std_tests.rs":"967d4fb4cae24a374633c9b68f1ff65f86ba4c8a0e980adfe69dcaf60a9049c2","tests/transparent.rs":"ecef6e0987e28121b480942e58ce4534f13fe35667bde7f5c6e04e590b02f6a3","tests/wrapper_forgets.rs":"c6330546f6aa696245625056e7323b3916e3fb1a9fbecefe9c9e62d3726812d9"},"package":"5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15"}
|
75
third_party/rust/bytemuck/Cargo.toml
vendored
Normal file
75
third_party/rust/bytemuck/Cargo.toml
vendored
Normal file
@ -0,0 +1,75 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2018"
|
||||
name = "bytemuck"
|
||||
version = "1.15.0"
|
||||
authors = ["Lokathor <zefria@gmail.com>"]
|
||||
exclude = ["/pedantic.bat"]
|
||||
description = "A crate for mucking around with piles of bytes."
|
||||
readme = "README.md"
|
||||
keywords = [
|
||||
"transmute",
|
||||
"bytes",
|
||||
"casting",
|
||||
]
|
||||
categories = [
|
||||
"encoding",
|
||||
"no-std",
|
||||
]
|
||||
license = "Zlib OR Apache-2.0 OR MIT"
|
||||
repository = "https://github.com/Lokathor/bytemuck"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = [
|
||||
"nightly_docs",
|
||||
"derive",
|
||||
"extern_crate_alloc",
|
||||
"extern_crate_std",
|
||||
"zeroable_maybe_uninit",
|
||||
"zeroable_atomics",
|
||||
"min_const_generics",
|
||||
"wasm_simd",
|
||||
"must_cast",
|
||||
]
|
||||
|
||||
[package.metadata.playground]
|
||||
features = [
|
||||
"derive",
|
||||
"extern_crate_alloc",
|
||||
"extern_crate_std",
|
||||
"zeroable_maybe_uninit",
|
||||
"zeroable_atomics",
|
||||
"min_const_generics",
|
||||
"wasm_simd",
|
||||
"must_cast",
|
||||
]
|
||||
|
||||
[dependencies.bytemuck_derive]
|
||||
version = "1.4"
|
||||
optional = true
|
||||
|
||||
[features]
|
||||
aarch64_simd = []
|
||||
align_offset = []
|
||||
derive = ["bytemuck_derive"]
|
||||
extern_crate_alloc = []
|
||||
extern_crate_std = ["extern_crate_alloc"]
|
||||
min_const_generics = []
|
||||
must_cast = []
|
||||
nightly_docs = []
|
||||
nightly_portable_simd = []
|
||||
nightly_stdsimd = []
|
||||
unsound_ptr_pod_impl = []
|
||||
wasm_simd = []
|
||||
zeroable_atomics = []
|
||||
zeroable_maybe_uninit = []
|
61
third_party/rust/bytemuck/LICENSE-APACHE
vendored
Normal file
61
third_party/rust/bytemuck/LICENSE-APACHE
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
9
third_party/rust/bytemuck/LICENSE-MIT
vendored
Normal file
9
third_party/rust/bytemuck/LICENSE-MIT
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Daniel "Lokathor" Gee.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
11
third_party/rust/bytemuck/LICENSE-ZLIB
vendored
Normal file
11
third_party/rust/bytemuck/LICENSE-ZLIB
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
Copyright (c) 2019 Daniel "Lokathor" Gee.
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source distribution.
|
60
third_party/rust/bytemuck/README.md
vendored
Normal file
60
third_party/rust/bytemuck/README.md
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
* **[Latest Docs.rs Here](https://docs.rs/bytemuck/)**
|
||||
|
||||
[![License:Zlib](https://img.shields.io/badge/License-Zlib-brightgreen.svg)](https://opensource.org/licenses/Zlib)
|
||||
![Minimum Rust Version](https://img.shields.io/badge/Min%20Rust-1.34-green.svg)
|
||||
[![crates.io](https://img.shields.io/crates/v/bytemuck.svg)](https://crates.io/crates/bytemuck)
|
||||
|
||||
# bytemuck
|
||||
|
||||
A crate for mucking around with piles of bytes.
|
||||
|
||||
This crate lets you safely perform "bit cast" operations between data types.
|
||||
That's where you take a value and just reinterpret the bits as being some other
|
||||
type of value, without changing the bits.
|
||||
|
||||
* This is **not** like the [`as` keyword][keyword-as]
|
||||
* This is **not** like the [`From` trait][from-trait]
|
||||
* It is **most like** [`f32::to_bits`][f32-to_bits], just generalized to let you
|
||||
convert between all sorts of data types.
|
||||
|
||||
[keyword-as]: https://doc.rust-lang.org/nightly/std/keyword.as.html
|
||||
[from-trait]: https://doc.rust-lang.org/nightly/core/convert/trait.From.html
|
||||
[f32-to_bits]: https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.to_bits
|
||||
|
||||
### Here's the part you're more likely to care about: *you can do this with slices too!*
|
||||
|
||||
When a slice is involved it's not a *direct* bitcast. Instead, the `cast_slice`
|
||||
and `cast_slice_mut` functions will pull apart a slice's data and give you a new
|
||||
slice that's the same span of memory just viewed as the new type. If the size of
|
||||
the slice's element changes then the length of the slice you get back will be
|
||||
changed accordingly.
|
||||
|
||||
This lets you cast a slice of color values into a slice of `u8` and send it to
|
||||
the GPU, or things like that. I'm sure there's other examples, but honestly this
|
||||
crate is as popular as it is mostly because of Rust's 3D graphics community
|
||||
wanting to cast slices of different types into byte slices for sending to the
|
||||
GPU. Hi friends! Push those vertices, or whatever it is that you all do.
|
||||
|
||||
## See Also
|
||||
|
||||
While `bytemuck` is full of unsafe code, I've also started a "sibling crate"
|
||||
called [bitfrob](https://docs.rs/bitfrob/latest/bitfrob/), which is where
|
||||
operations that are 100% safe will be added.
|
||||
|
||||
## Stability
|
||||
|
||||
* The crate is 1.0 and I consider this it to be "basically done". New features
|
||||
are usually being accepted when other people want to put in the work, but
|
||||
myself I wanna move on to using `bytemuck` in bigger projects.
|
||||
* The default build of the `bytemuck` crate will continue to work with `rustc-1.34`
|
||||
for at least the rest of the `1.y.z` versions.
|
||||
* Any other cargo features of the crate **are not** held to the same standard, and
|
||||
may work only on the latest Stable or even only on latest Nightly.
|
||||
|
||||
**Future Plans:** Once the [Safe Transmute Project][pg-st] completes and
|
||||
stabilizes ("eventually") this crate will be updated to use that as the
|
||||
underlying mechanism for transmutation bounds, and a 2.0 version of `bytemuck`
|
||||
will be released. The hope is for the 1.0 to 2.0 transition to be as seamless as
|
||||
possible, but the future is always uncertain.
|
||||
|
||||
[pg-st]: https://rust-lang.github.io/rfcs/2835-project-safe-transmute.html
|
298
third_party/rust/bytemuck/changelog.md
vendored
Normal file
298
third_party/rust/bytemuck/changelog.md
vendored
Normal file
@ -0,0 +1,298 @@
|
||||
# `bytemuck` changelog
|
||||
|
||||
## 1.15.0
|
||||
|
||||
This primarily relaxes the bounds on a `From` impl.
|
||||
|
||||
Previously:
|
||||
|
||||
> `impl<T: NoUninit> From<Box<T>> for BoxBytes`
|
||||
|
||||
Now:
|
||||
|
||||
> `impl<T: ?Sized + sealed::BoxBytesOf> From<Box<T>> for BoxBytes`
|
||||
|
||||
All related functions and methods are similarly updated.
|
||||
|
||||
We believe this to be backwards compatible with all previous uses,
|
||||
and now `BoxBytes` can be converted to/from more types than before.
|
||||
|
||||
## 1.14.3
|
||||
|
||||
* The new std simd nightly features are apparently arch-specific.
|
||||
This adjusts the feature activation to be x86/ x86_64 only.
|
||||
|
||||
## 1.14.2
|
||||
|
||||
* Changes the name of the Nightly feature activated by the crate's
|
||||
`nightly_stdsimd` feature. This is needed as of (approximately) Nightly
|
||||
2024-02-06 and later, because the Nightly feature was changed.
|
||||
|
||||
## 1.14.1
|
||||
|
||||
* docs clarifications.
|
||||
|
||||
## 1.14
|
||||
|
||||
* `write_zeroes` and `fill_zeroes` functions: Writes (to one) or fills (a slice)
|
||||
zero bytes to all bytes covered by the provided reference. If your type has
|
||||
padding, this will even zero out the padding bytes.
|
||||
* `align_offset` feature: causes pointer alignment checks to use the
|
||||
`align_offset` pointer method rather than as-casting the pointer to `usize`.
|
||||
This *may* improve codegen, if the compiler would have otherwise thought that
|
||||
the pointer address escaped. No formal benchmarks have been done either way.
|
||||
* `must_cast` feature: Adds `must_*` family of functions. These functions will
|
||||
fail to compile if the cast requested can't be statically known to succeed.
|
||||
The error messages can be kinda bad when this happens, but eliminating the
|
||||
possibility of a runtime error might be worth it to you.
|
||||
|
||||
## 1.13.1
|
||||
|
||||
* Remove the requirement for the *source* data type to be `AnyBitPattern` on
|
||||
`pod_collect_to_vec`, allowing you to pod collect vecs of `char` into vecs of
|
||||
`u32`, or whatever.
|
||||
|
||||
## 1.13
|
||||
|
||||
* Now depends on `bytemuck_derive-1.4.0`
|
||||
* Various small enhancements that would have been patch version updates, but
|
||||
which have been rolled into this minor version update.
|
||||
|
||||
## 1.12.4
|
||||
|
||||
* This has additional impls for existing traits and cleans up some internal code,
|
||||
but there's no new functions so I guess it counts as just a patch release.
|
||||
|
||||
## 1.12.3
|
||||
|
||||
* This bugfix makes the crate do stuff with `Arc` or not based on the
|
||||
`target_has_atomic` config. Previously, some targets that have allocation but
|
||||
not atomics were getting errors. This raises the MSRV of the
|
||||
`extern_crate_alloc` feature to 1.60, but opt-in features are *not* considered
|
||||
to be hard locked to 1.34 like the basic build of the crate is.
|
||||
|
||||
## 1.12.2
|
||||
|
||||
* Fixes `try_pod_read_unaligned` bug that made it always fail unless the target
|
||||
type was exactly pointer sized in which case UB *could* happen. The
|
||||
`CheckedBitPattern::is_valid_bit_pattern` was being asked to check that a
|
||||
*reference* to the `pod` value was a valid bit pattern, rather than the actual
|
||||
bit pattern itself, and so the check could in some cases be illegally
|
||||
bypassed.
|
||||
|
||||
## 1.12.1
|
||||
|
||||
* Patch bumped the required `bytemuck_derive` version because of a regression in
|
||||
how it handled `align(N)` attributes.
|
||||
|
||||
## 1.12
|
||||
|
||||
* This minor version bump is caused by a version bump in our `bytemuck_derive`
|
||||
dependency, which is in turn caused by a mixup in the minimum version of `syn`
|
||||
that `bytemuck_derive` uses. See [Issue
|
||||
122](https://github.com/Lokathor/bytemuck/issues/122). There's not any
|
||||
specific "new" API as you might normally expect from a minor version bump.
|
||||
* [pali](https://github.com/pali6) fixed a problem with SPIR-V builds being
|
||||
broken. The error handling functions were trying to be generic over `Display`,
|
||||
which the error types normally support, except on SPIR-V targets (which run on
|
||||
the GPU and don't have text formatting).
|
||||
|
||||
## 1.11
|
||||
|
||||
* [WaffleLapkin](https://github.com/WaffleLapkin) added `wrap_box` and `peel_box`
|
||||
to the `TransparentWrapperAlloc` trait. Default impls of these functions are
|
||||
provided, and (as usual with the transparent trait stuff) you should not override
|
||||
the default versions.
|
||||
|
||||
## 1.10
|
||||
|
||||
* [TheEdward162](https://github.com/TheEdward162) added the `ZeroableInOption`
|
||||
and `PodInOption` traits. These are for types that are `Zeroable` or `Pod`
|
||||
*when in an option*, but not on their own. We provide impls for the various
|
||||
"NonZeroINTEGER" types in `core`, and if you need to newtype a NonZero value
|
||||
then you can impl these traits when you use `repr(transparent)`.
|
||||
|
||||
## 1.9.1
|
||||
|
||||
* Bumped the minimum `bytemuck_derive` dependency version from `1.0` to `1.1`.
|
||||
The fact that `bytemuck` and `bytemuck_derive` are separate crates at all is
|
||||
an unfortunate technical limit of current Rust, woe and calamity.
|
||||
|
||||
## 1.9.0
|
||||
|
||||
* [fu5ha](https://github.com/fu5ha) added the `NoUninit`, `AnyBitPattern`, and
|
||||
`CheckedBitPattern` traits. This allows for a more fine-grained level of
|
||||
detail in what casting operations are allowed for a type. Types that already
|
||||
implement `Zeroable` and `Pod` will have a blanket impl for these new traits.
|
||||
This is a "preview" of the direction that the crate will probably go in the
|
||||
eventual 2.0 version. We're still waiting on [Project Safe
|
||||
Transmute](https://github.com/rust-lang/project-safe-transmute) for an actual
|
||||
2.0 version of the crate, but until then please enjoy this preview.
|
||||
* Also Fusha added better support for `union` types in the derive macros. I
|
||||
still don't know how any of the proc-macro stuff works at all, so please
|
||||
direct questions to her.
|
||||
|
||||
## 1.8.0
|
||||
|
||||
* `try_pod_read_unaligned` and `pod_read_unaligned` let you go from `&[u8]` to
|
||||
`T:Pod` without worrying about alignment.
|
||||
|
||||
## 1.7.3
|
||||
|
||||
* Experimental support for the `portable_simd` language extension under the
|
||||
`nightly_portable_simd` cargo feature. As the name implies, this is an
|
||||
experimental crate feature and it's **not** part of the semver contract. All
|
||||
it does is add the appropriate `Zeroable` and `Pod` impls.
|
||||
|
||||
## 1.7.2
|
||||
|
||||
* Why does this repo keep being hit with publishing problems? What did I do to
|
||||
deserve this curse, Ferris? This doesn't ever happen with tinyvec or fermium,
|
||||
only bytemuck.
|
||||
|
||||
## 1.7.1
|
||||
|
||||
* **Soundness Fix:** The wrap/peel methods for owned value conversion, added to
|
||||
`TransparentWrapper` in 1.6, can cause a double-drop if used with types that
|
||||
impl `Drop`. The fix was simply to add a `ManuallyDrop` layer around the value
|
||||
before doing the `transmute_copy` that is used to wrap/peel. While this fix
|
||||
could technically be backported to the 1.6 series, since 1.7 is semver
|
||||
compatible anyway the 1.6 series has simply been yanked.
|
||||
|
||||
## 1.7
|
||||
|
||||
* In response to [Unsafe Code Guidelines Issue
|
||||
#286](https://github.com/rust-lang/unsafe-code-guidelines/issues/286), this
|
||||
version of Bytemuck has a ***Soundness-Required Breaking Change***. This is
|
||||
"allowed" under Rust's backwards-compatibility guidelines, but it's still
|
||||
annoying of course so we're trying to keep the damage minimal.
|
||||
* **The Reason:** It turns out that pointer values should not have been `Pod`. More
|
||||
specifically, `ptr as usize` is *not* the same operation as calling
|
||||
`transmute::<_, usize>(ptr)`.
|
||||
* LLVM has yet to fully sort out their story, but until they do, transmuting
|
||||
pointers can cause miscompilations. They may fix things up in the future,
|
||||
but we're not gonna just wait and have broken code in the mean time.
|
||||
* **The Fix:** The breaking change is that the `Pod` impls for `*const T`,
|
||||
`*mut T`, and `Option<NonNull<T>` are now gated behind the
|
||||
`unsound_ptr_pod_impl` feature, which is off by default.
|
||||
* You are *strongly discouraged* from using this feature, but if a dependency
|
||||
of yours doesn't work when you upgrade to 1.7 because it relied on pointer
|
||||
casting, then you might wish to temporarily enable the feature just to get
|
||||
that dependency to build. Enabled features are global across all users of a
|
||||
given semver compatible version, so if you enable the feature in your own
|
||||
crate, your dependency will also end up getting the feature too, and then
|
||||
it'll be able to compile.
|
||||
* Please move away from using this feature as soon as you can. Consider it to
|
||||
*already* be deprecated.
|
||||
* [PR 65](https://github.com/Lokathor/bytemuck/pull/65)
|
||||
|
||||
## 1.6.3
|
||||
|
||||
* Small goof with an errant `;`, so [PR 69](https://github.com/Lokathor/bytemuck/pull/69)
|
||||
*actually* got things working on SPIR-V.
|
||||
|
||||
## 1.6.2
|
||||
|
||||
cargo upload goof! ignore this one.
|
||||
|
||||
## 1.6.1
|
||||
|
||||
* [DJMcNab](https://github.com/DJMcNab) did a fix so that the crate can build for SPIR-V
|
||||
[PR 67](https://github.com/Lokathor/bytemuck/pull/67)
|
||||
|
||||
## 1.6
|
||||
|
||||
* The `TransparentWrapper` trait now has more methods. More ways to wrap, and
|
||||
now you can "peel" too! Note that we don't call it "unwrap" because that name
|
||||
is too strongly associated with the Option/Result methods.
|
||||
Thanks to [LU15W1R7H](https://github.com/LU15W1R7H) for doing
|
||||
[PR 58](https://github.com/Lokathor/bytemuck/pull/58)
|
||||
* Min Const Generics! Now there's Pod and Zeroable for arrays of any size when
|
||||
you turn on the `min_const_generics` crate feature.
|
||||
[zakarumych](https://github.com/zakarumych) got the work started in
|
||||
[PR 59](https://github.com/Lokathor/bytemuck/pull/59),
|
||||
and [chorman0773](https://github.com/chorman0773) finished off the task in
|
||||
[PR 63](https://github.com/Lokathor/bytemuck/pull/63)
|
||||
|
||||
## 1.5.1
|
||||
|
||||
* Fix `bytes_of` failing on zero sized types.
|
||||
[PR 53](https://github.com/Lokathor/bytemuck/pull/53)
|
||||
|
||||
## 1.5
|
||||
|
||||
* Added `pod_collect_to_vec`, which will gather a slice into a vec,
|
||||
allowing you to change the pod type while also safely ignoring alignment.
|
||||
[PR 50](https://github.com/Lokathor/bytemuck/pull/50)
|
||||
|
||||
## 1.4.2
|
||||
|
||||
* [Kimundi](https://github.com/Kimundi) fixed an issue that could make `try_zeroed_box`
|
||||
stack overflow for large values at low optimization levels.
|
||||
[PR 43](https://github.com/Lokathor/bytemuck/pull/43)
|
||||
|
||||
## 1.4.1
|
||||
|
||||
* [thomcc](https://github.com/thomcc) fixed up the CI and patched over a soundness hole in `offset_of!`.
|
||||
[PR 38](https://github.com/Lokathor/bytemuck/pull/38)
|
||||
|
||||
## 1.4
|
||||
|
||||
* [icewind1991](https://github.com/icewind1991) has contributed the proc-macros
|
||||
for deriving impls of `Pod`, `TransparentWrapper`, `Zeroable`!! Everyone has
|
||||
been waiting for this one folks! It's a big deal. Just enable the `derive`
|
||||
cargo feature and then you'll be able to derive the traits on your types. It
|
||||
generates all the appropriate tests for you.
|
||||
* The `zeroable_maybe_uninit` feature now adds a `Zeroable` impl to the
|
||||
`MaybeUninit` type. This is only behind a feature flag because `MaybeUninit`
|
||||
didn't exist back in `1.34.0` (the minimum rust version of `bytemuck`).
|
||||
|
||||
## 1.3.1
|
||||
|
||||
* The entire crate is now available under the `Apache-2.0 OR MIT` license as
|
||||
well as the previous `Zlib` license
|
||||
[#24](https://github.com/Lokathor/bytemuck/pull/24).
|
||||
* [HeroicKatora](https://github.com/HeroicKatora) added the
|
||||
`try_zeroed_slice_box` function
|
||||
[#10](https://github.com/Lokathor/bytemuck/pull/17). `zeroed_slice_box` is
|
||||
also available.
|
||||
* The `offset_of!` macro now supports a 2-arg version. For types that impl
|
||||
Default, it'll just make an instance using `default` and then call over to the
|
||||
3-arg version.
|
||||
* The `PodCastError` type now supports `Hash` and `Display`. Also if you enable
|
||||
the `extern_crate_std` feature then it will support `std::error::Error`.
|
||||
* We now provide a `TransparentWrapper<T>` impl for `core::num::Wrapper<T>`.
|
||||
* The error type of `try_from_bytes` and `try_from_bytes_mut` when the input
|
||||
isn't aligned has been corrected from being `AlignmentMismatch` (intended for
|
||||
allocation casting only) to `TargetAlignmentGreaterAndInputNotAligned`.
|
||||
|
||||
## 1.3.0
|
||||
|
||||
* Had a bug because the CI was messed up! It wasn't soundness related, because
|
||||
it prevented the crate from building entirely if the `extern_crate_alloc`
|
||||
feature was used. Still, this is yanked, sorry.
|
||||
|
||||
## 1.2.0
|
||||
|
||||
* [thomcc](https://github.com/thomcc) added many things:
|
||||
* A fully sound `offset_of!` macro
|
||||
[#10](https://github.com/Lokathor/bytemuck/pull/10)
|
||||
* A `Contiguous` trait for when you've got enums with declared values
|
||||
all in a row [#12](https://github.com/Lokathor/bytemuck/pull/12)
|
||||
* A `TransparentWrapper` marker trait for when you want to more clearly
|
||||
enable adding and removing a wrapper struct to its inner value
|
||||
[#15](https://github.com/Lokathor/bytemuck/pull/15)
|
||||
* Now MIRI is run on CI in every single push!
|
||||
[#16](https://github.com/Lokathor/bytemuck/pull/16)
|
||||
|
||||
## 1.1.0
|
||||
|
||||
* [SimonSapin](https://github.com/SimonSapin) added `from_bytes`,
|
||||
`from_bytes_mut`, `try_from_bytes`, and `try_from_bytes_mut` ([PR
|
||||
Link](https://github.com/Lokathor/bytemuck/pull/8))
|
||||
|
||||
## 1.0.1
|
||||
|
||||
* Changed to the [zlib](https://opensource.org/licenses/Zlib) license.
|
||||
* Added much more proper documentation.
|
||||
* Reduced the minimum Rust version to 1.34
|
16
third_party/rust/bytemuck/rustfmt.toml
vendored
Normal file
16
third_party/rust/bytemuck/rustfmt.toml
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
# Based on
|
||||
# https://github.com/rust-lang/rustfmt/blob/rustfmt-1.4.19/Configurations.md
|
||||
|
||||
# Stable
|
||||
edition = "2018"
|
||||
fn_args_layout = "Compressed"
|
||||
max_width = 80
|
||||
tab_spaces = 2
|
||||
use_field_init_shorthand = true
|
||||
use_try_shorthand = true
|
||||
use_small_heuristics = "Max"
|
||||
|
||||
# Unstable
|
||||
format_code_in_doc_comments = true
|
||||
imports_granularity = "Crate"
|
||||
wrap_comments = true
|
882
third_party/rust/bytemuck/src/allocation.rs
vendored
Normal file
882
third_party/rust/bytemuck/src/allocation.rs
vendored
Normal file
@ -0,0 +1,882 @@
|
||||
#![cfg(feature = "extern_crate_alloc")]
|
||||
|
||||
//! Stuff to boost things in the `alloc` crate.
|
||||
//!
|
||||
//! * You must enable the `extern_crate_alloc` feature of `bytemuck` or you will
|
||||
//! not be able to use this module! This is generally done by adding the
|
||||
//! feature to the dependency in Cargo.toml like so:
|
||||
//!
|
||||
//! `bytemuck = { version = "VERSION_YOU_ARE_USING", features =
|
||||
//! ["extern_crate_alloc"]}`
|
||||
|
||||
use super::*;
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
use alloc::sync::Arc;
|
||||
use alloc::{
|
||||
alloc::{alloc_zeroed, Layout},
|
||||
boxed::Box,
|
||||
rc::Rc,
|
||||
vec,
|
||||
vec::Vec,
|
||||
};
|
||||
use core::ops::{Deref, DerefMut};
|
||||
|
||||
/// As [`try_cast_box`](try_cast_box), but unwraps for you.
|
||||
#[inline]
|
||||
pub fn cast_box<A: NoUninit, B: AnyBitPattern>(input: Box<A>) -> Box<B> {
|
||||
try_cast_box(input).map_err(|(e, _v)| e).unwrap()
|
||||
}
|
||||
|
||||
/// Attempts to cast the content type of a [`Box`](alloc::boxed::Box).
|
||||
///
|
||||
/// On failure you get back an error along with the starting `Box`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * The start and end content type of the `Box` must have the exact same
|
||||
/// alignment.
|
||||
/// * The start and end size of the `Box` must have the exact same size.
|
||||
#[inline]
|
||||
pub fn try_cast_box<A: NoUninit, B: AnyBitPattern>(
|
||||
input: Box<A>,
|
||||
) -> Result<Box<B>, (PodCastError, Box<A>)> {
|
||||
if align_of::<A>() != align_of::<B>() {
|
||||
Err((PodCastError::AlignmentMismatch, input))
|
||||
} else if size_of::<A>() != size_of::<B>() {
|
||||
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;
|
||||
Ok(unsafe { Box::from_raw(ptr) })
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocates a `Box<T>` with all of the contents being zeroed out.
|
||||
///
|
||||
/// This uses the global allocator to create a zeroed allocation and _then_
|
||||
/// turns it into a Box. In other words, it's 100% assured that the zeroed data
|
||||
/// won't be put temporarily on the stack. You can make a box of any size
|
||||
/// without fear of a stack overflow.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// This fails if the allocation fails.
|
||||
#[inline]
|
||||
pub fn try_zeroed_box<T: Zeroable>() -> Result<Box<T>, ()> {
|
||||
if size_of::<T>() == 0 {
|
||||
// This will not allocate but simply create a dangling pointer.
|
||||
let dangling = core::ptr::NonNull::dangling().as_ptr();
|
||||
return Ok(unsafe { Box::from_raw(dangling) });
|
||||
}
|
||||
let layout = Layout::new::<T>();
|
||||
let ptr = unsafe { alloc_zeroed(layout) };
|
||||
if ptr.is_null() {
|
||||
// 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) })
|
||||
}
|
||||
}
|
||||
|
||||
/// As [`try_zeroed_box`], but unwraps for you.
|
||||
#[inline]
|
||||
pub fn zeroed_box<T: Zeroable>() -> Box<T> {
|
||||
try_zeroed_box().unwrap()
|
||||
}
|
||||
|
||||
/// Allocates a `Vec<T>` of length and capacity exactly equal to `length` and
|
||||
/// all elements zeroed.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// This fails if the allocation fails, or if a layout cannot be calculated for
|
||||
/// the allocation.
|
||||
pub fn try_zeroed_vec<T: Zeroable>(length: usize) -> Result<Vec<T>, ()> {
|
||||
if length == 0 {
|
||||
Ok(Vec::new())
|
||||
} else {
|
||||
let boxed_slice = try_zeroed_slice_box(length)?;
|
||||
Ok(boxed_slice.into_vec())
|
||||
}
|
||||
}
|
||||
|
||||
/// As [`try_zeroed_vec`] but unwraps for you
|
||||
pub fn zeroed_vec<T: Zeroable>(length: usize) -> Vec<T> {
|
||||
try_zeroed_vec(length).unwrap()
|
||||
}
|
||||
|
||||
/// Allocates a `Box<[T]>` with all contents being zeroed out.
|
||||
///
|
||||
/// This uses the global allocator to create a zeroed allocation and _then_
|
||||
/// turns it into a Box. In other words, it's 100% assured that the zeroed data
|
||||
/// won't be put temporarily on the stack. You can make a box of any size
|
||||
/// without fear of a stack overflow.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// This fails if the allocation fails, or if a layout cannot be calculated for
|
||||
/// the allocation.
|
||||
#[inline]
|
||||
pub fn try_zeroed_slice_box<T: Zeroable>(
|
||||
length: usize,
|
||||
) -> Result<Box<[T]>, ()> {
|
||||
if size_of::<T>() == 0 || length == 0 {
|
||||
// This will not allocate but simply create a dangling slice pointer.
|
||||
let dangling = core::ptr::NonNull::dangling().as_ptr();
|
||||
let dangling_slice = core::ptr::slice_from_raw_parts_mut(dangling, length);
|
||||
return Ok(unsafe { Box::from_raw(dangling_slice) });
|
||||
}
|
||||
let layout = core::alloc::Layout::array::<T>(length).map_err(|_| ())?;
|
||||
let ptr = unsafe { alloc_zeroed(layout) };
|
||||
if ptr.is_null() {
|
||||
// we don't know what the error is because `alloc_zeroed` is a dumb API
|
||||
Err(())
|
||||
} else {
|
||||
let slice =
|
||||
unsafe { core::slice::from_raw_parts_mut(ptr as *mut T, length) };
|
||||
Ok(unsafe { Box::<[T]>::from_raw(slice) })
|
||||
}
|
||||
}
|
||||
|
||||
/// As [`try_zeroed_slice_box`](try_zeroed_slice_box), but unwraps for you.
|
||||
pub fn zeroed_slice_box<T: Zeroable>(length: usize) -> Box<[T]> {
|
||||
try_zeroed_slice_box(length).unwrap()
|
||||
}
|
||||
|
||||
/// As [`try_cast_slice_box`](try_cast_slice_box), but unwraps for you.
|
||||
#[inline]
|
||||
pub fn cast_slice_box<A: NoUninit, B: AnyBitPattern>(
|
||||
input: Box<[A]>,
|
||||
) -> Box<[B]> {
|
||||
try_cast_slice_box(input).map_err(|(e, _v)| e).unwrap()
|
||||
}
|
||||
|
||||
/// Attempts to cast the content type of a `Box<[T]>`.
|
||||
///
|
||||
/// On failure you get back an error along with the starting `Box<[T]>`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * The start and end content type of the `Box<[T]>` must have the exact same
|
||||
/// alignment.
|
||||
/// * The start and end content size in bytes of the `Box<[T]>` must be the
|
||||
/// exact same.
|
||||
#[inline]
|
||||
pub fn try_cast_slice_box<A: NoUninit, B: AnyBitPattern>(
|
||||
input: Box<[A]>,
|
||||
) -> Result<Box<[B]>, (PodCastError, Box<[A]>)> {
|
||||
if align_of::<A>() != align_of::<B>() {
|
||||
Err((PodCastError::AlignmentMismatch, input))
|
||||
} else if size_of::<A>() != size_of::<B>() {
|
||||
if size_of::<A>() * input.len() % size_of::<B>() != 0 {
|
||||
// If the size in bytes of the underlying buffer does not match an exact
|
||||
// multiple of the size of B, we cannot cast between them.
|
||||
Err((PodCastError::SizeMismatch, input))
|
||||
} else {
|
||||
// Because the size is an exact multiple, we can now change the length
|
||||
// of the slice and recreate the Box
|
||||
// NOTE: This is a valid operation because according to the docs of
|
||||
// std::alloc::GlobalAlloc::dealloc(), the Layout that was used to alloc
|
||||
// the block must be the same Layout that is used to dealloc the block.
|
||||
// Luckily, Layout only stores two things, the alignment, and the size in
|
||||
// bytes. So as long as both of those stay the same, the Layout will
|
||||
// remain a valid input to dealloc.
|
||||
let length = size_of::<A>() * input.len() / size_of::<B>();
|
||||
let box_ptr: *mut A = Box::into_raw(input) as *mut A;
|
||||
let ptr: *mut [B] =
|
||||
unsafe { core::slice::from_raw_parts_mut(box_ptr as *mut B, length) };
|
||||
Ok(unsafe { Box::<[B]>::from_raw(ptr) })
|
||||
}
|
||||
} else {
|
||||
let box_ptr: *mut [A] = Box::into_raw(input);
|
||||
let ptr: *mut [B] = box_ptr as *mut [B];
|
||||
Ok(unsafe { Box::<[B]>::from_raw(ptr) })
|
||||
}
|
||||
}
|
||||
|
||||
/// As [`try_cast_vec`](try_cast_vec), but unwraps for you.
|
||||
#[inline]
|
||||
pub fn cast_vec<A: NoUninit, B: AnyBitPattern>(input: Vec<A>) -> Vec<B> {
|
||||
try_cast_vec(input).map_err(|(e, _v)| e).unwrap()
|
||||
}
|
||||
|
||||
/// Attempts to cast the content type of a [`Vec`](alloc::vec::Vec).
|
||||
///
|
||||
/// On failure you get back an error along with the starting `Vec`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * The start and end content type of the `Vec` must have the exact same
|
||||
/// alignment.
|
||||
/// * The start and end content size in bytes of the `Vec` must be the exact
|
||||
/// same.
|
||||
/// * The start and end capacity in bytes of the `Vec` must be the exact same.
|
||||
#[inline]
|
||||
pub fn try_cast_vec<A: NoUninit, B: AnyBitPattern>(
|
||||
input: Vec<A>,
|
||||
) -> Result<Vec<B>, (PodCastError, Vec<A>)> {
|
||||
if align_of::<A>() != align_of::<B>() {
|
||||
Err((PodCastError::AlignmentMismatch, input))
|
||||
} else if size_of::<A>() != size_of::<B>() {
|
||||
if size_of::<A>() * input.len() % size_of::<B>() != 0
|
||||
|| size_of::<A>() * input.capacity() % size_of::<B>() != 0
|
||||
{
|
||||
// If the size in bytes of the underlying buffer does not match an exact
|
||||
// multiple of the size of B, we cannot cast between them.
|
||||
// Note that we have to pay special attention to make sure that both
|
||||
// length and capacity are valid under B, as we do not want to
|
||||
// change which bytes are considered part of the initialized slice
|
||||
// of the Vec
|
||||
Err((PodCastError::SizeMismatch, input))
|
||||
} else {
|
||||
// Because the size is an exact multiple, we can now change the length and
|
||||
// capacity and recreate the Vec
|
||||
// NOTE: This is a valid operation because according to the docs of
|
||||
// std::alloc::GlobalAlloc::dealloc(), the Layout that was used to alloc
|
||||
// the block must be the same Layout that is used to dealloc the block.
|
||||
// Luckily, Layout only stores two things, the alignment, and the size in
|
||||
// bytes. So as long as both of those stay the same, the Layout will
|
||||
// remain a valid input to dealloc.
|
||||
|
||||
// Note(Lokathor): First we record the length and capacity, which don't
|
||||
// have any secret provenance metadata.
|
||||
let length: usize = size_of::<A>() * input.len() / size_of::<B>();
|
||||
let capacity: usize = size_of::<A>() * input.capacity() / size_of::<B>();
|
||||
// Note(Lokathor): Next we "pre-forget" the old Vec by wrapping with
|
||||
// ManuallyDrop, because if we used `core::mem::forget` after taking the
|
||||
// pointer then that would invalidate our pointer. In nightly there's a
|
||||
// "into raw parts" method, which we can switch this too eventually.
|
||||
let mut manual_drop_vec = ManuallyDrop::new(input);
|
||||
let vec_ptr: *mut A = manual_drop_vec.as_mut_ptr();
|
||||
let ptr: *mut B = vec_ptr as *mut B;
|
||||
Ok(unsafe { Vec::from_raw_parts(ptr, length, capacity) })
|
||||
}
|
||||
} else {
|
||||
// Note(Lokathor): First we record the length and capacity, which don't have
|
||||
// any secret provenance metadata.
|
||||
let length: usize = input.len();
|
||||
let capacity: usize = input.capacity();
|
||||
// Note(Lokathor): Next we "pre-forget" the old Vec by wrapping with
|
||||
// ManuallyDrop, because if we used `core::mem::forget` after taking the
|
||||
// pointer then that would invalidate our pointer. In nightly there's a
|
||||
// "into raw parts" method, which we can switch this too eventually.
|
||||
let mut manual_drop_vec = ManuallyDrop::new(input);
|
||||
let vec_ptr: *mut A = manual_drop_vec.as_mut_ptr();
|
||||
let ptr: *mut B = vec_ptr as *mut B;
|
||||
Ok(unsafe { Vec::from_raw_parts(ptr, length, capacity) })
|
||||
}
|
||||
}
|
||||
|
||||
/// This "collects" a slice of pod data into a vec of a different pod type.
|
||||
///
|
||||
/// Unlike with [`cast_slice`] and [`cast_slice_mut`], this will always work.
|
||||
///
|
||||
/// The output vec will be of a minimal size/capacity to hold the slice given.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck::*;
|
||||
/// let halfwords: [u16; 4] = [5, 6, 7, 8];
|
||||
/// let vec_of_words: Vec<u32> = pod_collect_to_vec(&halfwords);
|
||||
/// if cfg!(target_endian = "little") {
|
||||
/// assert_eq!(&vec_of_words[..], &[0x0006_0005, 0x0008_0007][..])
|
||||
/// } else {
|
||||
/// assert_eq!(&vec_of_words[..], &[0x0005_0006, 0x0007_0008][..])
|
||||
/// }
|
||||
/// ```
|
||||
pub fn pod_collect_to_vec<A: NoUninit, B: NoUninit + AnyBitPattern>(
|
||||
src: &[A],
|
||||
) -> Vec<B> {
|
||||
let src_size = size_of_val(src);
|
||||
// Note(Lokathor): dst_count is rounded up so that the dest will always be at
|
||||
// least as many bytes as the src.
|
||||
let dst_count = src_size / size_of::<B>()
|
||||
+ if src_size % size_of::<B>() != 0 { 1 } else { 0 };
|
||||
let mut dst = vec![B::zeroed(); dst_count];
|
||||
|
||||
let src_bytes: &[u8] = cast_slice(src);
|
||||
let dst_bytes: &mut [u8] = cast_slice_mut(&mut dst[..]);
|
||||
dst_bytes[..src_size].copy_from_slice(src_bytes);
|
||||
dst
|
||||
}
|
||||
|
||||
/// As [`try_cast_rc`](try_cast_rc), but unwraps for you.
|
||||
#[inline]
|
||||
pub fn cast_rc<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>(
|
||||
input: Rc<A>,
|
||||
) -> Rc<B> {
|
||||
try_cast_rc(input).map_err(|(e, _v)| e).unwrap()
|
||||
}
|
||||
|
||||
/// Attempts to cast the content type of a [`Rc`](alloc::rc::Rc).
|
||||
///
|
||||
/// On failure you get back an error along with the starting `Rc`.
|
||||
///
|
||||
/// The bounds on this function are the same as [`cast_mut`], because a user
|
||||
/// could call `Rc::get_unchecked_mut` on the output, which could be observable
|
||||
/// in the input.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * The start and end content type of the `Rc` must have the exact same
|
||||
/// alignment.
|
||||
/// * The start and end size of the `Rc` must have the exact same size.
|
||||
#[inline]
|
||||
pub fn try_cast_rc<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>(
|
||||
input: Rc<A>,
|
||||
) -> Result<Rc<B>, (PodCastError, Rc<A>)> {
|
||||
if align_of::<A>() != align_of::<B>() {
|
||||
Err((PodCastError::AlignmentMismatch, input))
|
||||
} else if size_of::<A>() != size_of::<B>() {
|
||||
Err((PodCastError::SizeMismatch, input))
|
||||
} else {
|
||||
// Safety: Rc::from_raw requires size and alignment match, which is met.
|
||||
let ptr: *const B = Rc::into_raw(input) as *const B;
|
||||
Ok(unsafe { Rc::from_raw(ptr) })
|
||||
}
|
||||
}
|
||||
|
||||
/// As [`try_cast_arc`](try_cast_arc), but unwraps for you.
|
||||
#[inline]
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
pub fn cast_arc<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>(
|
||||
input: Arc<A>,
|
||||
) -> Arc<B> {
|
||||
try_cast_arc(input).map_err(|(e, _v)| e).unwrap()
|
||||
}
|
||||
|
||||
/// Attempts to cast the content type of a [`Arc`](alloc::sync::Arc).
|
||||
///
|
||||
/// On failure you get back an error along with the starting `Arc`.
|
||||
///
|
||||
/// The bounds on this function are the same as [`cast_mut`], because a user
|
||||
/// could call `Rc::get_unchecked_mut` on the output, which could be observable
|
||||
/// in the input.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * The start and end content type of the `Arc` must have the exact same
|
||||
/// alignment.
|
||||
/// * The start and end size of the `Arc` must have the exact same size.
|
||||
#[inline]
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
pub fn try_cast_arc<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
input: Arc<A>,
|
||||
) -> Result<Arc<B>, (PodCastError, Arc<A>)> {
|
||||
if align_of::<A>() != align_of::<B>() {
|
||||
Err((PodCastError::AlignmentMismatch, input))
|
||||
} else if size_of::<A>() != size_of::<B>() {
|
||||
Err((PodCastError::SizeMismatch, input))
|
||||
} else {
|
||||
// Safety: Arc::from_raw requires size and alignment match, which is met.
|
||||
let ptr: *const B = Arc::into_raw(input) as *const B;
|
||||
Ok(unsafe { Arc::from_raw(ptr) })
|
||||
}
|
||||
}
|
||||
|
||||
/// As [`try_cast_slice_rc`](try_cast_slice_rc), but unwraps for you.
|
||||
#[inline]
|
||||
pub fn cast_slice_rc<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
input: Rc<[A]>,
|
||||
) -> Rc<[B]> {
|
||||
try_cast_slice_rc(input).map_err(|(e, _v)| e).unwrap()
|
||||
}
|
||||
|
||||
/// Attempts to cast the content type of a `Rc<[T]>`.
|
||||
///
|
||||
/// On failure you get back an error along with the starting `Rc<[T]>`.
|
||||
///
|
||||
/// The bounds on this function are the same as [`cast_mut`], because a user
|
||||
/// could call `Rc::get_unchecked_mut` on the output, which could be observable
|
||||
/// in the input.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * The start and end content type of the `Rc<[T]>` must have the exact same
|
||||
/// alignment.
|
||||
/// * The start and end content size in bytes of the `Rc<[T]>` must be the exact
|
||||
/// same.
|
||||
#[inline]
|
||||
pub fn try_cast_slice_rc<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
input: Rc<[A]>,
|
||||
) -> Result<Rc<[B]>, (PodCastError, Rc<[A]>)> {
|
||||
if align_of::<A>() != align_of::<B>() {
|
||||
Err((PodCastError::AlignmentMismatch, input))
|
||||
} else if size_of::<A>() != size_of::<B>() {
|
||||
if size_of::<A>() * input.len() % size_of::<B>() != 0 {
|
||||
// If the size in bytes of the underlying buffer does not match an exact
|
||||
// multiple of the size of B, we cannot cast between them.
|
||||
Err((PodCastError::SizeMismatch, input))
|
||||
} else {
|
||||
// Because the size is an exact multiple, we can now change the length
|
||||
// of the slice and recreate the Rc
|
||||
// NOTE: This is a valid operation because according to the docs of
|
||||
// std::rc::Rc::from_raw(), the type U that was in the original Rc<U>
|
||||
// acquired from Rc::into_raw() must have the same size alignment and
|
||||
// size of the type T in the new Rc<T>. So as long as both the size
|
||||
// and alignment stay the same, the Rc will remain a valid Rc.
|
||||
let length = size_of::<A>() * input.len() / size_of::<B>();
|
||||
let rc_ptr: *const A = Rc::into_raw(input) as *const A;
|
||||
// Must use ptr::slice_from_raw_parts, because we cannot make an
|
||||
// intermediate const reference, because it has mutable provenance,
|
||||
// nor an intermediate mutable reference, because it could be aliased.
|
||||
let ptr = core::ptr::slice_from_raw_parts(rc_ptr as *const B, length);
|
||||
Ok(unsafe { Rc::<[B]>::from_raw(ptr) })
|
||||
}
|
||||
} else {
|
||||
let rc_ptr: *const [A] = Rc::into_raw(input);
|
||||
let ptr: *const [B] = rc_ptr as *const [B];
|
||||
Ok(unsafe { Rc::<[B]>::from_raw(ptr) })
|
||||
}
|
||||
}
|
||||
|
||||
/// As [`try_cast_slice_arc`](try_cast_slice_arc), but unwraps for you.
|
||||
#[inline]
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
pub fn cast_slice_arc<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
input: Arc<[A]>,
|
||||
) -> Arc<[B]> {
|
||||
try_cast_slice_arc(input).map_err(|(e, _v)| e).unwrap()
|
||||
}
|
||||
|
||||
/// Attempts to cast the content type of a `Arc<[T]>`.
|
||||
///
|
||||
/// On failure you get back an error along with the starting `Arc<[T]>`.
|
||||
///
|
||||
/// The bounds on this function are the same as [`cast_mut`], because a user
|
||||
/// could call `Rc::get_unchecked_mut` on the output, which could be observable
|
||||
/// in the input.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * The start and end content type of the `Arc<[T]>` must have the exact same
|
||||
/// alignment.
|
||||
/// * The start and end content size in bytes of the `Arc<[T]>` must be the
|
||||
/// exact same.
|
||||
#[inline]
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
pub fn try_cast_slice_arc<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
input: Arc<[A]>,
|
||||
) -> Result<Arc<[B]>, (PodCastError, Arc<[A]>)> {
|
||||
if align_of::<A>() != align_of::<B>() {
|
||||
Err((PodCastError::AlignmentMismatch, input))
|
||||
} else if size_of::<A>() != size_of::<B>() {
|
||||
if size_of::<A>() * input.len() % size_of::<B>() != 0 {
|
||||
// If the size in bytes of the underlying buffer does not match an exact
|
||||
// multiple of the size of B, we cannot cast between them.
|
||||
Err((PodCastError::SizeMismatch, input))
|
||||
} else {
|
||||
// Because the size is an exact multiple, we can now change the length
|
||||
// of the slice and recreate the Arc
|
||||
// NOTE: This is a valid operation because according to the docs of
|
||||
// std::sync::Arc::from_raw(), the type U that was in the original Arc<U>
|
||||
// acquired from Arc::into_raw() must have the same size alignment and
|
||||
// size of the type T in the new Arc<T>. So as long as both the size
|
||||
// and alignment stay the same, the Arc will remain a valid Arc.
|
||||
let length = size_of::<A>() * input.len() / size_of::<B>();
|
||||
let arc_ptr: *const A = Arc::into_raw(input) as *const A;
|
||||
// Must use ptr::slice_from_raw_parts, because we cannot make an
|
||||
// intermediate const reference, because it has mutable provenance,
|
||||
// nor an intermediate mutable reference, because it could be aliased.
|
||||
let ptr = core::ptr::slice_from_raw_parts(arc_ptr as *const B, length);
|
||||
Ok(unsafe { Arc::<[B]>::from_raw(ptr) })
|
||||
}
|
||||
} else {
|
||||
let arc_ptr: *const [A] = Arc::into_raw(input);
|
||||
let ptr: *const [B] = arc_ptr as *const [B];
|
||||
Ok(unsafe { Arc::<[B]>::from_raw(ptr) })
|
||||
}
|
||||
}
|
||||
|
||||
/// An extension trait for `TransparentWrapper` and alloc types.
|
||||
pub trait TransparentWrapperAlloc<Inner: ?Sized>:
|
||||
TransparentWrapper<Inner>
|
||||
{
|
||||
/// Convert a vec of the inner type into a vec of the wrapper type.
|
||||
fn wrap_vec(s: Vec<Inner>) -> Vec<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
Inner: Sized,
|
||||
{
|
||||
let mut s = core::mem::ManuallyDrop::new(s);
|
||||
|
||||
let length = s.len();
|
||||
let capacity = s.capacity();
|
||||
let ptr = s.as_mut_ptr();
|
||||
|
||||
unsafe {
|
||||
// SAFETY:
|
||||
// * ptr comes from Vec (and will not be double-dropped)
|
||||
// * the two types have the identical representation
|
||||
// * the len and capacity fields are valid
|
||||
Vec::from_raw_parts(ptr as *mut Self, length, capacity)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a box to the inner type into a box to the wrapper
|
||||
/// type.
|
||||
#[inline]
|
||||
fn wrap_box(s: Box<Inner>) -> Box<Self> {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
|
||||
unsafe {
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the sizes are unspecified.
|
||||
//
|
||||
// SAFETY:
|
||||
// * The unsafe contract requires that pointers to Inner and Self have
|
||||
// identical representations
|
||||
// * Box is guaranteed to have representation identical to a (non-null)
|
||||
// pointer
|
||||
// * The pointer comes from a box (and thus satisfies all safety
|
||||
// requirements of Box)
|
||||
let inner_ptr: *mut Inner = Box::into_raw(s);
|
||||
let wrapper_ptr: *mut Self = transmute!(inner_ptr);
|
||||
Box::from_raw(wrapper_ptr)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert an [`Rc`](alloc::rc::Rc) to the inner type into an `Rc` to the
|
||||
/// wrapper type.
|
||||
#[inline]
|
||||
fn wrap_rc(s: Rc<Inner>) -> Rc<Self> {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
|
||||
unsafe {
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the layout of Rc is unspecified.
|
||||
//
|
||||
// SAFETY:
|
||||
// * The unsafe contract requires that pointers to Inner and Self have
|
||||
// identical representations, and that the size and alignment of Inner
|
||||
// and Self are the same, which meets the safety requirements of
|
||||
// Rc::from_raw
|
||||
let inner_ptr: *const Inner = Rc::into_raw(s);
|
||||
let wrapper_ptr: *const Self = transmute!(inner_ptr);
|
||||
Rc::from_raw(wrapper_ptr)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert an [`Arc`](alloc::sync::Arc) to the inner type into an `Arc` to
|
||||
/// the wrapper type.
|
||||
#[inline]
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
fn wrap_arc(s: Arc<Inner>) -> Arc<Self> {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
|
||||
unsafe {
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the layout of Arc is unspecified.
|
||||
//
|
||||
// SAFETY:
|
||||
// * The unsafe contract requires that pointers to Inner and Self have
|
||||
// identical representations, and that the size and alignment of Inner
|
||||
// and Self are the same, which meets the safety requirements of
|
||||
// Arc::from_raw
|
||||
let inner_ptr: *const Inner = Arc::into_raw(s);
|
||||
let wrapper_ptr: *const Self = transmute!(inner_ptr);
|
||||
Arc::from_raw(wrapper_ptr)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a vec of the wrapper type into a vec of the inner type.
|
||||
fn peel_vec(s: Vec<Self>) -> Vec<Inner>
|
||||
where
|
||||
Self: Sized,
|
||||
Inner: Sized,
|
||||
{
|
||||
let mut s = core::mem::ManuallyDrop::new(s);
|
||||
|
||||
let length = s.len();
|
||||
let capacity = s.capacity();
|
||||
let ptr = s.as_mut_ptr();
|
||||
|
||||
unsafe {
|
||||
// SAFETY:
|
||||
// * ptr comes from Vec (and will not be double-dropped)
|
||||
// * the two types have the identical representation
|
||||
// * the len and capacity fields are valid
|
||||
Vec::from_raw_parts(ptr as *mut Inner, length, capacity)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a box to the wrapper type into a box to the inner
|
||||
/// type.
|
||||
#[inline]
|
||||
fn peel_box(s: Box<Self>) -> Box<Inner> {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
|
||||
unsafe {
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the sizes are unspecified.
|
||||
//
|
||||
// SAFETY:
|
||||
// * The unsafe contract requires that pointers to Inner and Self have
|
||||
// identical representations
|
||||
// * Box is guaranteed to have representation identical to a (non-null)
|
||||
// pointer
|
||||
// * The pointer comes from a box (and thus satisfies all safety
|
||||
// requirements of Box)
|
||||
let wrapper_ptr: *mut Self = Box::into_raw(s);
|
||||
let inner_ptr: *mut Inner = transmute!(wrapper_ptr);
|
||||
Box::from_raw(inner_ptr)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert an [`Rc`](alloc::rc::Rc) to the wrapper type into an `Rc` to the
|
||||
/// inner type.
|
||||
#[inline]
|
||||
fn peel_rc(s: Rc<Self>) -> Rc<Inner> {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
|
||||
unsafe {
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the layout of Rc is unspecified.
|
||||
//
|
||||
// SAFETY:
|
||||
// * The unsafe contract requires that pointers to Inner and Self have
|
||||
// identical representations, and that the size and alignment of Inner
|
||||
// and Self are the same, which meets the safety requirements of
|
||||
// Rc::from_raw
|
||||
let wrapper_ptr: *const Self = Rc::into_raw(s);
|
||||
let inner_ptr: *const Inner = transmute!(wrapper_ptr);
|
||||
Rc::from_raw(inner_ptr)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert an [`Arc`](alloc::sync::Arc) to the wrapper type into an `Arc` to
|
||||
/// the inner type.
|
||||
#[inline]
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
fn peel_arc(s: Arc<Self>) -> Arc<Inner> {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
|
||||
unsafe {
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the layout of Arc is unspecified.
|
||||
//
|
||||
// SAFETY:
|
||||
// * The unsafe contract requires that pointers to Inner and Self have
|
||||
// identical representations, and that the size and alignment of Inner
|
||||
// and Self are the same, which meets the safety requirements of
|
||||
// Arc::from_raw
|
||||
let wrapper_ptr: *const Self = Arc::into_raw(s);
|
||||
let inner_ptr: *const Inner = transmute!(wrapper_ptr);
|
||||
Arc::from_raw(inner_ptr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: ?Sized, T: ?Sized + TransparentWrapper<I>> TransparentWrapperAlloc<I>
|
||||
for T
|
||||
{
|
||||
}
|
||||
|
||||
/// As `Box<[u8]>`, but remembers the original alignment.
|
||||
pub struct BoxBytes {
|
||||
// SAFETY: `ptr` is owned, was allocated with `layout`, and points to
|
||||
// `layout.size()` initialized bytes.
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
}
|
||||
|
||||
impl Deref for BoxBytes {
|
||||
type Target = [u8];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// SAFETY: See type invariant.
|
||||
unsafe {
|
||||
core::slice::from_raw_parts(self.ptr.as_ptr(), self.layout.size())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for BoxBytes {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
// SAFETY: See type invariant.
|
||||
unsafe {
|
||||
core::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.layout.size())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BoxBytes {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: See type invariant.
|
||||
unsafe { alloc::alloc::dealloc(self.ptr.as_ptr(), self.layout) };
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + sealed::BoxBytesOf> From<Box<T>> for BoxBytes {
|
||||
fn from(value: Box<T>) -> Self {
|
||||
value.box_bytes_of()
|
||||
}
|
||||
}
|
||||
|
||||
mod sealed {
|
||||
use crate::{BoxBytes, PodCastError};
|
||||
use alloc::boxed::Box;
|
||||
|
||||
pub trait BoxBytesOf {
|
||||
fn box_bytes_of(self: Box<Self>) -> BoxBytes;
|
||||
}
|
||||
|
||||
pub trait FromBoxBytes {
|
||||
fn try_from_box_bytes(
|
||||
bytes: BoxBytes,
|
||||
) -> Result<Box<Self>, (PodCastError, BoxBytes)>;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: NoUninit> sealed::BoxBytesOf for T {
|
||||
fn box_bytes_of(self: Box<Self>) -> BoxBytes {
|
||||
let layout = Layout::new::<T>();
|
||||
let ptr = Box::into_raw(self) as *mut u8;
|
||||
// SAFETY: Box::into_raw() returns a non-null pointer.
|
||||
let ptr = unsafe { NonNull::new_unchecked(ptr) };
|
||||
BoxBytes { ptr, layout }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: NoUninit> sealed::BoxBytesOf for [T] {
|
||||
fn box_bytes_of(self: Box<Self>) -> BoxBytes {
|
||||
let layout = Layout::for_value::<[T]>(&self);
|
||||
let ptr = Box::into_raw(self) as *mut u8;
|
||||
// SAFETY: Box::into_raw() returns a non-null pointer.
|
||||
let ptr = unsafe { NonNull::new_unchecked(ptr) };
|
||||
BoxBytes { ptr, layout }
|
||||
}
|
||||
}
|
||||
|
||||
impl sealed::BoxBytesOf for str {
|
||||
fn box_bytes_of(self: Box<Self>) -> BoxBytes {
|
||||
self.into_boxed_bytes().box_bytes_of()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnyBitPattern> sealed::FromBoxBytes for T {
|
||||
fn try_from_box_bytes(
|
||||
bytes: BoxBytes,
|
||||
) -> Result<Box<Self>, (PodCastError, BoxBytes)> {
|
||||
let layout = Layout::new::<T>();
|
||||
if bytes.layout.align() != layout.align() {
|
||||
Err((PodCastError::AlignmentMismatch, bytes))
|
||||
} else if bytes.layout.size() != layout.size() {
|
||||
Err((PodCastError::SizeMismatch, bytes))
|
||||
} else {
|
||||
let (ptr, _) = bytes.into_raw_parts();
|
||||
// SAFETY: See BoxBytes type invariant.
|
||||
Ok(unsafe { Box::from_raw(ptr.as_ptr() as *mut T) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnyBitPattern> sealed::FromBoxBytes for [T] {
|
||||
fn try_from_box_bytes(
|
||||
bytes: BoxBytes,
|
||||
) -> Result<Box<Self>, (PodCastError, BoxBytes)> {
|
||||
let single_layout = Layout::new::<T>();
|
||||
if bytes.layout.align() != single_layout.align() {
|
||||
Err((PodCastError::AlignmentMismatch, bytes))
|
||||
} else if single_layout.size() == 0 {
|
||||
Err((PodCastError::SizeMismatch, bytes))
|
||||
} else if bytes.layout.size() % single_layout.size() != 0 {
|
||||
Err((PodCastError::OutputSliceWouldHaveSlop, bytes))
|
||||
} else {
|
||||
let (ptr, layout) = bytes.into_raw_parts();
|
||||
let length = layout.size() / single_layout.size();
|
||||
let ptr =
|
||||
core::ptr::slice_from_raw_parts_mut(ptr.as_ptr() as *mut T, length);
|
||||
// SAFETY: See BoxBytes type invariant.
|
||||
Ok(unsafe { Box::from_raw(ptr) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `Box<T>` as `BoxBytes`.
|
||||
///
|
||||
/// `T` must be either [`Sized`] and [`NoUninit`],
|
||||
/// [`[U]`](slice) where `U: NoUninit`, or [`str`].
|
||||
#[inline]
|
||||
pub fn box_bytes_of<T: sealed::BoxBytesOf + ?Sized>(input: Box<T>) -> BoxBytes {
|
||||
input.box_bytes_of()
|
||||
}
|
||||
|
||||
/// Re-interprets `BoxBytes` as `Box<T>`.
|
||||
///
|
||||
/// `T` must be either [`Sized`] + [`AnyBitPattern`], or
|
||||
/// [`[U]`](slice) where `U: AnyBitPattern`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_from_box_bytes`] but will panic on error and the input will be
|
||||
/// dropped.
|
||||
#[inline]
|
||||
pub fn from_box_bytes<T: sealed::FromBoxBytes + ?Sized>(
|
||||
input: BoxBytes,
|
||||
) -> Box<T> {
|
||||
try_from_box_bytes(input).map_err(|(error, _)| error).unwrap()
|
||||
}
|
||||
|
||||
/// Re-interprets `BoxBytes` as `Box<T>`.
|
||||
///
|
||||
/// `T` must be either [`Sized`] + [`AnyBitPattern`], or
|
||||
/// [`[U]`](slice) where `U: AnyBitPattern`.
|
||||
///
|
||||
/// Returns `Err`:
|
||||
/// * If the input isn't aligned for `T`.
|
||||
/// * If `T: Sized` and the input's length isn't exactly the size of `T`.
|
||||
/// * If `T = [U]` and the input's length isn't exactly a multiple of the size
|
||||
/// of `U`.
|
||||
#[inline]
|
||||
pub fn try_from_box_bytes<T: sealed::FromBoxBytes + ?Sized>(
|
||||
input: BoxBytes,
|
||||
) -> Result<Box<T>, (PodCastError, BoxBytes)> {
|
||||
T::try_from_box_bytes(input)
|
||||
}
|
||||
|
||||
impl BoxBytes {
|
||||
/// Constructs a `BoxBytes` from its raw parts.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The pointer is owned, has been allocated with the provided layout, and
|
||||
/// points to `layout.size()` initialized bytes.
|
||||
pub unsafe fn from_raw_parts(ptr: NonNull<u8>, layout: Layout) -> Self {
|
||||
BoxBytes { ptr, layout }
|
||||
}
|
||||
|
||||
/// Deconstructs a `BoxBytes` into its raw parts.
|
||||
///
|
||||
/// The pointer is owned, has been allocated with the provided layout, and
|
||||
/// points to `layout.size()` initialized bytes.
|
||||
pub fn into_raw_parts(self) -> (NonNull<u8>, Layout) {
|
||||
let me = ManuallyDrop::new(self);
|
||||
(me.ptr, me.layout)
|
||||
}
|
||||
|
||||
/// Returns the original layout.
|
||||
pub fn layout(&self) -> Layout {
|
||||
self.layout
|
||||
}
|
||||
}
|
61
third_party/rust/bytemuck/src/anybitpattern.rs
vendored
Normal file
61
third_party/rust/bytemuck/src/anybitpattern.rs
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
use crate::{Pod, Zeroable};
|
||||
|
||||
/// Marker trait for "plain old data" types that are valid for any bit pattern.
|
||||
///
|
||||
/// The requirements for this is very similar to [`Pod`],
|
||||
/// except that the type can allow uninit (or padding) bytes.
|
||||
/// This limits what you can do with a type of this kind, but also broadens the
|
||||
/// included types to `repr(C)` `struct`s that contain padding as well as
|
||||
/// `union`s. Notably, you can only cast *immutable* references and *owned*
|
||||
/// values into [`AnyBitPattern`] types, not *mutable* references.
|
||||
///
|
||||
/// [`Pod`] is a subset of [`AnyBitPattern`], meaning that any `T: Pod` is also
|
||||
/// [`AnyBitPattern`] but any `T: AnyBitPattern` is not necessarily [`Pod`].
|
||||
///
|
||||
/// [`AnyBitPattern`] is a subset of [`Zeroable`], meaning that any `T:
|
||||
/// AnyBitPattern` is also [`Zeroable`], but any `T: Zeroable` is not
|
||||
/// necessarily [`AnyBitPattern`]
|
||||
///
|
||||
/// # Derive
|
||||
///
|
||||
/// A `#[derive(AnyBitPattern)]` macro is provided under the `derive` feature
|
||||
/// flag which will automatically validate the requirements of this trait and
|
||||
/// implement the trait for you for both structs and enums. This is the
|
||||
/// recommended method for implementing the trait, however it's also possible to
|
||||
/// do manually. If you implement it manually, you *must* carefully follow the
|
||||
/// below safety rules.
|
||||
///
|
||||
/// * *NOTE: even `C-style`, fieldless enums are intentionally **excluded** from
|
||||
/// this trait, since it is **unsound** for an enum to have a discriminant value
|
||||
/// that is not one of its defined variants.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Similar to [`Pod`] except we disregard the rule about it must not contain
|
||||
/// uninit bytes. Still, this is a quite strong guarantee about a type, so *be
|
||||
/// careful* when implementing it manually.
|
||||
///
|
||||
/// * The type must be inhabited (eg: no
|
||||
/// [Infallible](core::convert::Infallible)).
|
||||
/// * The type must be valid for any bit pattern of its backing memory.
|
||||
/// * Structs need to have all fields also be `AnyBitPattern`.
|
||||
/// * It is disallowed for types to contain pointer types, `Cell`, `UnsafeCell`,
|
||||
/// atomics, and any other forms of interior mutability.
|
||||
/// * More precisely: A shared reference to the type must allow reads, and
|
||||
/// *only* reads. RustBelt's separation logic is based on the notion that a
|
||||
/// type is allowed to define a sharing predicate, its own invariant that must
|
||||
/// hold for shared references, and this predicate is the reasoning that allow
|
||||
/// it to deal with atomic and cells etc. We require the sharing predicate to
|
||||
/// be trivial and permit only read-only access.
|
||||
/// * There's probably more, don't mess it up (I mean it).
|
||||
pub unsafe trait AnyBitPattern:
|
||||
Zeroable + Sized + Copy + 'static
|
||||
{
|
||||
}
|
||||
|
||||
unsafe impl<T: Pod> AnyBitPattern for T {}
|
||||
|
||||
#[cfg(feature = "zeroable_maybe_uninit")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "zeroable_maybe_uninit")))]
|
||||
unsafe impl<T> AnyBitPattern for core::mem::MaybeUninit<T> where T: AnyBitPattern
|
||||
{}
|
522
third_party/rust/bytemuck/src/checked.rs
vendored
Normal file
522
third_party/rust/bytemuck/src/checked.rs
vendored
Normal file
@ -0,0 +1,522 @@
|
||||
//! Checked versions of the casting functions exposed in crate root
|
||||
//! that support [`CheckedBitPattern`] types.
|
||||
|
||||
use crate::{
|
||||
internal::{self, something_went_wrong},
|
||||
AnyBitPattern, NoUninit,
|
||||
};
|
||||
|
||||
/// A marker trait that allows types that have some invalid bit patterns to be
|
||||
/// used in places that otherwise require [`AnyBitPattern`] or [`Pod`] types by
|
||||
/// performing a runtime check on a perticular set of bits. This is particularly
|
||||
/// useful for types like fieldless ('C-style') enums, [`char`], bool, and
|
||||
/// structs containing them.
|
||||
///
|
||||
/// To do this, we define a `Bits` type which is a type with equivalent layout
|
||||
/// to `Self` other than the invalid bit patterns which disallow `Self` from
|
||||
/// being [`AnyBitPattern`]. This `Bits` type must itself implement
|
||||
/// [`AnyBitPattern`]. Then, we implement a function that checks whether a
|
||||
/// certain instance of the `Bits` is also a valid bit pattern of `Self`. If
|
||||
/// this check passes, then we can allow casting from the `Bits` to `Self` (and
|
||||
/// therefore, any type which is able to be cast to `Bits` is also able to be
|
||||
/// cast to `Self`).
|
||||
///
|
||||
/// [`AnyBitPattern`] is a subset of [`CheckedBitPattern`], meaning that any `T:
|
||||
/// AnyBitPattern` is also [`CheckedBitPattern`]. This means you can also use
|
||||
/// any [`AnyBitPattern`] type in the checked versions of casting functions in
|
||||
/// this module. If it's possible, prefer implementing [`AnyBitPattern`] for
|
||||
/// your type directly instead of [`CheckedBitPattern`] as it gives greater
|
||||
/// flexibility.
|
||||
///
|
||||
/// # Derive
|
||||
///
|
||||
/// A `#[derive(CheckedBitPattern)]` macro is provided under the `derive`
|
||||
/// feature flag which will automatically validate the requirements of this
|
||||
/// trait and implement the trait for you for both enums and structs. This is
|
||||
/// the recommended method for implementing the trait, however it's also
|
||||
/// possible to do manually.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// If manually implementing the trait, we can do something like so:
|
||||
///
|
||||
/// ```rust
|
||||
/// use bytemuck::{CheckedBitPattern, NoUninit};
|
||||
///
|
||||
/// #[repr(u32)]
|
||||
/// #[derive(Copy, Clone)]
|
||||
/// enum MyEnum {
|
||||
/// Variant0 = 0,
|
||||
/// Variant1 = 1,
|
||||
/// Variant2 = 2,
|
||||
/// }
|
||||
///
|
||||
/// unsafe impl CheckedBitPattern for MyEnum {
|
||||
/// type Bits = u32;
|
||||
///
|
||||
/// fn is_valid_bit_pattern(bits: &u32) -> bool {
|
||||
/// match *bits {
|
||||
/// 0 | 1 | 2 => true,
|
||||
/// _ => false,
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // It is often useful to also implement `NoUninit` on our `CheckedBitPattern` types.
|
||||
/// // This will allow us to do casting of mutable references (and mutable slices).
|
||||
/// // It is not always possible to do so, but in this case we have no padding so it is.
|
||||
/// unsafe impl NoUninit for MyEnum {}
|
||||
/// ```
|
||||
///
|
||||
/// We can now use relevant casting functions. For example,
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck::{CheckedBitPattern, NoUninit};
|
||||
/// # #[repr(u32)]
|
||||
/// # #[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
/// # enum MyEnum {
|
||||
/// # Variant0 = 0,
|
||||
/// # Variant1 = 1,
|
||||
/// # Variant2 = 2,
|
||||
/// # }
|
||||
/// # unsafe impl NoUninit for MyEnum {}
|
||||
/// # unsafe impl CheckedBitPattern for MyEnum {
|
||||
/// # type Bits = u32;
|
||||
/// # fn is_valid_bit_pattern(bits: &u32) -> bool {
|
||||
/// # match *bits {
|
||||
/// # 0 | 1 | 2 => true,
|
||||
/// # _ => false,
|
||||
/// # }
|
||||
/// # }
|
||||
/// # }
|
||||
/// use bytemuck::{bytes_of, bytes_of_mut};
|
||||
/// use bytemuck::checked;
|
||||
///
|
||||
/// let bytes = bytes_of(&2u32);
|
||||
/// let result = checked::try_from_bytes::<MyEnum>(bytes);
|
||||
/// assert_eq!(result, Ok(&MyEnum::Variant2));
|
||||
///
|
||||
/// // Fails for invalid discriminant
|
||||
/// let bytes = bytes_of(&100u32);
|
||||
/// let result = checked::try_from_bytes::<MyEnum>(bytes);
|
||||
/// assert!(result.is_err());
|
||||
///
|
||||
/// // Since we implemented NoUninit, we can also cast mutably from an original type
|
||||
/// // that is `NoUninit + AnyBitPattern`:
|
||||
/// let mut my_u32 = 2u32;
|
||||
/// {
|
||||
/// let as_enum_mut = checked::cast_mut::<_, MyEnum>(&mut my_u32);
|
||||
/// assert_eq!(as_enum_mut, &mut MyEnum::Variant2);
|
||||
/// *as_enum_mut = MyEnum::Variant0;
|
||||
/// }
|
||||
/// assert_eq!(my_u32, 0u32);
|
||||
/// ```
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// * `Self` *must* have the same layout as the specified `Bits` except for
|
||||
/// the possible invalid bit patterns being checked during
|
||||
/// [`is_valid_bit_pattern`].
|
||||
/// * This almost certainly means your type must be `#[repr(C)]` or a similar
|
||||
/// specified repr, but if you think you know better, you probably don't. If
|
||||
/// you still think you know better, be careful and have fun. And don't mess
|
||||
/// it up (I mean it).
|
||||
/// * If [`is_valid_bit_pattern`] returns true, then the bit pattern contained
|
||||
/// in `bits` must also be valid for an instance of `Self`.
|
||||
/// * Probably more, don't mess it up (I mean it 2.0)
|
||||
///
|
||||
/// [`is_valid_bit_pattern`]: CheckedBitPattern::is_valid_bit_pattern
|
||||
/// [`Pod`]: crate::Pod
|
||||
pub unsafe trait CheckedBitPattern: Copy {
|
||||
/// `Self` *must* have the same layout as the specified `Bits` except for
|
||||
/// the possible invalid bit patterns being checked during
|
||||
/// [`is_valid_bit_pattern`].
|
||||
///
|
||||
/// [`is_valid_bit_pattern`]: CheckedBitPattern::is_valid_bit_pattern
|
||||
type Bits: AnyBitPattern;
|
||||
|
||||
/// If this function returns true, then it must be valid to reinterpret `bits`
|
||||
/// as `&Self`.
|
||||
fn is_valid_bit_pattern(bits: &Self::Bits) -> bool;
|
||||
}
|
||||
|
||||
unsafe impl<T: AnyBitPattern> CheckedBitPattern for T {
|
||||
type Bits = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn is_valid_bit_pattern(_bits: &T) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl CheckedBitPattern for char {
|
||||
type Bits = u32;
|
||||
|
||||
#[inline]
|
||||
fn is_valid_bit_pattern(bits: &Self::Bits) -> bool {
|
||||
core::char::from_u32(*bits).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl CheckedBitPattern for bool {
|
||||
type Bits = u8;
|
||||
|
||||
#[inline]
|
||||
fn is_valid_bit_pattern(bits: &Self::Bits) -> bool {
|
||||
match *bits {
|
||||
0 | 1 => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rust 1.70.0 documents that NonZero[int] has the same layout as [int].
|
||||
macro_rules! impl_checked_for_nonzero {
|
||||
($($nonzero:ty: $primitive:ty),* $(,)?) => {
|
||||
$(
|
||||
unsafe impl CheckedBitPattern for $nonzero {
|
||||
type Bits = $primitive;
|
||||
|
||||
#[inline]
|
||||
fn is_valid_bit_pattern(bits: &Self::Bits) -> bool {
|
||||
*bits != 0
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
impl_checked_for_nonzero! {
|
||||
core::num::NonZeroU8: u8,
|
||||
core::num::NonZeroI8: i8,
|
||||
core::num::NonZeroU16: u16,
|
||||
core::num::NonZeroI16: i16,
|
||||
core::num::NonZeroU32: u32,
|
||||
core::num::NonZeroI32: i32,
|
||||
core::num::NonZeroU64: u64,
|
||||
core::num::NonZeroI64: i64,
|
||||
core::num::NonZeroI128: i128,
|
||||
core::num::NonZeroU128: u128,
|
||||
core::num::NonZeroUsize: usize,
|
||||
core::num::NonZeroIsize: isize,
|
||||
}
|
||||
|
||||
/// The things that can go wrong when casting between [`CheckedBitPattern`] data
|
||||
/// forms.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum CheckedCastError {
|
||||
/// An error occurred during a true-[`Pod`] cast
|
||||
///
|
||||
/// [`Pod`]: crate::Pod
|
||||
PodCastError(crate::PodCastError),
|
||||
/// When casting to a [`CheckedBitPattern`] type, it is possible that the
|
||||
/// original data contains an invalid bit pattern. If so, the cast will
|
||||
/// fail and this error will be returned. Will never happen on casts
|
||||
/// between [`Pod`] types.
|
||||
///
|
||||
/// [`Pod`]: crate::Pod
|
||||
InvalidBitPattern,
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
impl core::fmt::Display for CheckedCastError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "extern_crate_std")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_std")))]
|
||||
impl std::error::Error for CheckedCastError {}
|
||||
|
||||
impl From<crate::PodCastError> for CheckedCastError {
|
||||
fn from(err: crate::PodCastError) -> CheckedCastError {
|
||||
CheckedCastError::PodCastError(err)
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&[u8]` as `&T`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the slice isn't aligned for the new type
|
||||
/// * If the slice's length isn’t exactly the size of the new type
|
||||
/// * If the slice contains an invalid bit pattern for `T`
|
||||
#[inline]
|
||||
pub fn try_from_bytes<T: CheckedBitPattern>(
|
||||
s: &[u8],
|
||||
) -> Result<&T, CheckedCastError> {
|
||||
let pod = crate::try_from_bytes(s)?;
|
||||
|
||||
if <T as CheckedBitPattern>::is_valid_bit_pattern(pod) {
|
||||
Ok(unsafe { &*(pod as *const <T as CheckedBitPattern>::Bits as *const T) })
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&mut [u8]` as `&mut T`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the slice isn't aligned for the new type
|
||||
/// * If the slice's length isn’t exactly the size of the new type
|
||||
/// * If the slice contains an invalid bit pattern for `T`
|
||||
#[inline]
|
||||
pub fn try_from_bytes_mut<T: CheckedBitPattern + NoUninit>(
|
||||
s: &mut [u8],
|
||||
) -> Result<&mut T, CheckedCastError> {
|
||||
let pod = unsafe { internal::try_from_bytes_mut(s) }?;
|
||||
|
||||
if <T as CheckedBitPattern>::is_valid_bit_pattern(pod) {
|
||||
Ok(unsafe { &mut *(pod as *mut <T as CheckedBitPattern>::Bits as *mut T) })
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads from the bytes as if they were a `T`.
|
||||
///
|
||||
/// ## Failure
|
||||
/// * If the `bytes` length is not equal to `size_of::<T>()`.
|
||||
/// * If the slice contains an invalid bit pattern for `T`
|
||||
#[inline]
|
||||
pub fn try_pod_read_unaligned<T: CheckedBitPattern>(
|
||||
bytes: &[u8],
|
||||
) -> Result<T, CheckedCastError> {
|
||||
let pod = crate::try_pod_read_unaligned(bytes)?;
|
||||
|
||||
if <T as CheckedBitPattern>::is_valid_bit_pattern(&pod) {
|
||||
Ok(unsafe { transmute!(pod) })
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to cast `T` into `U`.
|
||||
///
|
||||
/// Note that for this particular type of cast, alignment isn't a factor. The
|
||||
/// input value is semantically copied into the function and then returned to a
|
||||
/// new memory location which will have whatever the required alignment of the
|
||||
/// output type is.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the types don't have the same size this fails.
|
||||
/// * If `a` contains an invalid bit pattern for `B` this fails.
|
||||
#[inline]
|
||||
pub fn try_cast<A: NoUninit, B: CheckedBitPattern>(
|
||||
a: A,
|
||||
) -> Result<B, CheckedCastError> {
|
||||
let pod = crate::try_cast(a)?;
|
||||
|
||||
if <B as CheckedBitPattern>::is_valid_bit_pattern(&pod) {
|
||||
Ok(unsafe { transmute!(pod) })
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to convert a `&T` into `&U`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the reference isn't aligned in the new type
|
||||
/// * If the source type and target type aren't the same size.
|
||||
/// * If `a` contains an invalid bit pattern for `B` this fails.
|
||||
#[inline]
|
||||
pub fn try_cast_ref<A: NoUninit, B: CheckedBitPattern>(
|
||||
a: &A,
|
||||
) -> Result<&B, CheckedCastError> {
|
||||
let pod = crate::try_cast_ref(a)?;
|
||||
|
||||
if <B as CheckedBitPattern>::is_valid_bit_pattern(pod) {
|
||||
Ok(unsafe { &*(pod as *const <B as CheckedBitPattern>::Bits as *const B) })
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to convert a `&mut T` into `&mut U`.
|
||||
///
|
||||
/// As [`try_cast_ref`], but `mut`.
|
||||
#[inline]
|
||||
pub fn try_cast_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: CheckedBitPattern + NoUninit,
|
||||
>(
|
||||
a: &mut A,
|
||||
) -> Result<&mut B, CheckedCastError> {
|
||||
let pod = unsafe { internal::try_cast_mut(a) }?;
|
||||
|
||||
if <B as CheckedBitPattern>::is_valid_bit_pattern(pod) {
|
||||
Ok(unsafe { &mut *(pod as *mut <B as CheckedBitPattern>::Bits as *mut B) })
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to convert `&[A]` into `&[B]` (possibly with a change in length).
|
||||
///
|
||||
/// * `input.as_ptr() as usize == output.as_ptr() as usize`
|
||||
/// * `input.len() * size_of::<A>() == output.len() * size_of::<B>()`
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the target type has a greater alignment requirement and the input slice
|
||||
/// isn't aligned.
|
||||
/// * If the target element type is a different size from the current element
|
||||
/// type, and the output slice wouldn't be a whole number of elements when
|
||||
/// accounting for the size change (eg: 3 `u16` values is 1.5 `u32` values, so
|
||||
/// that's a failure).
|
||||
/// * Similarly, you can't convert between a [ZST](https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts)
|
||||
/// and a non-ZST.
|
||||
/// * If any element of the converted slice would contain an invalid bit pattern
|
||||
/// for `B` this fails.
|
||||
#[inline]
|
||||
pub fn try_cast_slice<A: NoUninit, B: CheckedBitPattern>(
|
||||
a: &[A],
|
||||
) -> Result<&[B], CheckedCastError> {
|
||||
let pod = crate::try_cast_slice(a)?;
|
||||
|
||||
if pod.iter().all(|pod| <B as CheckedBitPattern>::is_valid_bit_pattern(pod)) {
|
||||
Ok(unsafe {
|
||||
core::slice::from_raw_parts(pod.as_ptr() as *const B, pod.len())
|
||||
})
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to convert `&mut [A]` into `&mut [B]` (possibly with a change in
|
||||
/// length).
|
||||
///
|
||||
/// As [`try_cast_slice`], but `&mut`.
|
||||
#[inline]
|
||||
pub fn try_cast_slice_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: CheckedBitPattern + NoUninit,
|
||||
>(
|
||||
a: &mut [A],
|
||||
) -> Result<&mut [B], CheckedCastError> {
|
||||
let pod = unsafe { internal::try_cast_slice_mut(a) }?;
|
||||
|
||||
if pod.iter().all(|pod| <B as CheckedBitPattern>::is_valid_bit_pattern(pod)) {
|
||||
Ok(unsafe {
|
||||
core::slice::from_raw_parts_mut(pod.as_mut_ptr() as *mut B, pod.len())
|
||||
})
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&[u8]` as `&T`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_from_bytes`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn from_bytes<T: CheckedBitPattern>(s: &[u8]) -> &T {
|
||||
match try_from_bytes(s) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("from_bytes", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&mut [u8]` as `&mut T`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_from_bytes_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn from_bytes_mut<T: NoUninit + CheckedBitPattern>(s: &mut [u8]) -> &mut T {
|
||||
match try_from_bytes_mut(s) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("from_bytes_mut", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the slice into a `T` value.
|
||||
///
|
||||
/// ## Panics
|
||||
/// * This is like `try_pod_read_unaligned` but will panic on failure.
|
||||
#[inline]
|
||||
pub fn pod_read_unaligned<T: CheckedBitPattern>(bytes: &[u8]) -> T {
|
||||
match try_pod_read_unaligned(bytes) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("pod_read_unaligned", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `T` into `U`
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// * This is like [`try_cast`], but will panic on a size mismatch.
|
||||
#[inline]
|
||||
pub fn cast<A: NoUninit, B: CheckedBitPattern>(a: A) -> B {
|
||||
match try_cast(a) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("cast", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `&mut T` into `&mut U`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn cast_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + CheckedBitPattern,
|
||||
>(
|
||||
a: &mut A,
|
||||
) -> &mut B {
|
||||
match try_cast_mut(a) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("cast_mut", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `&T` into `&U`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_ref`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn cast_ref<A: NoUninit, B: CheckedBitPattern>(a: &A) -> &B {
|
||||
match try_cast_ref(a) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("cast_ref", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `&[A]` into `&[B]`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_slice`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn cast_slice<A: NoUninit, B: CheckedBitPattern>(a: &[A]) -> &[B] {
|
||||
match try_cast_slice(a) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("cast_slice", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `&mut [T]` into `&mut [U]`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_slice_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn cast_slice_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + CheckedBitPattern,
|
||||
>(
|
||||
a: &mut [A],
|
||||
) -> &mut [B] {
|
||||
match try_cast_slice_mut(a) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("cast_slice_mut", e),
|
||||
}
|
||||
}
|
202
third_party/rust/bytemuck/src/contiguous.rs
vendored
Normal file
202
third_party/rust/bytemuck/src/contiguous.rs
vendored
Normal file
@ -0,0 +1,202 @@
|
||||
use super::*;
|
||||
|
||||
/// A trait indicating that:
|
||||
///
|
||||
/// 1. A type has an equivalent representation to some known integral type.
|
||||
/// 2. All instances of this type fall in a fixed range of values.
|
||||
/// 3. Within that range, there are no gaps.
|
||||
///
|
||||
/// This is generally useful for fieldless enums (aka "c-style" enums), however
|
||||
/// it's important that it only be used for those with an explicit `#[repr]`, as
|
||||
/// `#[repr(Rust)]` fieldess enums have an unspecified layout.
|
||||
///
|
||||
/// Additionally, you shouldn't assume that all implementations are enums. Any
|
||||
/// type which meets the requirements above while following the rules under
|
||||
/// "Safety" below is valid.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use bytemuck::Contiguous;
|
||||
/// #[repr(u8)]
|
||||
/// #[derive(Debug, Copy, Clone, PartialEq)]
|
||||
/// enum Foo {
|
||||
/// A = 0,
|
||||
/// B = 1,
|
||||
/// C = 2,
|
||||
/// D = 3,
|
||||
/// E = 4,
|
||||
/// }
|
||||
/// unsafe impl Contiguous for Foo {
|
||||
/// type Int = u8;
|
||||
/// const MIN_VALUE: u8 = Foo::A as u8;
|
||||
/// const MAX_VALUE: u8 = Foo::E as u8;
|
||||
/// }
|
||||
/// assert_eq!(Foo::from_integer(3).unwrap(), Foo::D);
|
||||
/// assert_eq!(Foo::from_integer(8), None);
|
||||
/// assert_eq!(Foo::C.into_integer(), 2);
|
||||
/// ```
|
||||
/// # Safety
|
||||
///
|
||||
/// This is an unsafe trait, and incorrectly implementing it is undefined
|
||||
/// behavior.
|
||||
///
|
||||
/// Informally, by implementing it, you're asserting that `C` is identical to
|
||||
/// the integral type `C::Int`, and that every `C` falls between `C::MIN_VALUE`
|
||||
/// and `C::MAX_VALUE` exactly once, without any gaps.
|
||||
///
|
||||
/// Precisely, the guarantees you must uphold when implementing `Contiguous` for
|
||||
/// some type `C` are:
|
||||
///
|
||||
/// 1. The size of `C` and `C::Int` must be the same, and neither may be a ZST.
|
||||
/// (Note: alignment is explicitly allowed to differ)
|
||||
///
|
||||
/// 2. `C::Int` must be a primitive integer, and not a wrapper type. In the
|
||||
/// future, this may be lifted to include cases where the behavior is
|
||||
/// identical for a relevant set of traits (Ord, arithmetic, ...).
|
||||
///
|
||||
/// 3. All `C::Int`s which are in the *inclusive* range between `C::MIN_VALUE`
|
||||
/// and `C::MAX_VALUE` are bitwise identical to unique valid instances of
|
||||
/// `C`.
|
||||
///
|
||||
/// 4. There exist no instances of `C` such that their bitpatterns, when
|
||||
/// interpreted as instances of `C::Int`, fall outside of the `MAX_VALUE` /
|
||||
/// `MIN_VALUE` range -- It is legal for unsafe code to assume that if it
|
||||
/// gets a `C` that implements `Contiguous`, it is in the appropriate range.
|
||||
///
|
||||
/// 5. Finally, you promise not to provide overridden implementations of
|
||||
/// `Contiguous::from_integer` and `Contiguous::into_integer`.
|
||||
///
|
||||
/// For clarity, the following rules could be derived from the above, but are
|
||||
/// listed explicitly:
|
||||
///
|
||||
/// - `C::MAX_VALUE` must be greater or equal to `C::MIN_VALUE` (therefore, `C`
|
||||
/// must be an inhabited type).
|
||||
///
|
||||
/// - There exist no two values between `MIN_VALUE` and `MAX_VALUE` such that
|
||||
/// when interpreted as a `C` they are considered identical (by, say, match).
|
||||
pub unsafe trait Contiguous: Copy + 'static {
|
||||
/// The primitive integer type with an identical representation to this
|
||||
/// type.
|
||||
///
|
||||
/// Contiguous is broadly intended for use with fieldless enums, and for
|
||||
/// these the correct integer type is easy: The enum should have a
|
||||
/// `#[repr(Int)]` or `#[repr(C)]` attribute, (if it does not, it is
|
||||
/// *unsound* to implement `Contiguous`!).
|
||||
///
|
||||
/// - For `#[repr(Int)]`, use the listed `Int`. e.g. `#[repr(u8)]` should use
|
||||
/// `type Int = u8`.
|
||||
///
|
||||
/// - For `#[repr(C)]`, use whichever type the C compiler will use to
|
||||
/// represent the given enum. This is usually `c_int` (from `std::os::raw`
|
||||
/// or `libc`), but it's up to you to make the determination as the
|
||||
/// implementer of the unsafe trait.
|
||||
///
|
||||
/// For precise rules, see the list under "Safety" above.
|
||||
type Int: Copy + Ord;
|
||||
|
||||
/// The upper *inclusive* bound for valid instances of this type.
|
||||
const MAX_VALUE: Self::Int;
|
||||
|
||||
/// The lower *inclusive* bound for valid instances of this type.
|
||||
const MIN_VALUE: Self::Int;
|
||||
|
||||
/// If `value` is within the range for valid instances of this type,
|
||||
/// returns `Some(converted_value)`, otherwise, returns `None`.
|
||||
///
|
||||
/// This is a trait method so that you can write `value.into_integer()` in
|
||||
/// your code. It is a contract of this trait that if you implement
|
||||
/// `Contiguous` on your type you **must not** override this method.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// We will not panic for any correct implementation of `Contiguous`, but
|
||||
/// *may* panic if we detect an incorrect one.
|
||||
///
|
||||
/// This is undefined behavior regardless, so it could have been the nasal
|
||||
/// demons at that point anyway ;).
|
||||
#[inline]
|
||||
fn from_integer(value: Self::Int) -> Option<Self> {
|
||||
// Guard against an illegal implementation of Contiguous. Annoyingly we
|
||||
// can't rely on `transmute` to do this for us (see below), but
|
||||
// whatever, this gets compiled into nothing in release.
|
||||
assert!(size_of::<Self>() == size_of::<Self::Int>());
|
||||
if Self::MIN_VALUE <= value && value <= Self::MAX_VALUE {
|
||||
// SAFETY: We've checked their bounds (and their size, even though
|
||||
// they've sworn under the Oath Of Unsafe Rust that that already
|
||||
// matched) so this is allowed by `Contiguous`'s unsafe contract.
|
||||
//
|
||||
// So, the `transmute!`. ideally we'd use transmute here, which
|
||||
// is more obviously safe. Sadly, we can't, as these types still
|
||||
// have unspecified sizes.
|
||||
Some(unsafe { transmute!(value) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform the conversion from `C` into the underlying integral type. This
|
||||
/// mostly exists otherwise generic code would need unsafe for the `value as
|
||||
/// integer`
|
||||
///
|
||||
/// This is a trait method so that you can write `value.into_integer()` in
|
||||
/// your code. It is a contract of this trait that if you implement
|
||||
/// `Contiguous` on your type you **must not** override this method.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// We will not panic for any correct implementation of `Contiguous`, but
|
||||
/// *may* panic if we detect an incorrect one.
|
||||
///
|
||||
/// This is undefined behavior regardless, so it could have been the nasal
|
||||
/// demons at that point anyway ;).
|
||||
#[inline]
|
||||
fn into_integer(self) -> Self::Int {
|
||||
// Guard against an illegal implementation of Contiguous. Annoyingly we
|
||||
// can't rely on `transmute` to do the size check for us (see
|
||||
// `from_integer's comment`), but whatever, this gets compiled into
|
||||
// nothing in release. Note that we don't check the result of cast
|
||||
assert!(size_of::<Self>() == size_of::<Self::Int>());
|
||||
|
||||
// SAFETY: The unsafe contract requires that these have identical
|
||||
// representations, and that the range be entirely valid. Using
|
||||
// transmute! instead of transmute here is annoying, but is required
|
||||
// as `Self` and `Self::Int` have unspecified sizes still.
|
||||
unsafe { transmute!(self) }
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_contiguous {
|
||||
($($src:ty as $repr:ident in [$min:expr, $max:expr];)*) => {$(
|
||||
unsafe impl Contiguous for $src {
|
||||
type Int = $repr;
|
||||
const MAX_VALUE: $repr = $max;
|
||||
const MIN_VALUE: $repr = $min;
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
impl_contiguous! {
|
||||
bool as u8 in [0, 1];
|
||||
|
||||
u8 as u8 in [0, u8::max_value()];
|
||||
u16 as u16 in [0, u16::max_value()];
|
||||
u32 as u32 in [0, u32::max_value()];
|
||||
u64 as u64 in [0, u64::max_value()];
|
||||
u128 as u128 in [0, u128::max_value()];
|
||||
usize as usize in [0, usize::max_value()];
|
||||
|
||||
i8 as i8 in [i8::min_value(), i8::max_value()];
|
||||
i16 as i16 in [i16::min_value(), i16::max_value()];
|
||||
i32 as i32 in [i32::min_value(), i32::max_value()];
|
||||
i64 as i64 in [i64::min_value(), i64::max_value()];
|
||||
i128 as i128 in [i128::min_value(), i128::max_value()];
|
||||
isize as isize in [isize::min_value(), isize::max_value()];
|
||||
|
||||
NonZeroU8 as u8 in [1, u8::max_value()];
|
||||
NonZeroU16 as u16 in [1, u16::max_value()];
|
||||
NonZeroU32 as u32 in [1, u32::max_value()];
|
||||
NonZeroU64 as u64 in [1, u64::max_value()];
|
||||
NonZeroU128 as u128 in [1, u128::max_value()];
|
||||
NonZeroUsize as usize in [1, usize::max_value()];
|
||||
}
|
402
third_party/rust/bytemuck/src/internal.rs
vendored
Normal file
402
third_party/rust/bytemuck/src/internal.rs
vendored
Normal file
@ -0,0 +1,402 @@
|
||||
//! Internal implementation of casting functions not bound by marker traits
|
||||
//! and therefore marked as unsafe. This is used so that we don't need to
|
||||
//! duplicate the business logic contained in these functions between the
|
||||
//! versions exported in the crate root, `checked`, and `relaxed` modules.
|
||||
#![allow(unused_unsafe)]
|
||||
|
||||
use crate::PodCastError;
|
||||
use core::{marker::*, mem::*};
|
||||
|
||||
/*
|
||||
|
||||
Note(Lokathor): We've switched all of the `unwrap` to `match` because there is
|
||||
apparently a bug: https://github.com/rust-lang/rust/issues/68667
|
||||
and it doesn't seem to show up in simple godbolt examples but has been reported
|
||||
as having an impact when there's a cast mixed in with other more complicated
|
||||
code around it. Rustc/LLVM ends up missing that the `Err` can't ever happen for
|
||||
particular type combinations, and then it doesn't fully eliminated the panic
|
||||
possibility code branch.
|
||||
|
||||
*/
|
||||
|
||||
/// Immediately panics.
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
pub(crate) fn something_went_wrong<D: core::fmt::Display>(
|
||||
_src: &str, _err: D,
|
||||
) -> ! {
|
||||
// Note(Lokathor): Keeping the panic here makes the panic _formatting_ go
|
||||
// here too, which helps assembly readability and also helps keep down
|
||||
// the inline pressure.
|
||||
panic!("{src}>{err}", src = _src, err = _err);
|
||||
}
|
||||
|
||||
/// Immediately panics.
|
||||
#[cfg(target_arch = "spirv")]
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
pub(crate) fn something_went_wrong<D>(_src: &str, _err: D) -> ! {
|
||||
// Note: On the spirv targets from [rust-gpu](https://github.com/EmbarkStudios/rust-gpu)
|
||||
// panic formatting cannot be used. We we just give a generic error message
|
||||
// The chance that the panicking version of these functions will ever get
|
||||
// called on spir-v targets with invalid inputs is small, but giving a
|
||||
// simple error message is better than no error message at all.
|
||||
panic!("Called a panicing helper from bytemuck which paniced");
|
||||
}
|
||||
|
||||
/// Re-interprets `&T` as `&[u8]`.
|
||||
///
|
||||
/// Any ZST becomes an empty slice, and in that case the pointer value of that
|
||||
/// empty slice might not match the pointer value of the input reference.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn bytes_of<T: Copy>(t: &T) -> &[u8] {
|
||||
if size_of::<T>() == 0 {
|
||||
&[]
|
||||
} else {
|
||||
match try_cast_slice::<T, u8>(core::slice::from_ref(t)) {
|
||||
Ok(s) => s,
|
||||
Err(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&mut T` as `&mut [u8]`.
|
||||
///
|
||||
/// Any ZST becomes an empty slice, and in that case the pointer value of that
|
||||
/// empty slice might not match the pointer value of the input reference.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn bytes_of_mut<T: Copy>(t: &mut T) -> &mut [u8] {
|
||||
if size_of::<T>() == 0 {
|
||||
&mut []
|
||||
} else {
|
||||
match try_cast_slice_mut::<T, u8>(core::slice::from_mut(t)) {
|
||||
Ok(s) => s,
|
||||
Err(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&[u8]` as `&T`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_from_bytes`] but will panic on error.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn from_bytes<T: Copy>(s: &[u8]) -> &T {
|
||||
match try_from_bytes(s) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("from_bytes", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&mut [u8]` as `&mut T`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_from_bytes_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn from_bytes_mut<T: Copy>(s: &mut [u8]) -> &mut T {
|
||||
match try_from_bytes_mut(s) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("from_bytes_mut", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads from the bytes as if they were a `T`.
|
||||
///
|
||||
/// ## Failure
|
||||
/// * If the `bytes` length is not equal to `size_of::<T>()`.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn try_pod_read_unaligned<T: Copy>(
|
||||
bytes: &[u8],
|
||||
) -> Result<T, PodCastError> {
|
||||
if bytes.len() != size_of::<T>() {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
} else {
|
||||
Ok(unsafe { (bytes.as_ptr() as *const T).read_unaligned() })
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the slice into a `T` value.
|
||||
///
|
||||
/// ## Panics
|
||||
/// * This is like `try_pod_read_unaligned` but will panic on failure.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn pod_read_unaligned<T: Copy>(bytes: &[u8]) -> T {
|
||||
match try_pod_read_unaligned(bytes) {
|
||||
Ok(t) => t,
|
||||
Err(e) => something_went_wrong("pod_read_unaligned", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if `ptr` is aligned to an `align` memory boundary.
|
||||
///
|
||||
/// ## Panics
|
||||
/// * If `align` is not a power of two. This includes when `align` is zero.
|
||||
#[inline]
|
||||
pub(crate) fn is_aligned_to(ptr: *const (), align: usize) -> bool {
|
||||
#[cfg(feature = "align_offset")]
|
||||
{
|
||||
// This is in a way better than `ptr as usize % align == 0`,
|
||||
// because casting a pointer to an integer has the side effect that it
|
||||
// exposes the pointer's provenance, which may theoretically inhibit
|
||||
// some compiler optimizations.
|
||||
ptr.align_offset(align) == 0
|
||||
}
|
||||
#[cfg(not(feature = "align_offset"))]
|
||||
{
|
||||
((ptr as usize) % align) == 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&[u8]` as `&T`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the slice isn't aligned for the new type
|
||||
/// * If the slice's length isn’t exactly the size of the new type
|
||||
#[inline]
|
||||
pub(crate) unsafe fn try_from_bytes<T: Copy>(
|
||||
s: &[u8],
|
||||
) -> Result<&T, PodCastError> {
|
||||
if s.len() != size_of::<T>() {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
} else if !is_aligned_to(s.as_ptr() as *const (), align_of::<T>()) {
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else {
|
||||
Ok(unsafe { &*(s.as_ptr() as *const T) })
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-interprets `&mut [u8]` as `&mut T`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the slice isn't aligned for the new type
|
||||
/// * If the slice's length isn’t exactly the size of the new type
|
||||
#[inline]
|
||||
pub(crate) unsafe fn try_from_bytes_mut<T: Copy>(
|
||||
s: &mut [u8],
|
||||
) -> Result<&mut T, PodCastError> {
|
||||
if s.len() != size_of::<T>() {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
} else if !is_aligned_to(s.as_ptr() as *const (), align_of::<T>()) {
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else {
|
||||
Ok(unsafe { &mut *(s.as_mut_ptr() as *mut T) })
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `T` into `U`
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// * This is like [`try_cast`](try_cast), but will panic on a size mismatch.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn cast<A: Copy, B: Copy>(a: A) -> B {
|
||||
if size_of::<A>() == size_of::<B>() {
|
||||
unsafe { transmute!(a) }
|
||||
} else {
|
||||
something_went_wrong("cast", PodCastError::SizeMismatch)
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `&mut T` into `&mut U`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn cast_mut<A: Copy, B: Copy>(a: &mut A) -> &mut B {
|
||||
if size_of::<A>() == size_of::<B>() && align_of::<A>() >= align_of::<B>() {
|
||||
// Plz mr compiler, just notice that we can't ever hit Err in this case.
|
||||
match try_cast_mut(a) {
|
||||
Ok(b) => b,
|
||||
Err(_) => unreachable!(),
|
||||
}
|
||||
} else {
|
||||
match try_cast_mut(a) {
|
||||
Ok(b) => b,
|
||||
Err(e) => something_went_wrong("cast_mut", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `&T` into `&U`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_ref`] but will panic on error.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn cast_ref<A: Copy, B: Copy>(a: &A) -> &B {
|
||||
if size_of::<A>() == size_of::<B>() && align_of::<A>() >= align_of::<B>() {
|
||||
// Plz mr compiler, just notice that we can't ever hit Err in this case.
|
||||
match try_cast_ref(a) {
|
||||
Ok(b) => b,
|
||||
Err(_) => unreachable!(),
|
||||
}
|
||||
} else {
|
||||
match try_cast_ref(a) {
|
||||
Ok(b) => b,
|
||||
Err(e) => something_went_wrong("cast_ref", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `&[A]` into `&[B]`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_slice`] but will panic on error.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn cast_slice<A: Copy, B: Copy>(a: &[A]) -> &[B] {
|
||||
match try_cast_slice(a) {
|
||||
Ok(b) => b,
|
||||
Err(e) => something_went_wrong("cast_slice", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cast `&mut [T]` into `&mut [U]`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_slice_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn cast_slice_mut<A: Copy, B: Copy>(a: &mut [A]) -> &mut [B] {
|
||||
match try_cast_slice_mut(a) {
|
||||
Ok(b) => b,
|
||||
Err(e) => something_went_wrong("cast_slice_mut", e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to cast `T` into `U`.
|
||||
///
|
||||
/// Note that for this particular type of cast, alignment isn't a factor. The
|
||||
/// input value is semantically copied into the function and then returned to a
|
||||
/// new memory location which will have whatever the required alignment of the
|
||||
/// output type is.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the types don't have the same size this fails.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn try_cast<A: Copy, B: Copy>(
|
||||
a: A,
|
||||
) -> Result<B, PodCastError> {
|
||||
if size_of::<A>() == size_of::<B>() {
|
||||
Ok(unsafe { transmute!(a) })
|
||||
} else {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to convert a `&T` into `&U`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the reference isn't aligned in the new type
|
||||
/// * If the source type and target type aren't the same size.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn try_cast_ref<A: Copy, B: Copy>(
|
||||
a: &A,
|
||||
) -> Result<&B, PodCastError> {
|
||||
// Note(Lokathor): everything with `align_of` and `size_of` will optimize away
|
||||
// after monomorphization.
|
||||
if align_of::<B>() > align_of::<A>()
|
||||
&& !is_aligned_to(a as *const A as *const (), align_of::<B>())
|
||||
{
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else if size_of::<B>() == size_of::<A>() {
|
||||
Ok(unsafe { &*(a as *const A as *const B) })
|
||||
} else {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to convert a `&mut T` into `&mut U`.
|
||||
///
|
||||
/// As [`try_cast_ref`], but `mut`.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn try_cast_mut<A: Copy, B: Copy>(
|
||||
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>()
|
||||
&& !is_aligned_to(a as *const A as *const (), align_of::<B>())
|
||||
{
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else if size_of::<B>() == size_of::<A>() {
|
||||
Ok(unsafe { &mut *(a as *mut A as *mut B) })
|
||||
} else {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to convert `&[A]` into `&[B]` (possibly with a change in length).
|
||||
///
|
||||
/// * `input.as_ptr() as usize == output.as_ptr() as usize`
|
||||
/// * `input.len() * size_of::<A>() == output.len() * size_of::<B>()`
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the target type has a greater alignment requirement and the input slice
|
||||
/// isn't aligned.
|
||||
/// * If the target element type is a different size from the current element
|
||||
/// type, and the output slice wouldn't be a whole number of elements when
|
||||
/// accounting for the size change (eg: 3 `u16` values is 1.5 `u32` values, so
|
||||
/// that's a failure).
|
||||
/// * Similarly, you can't convert between a [ZST](https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts)
|
||||
/// and a non-ZST.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn try_cast_slice<A: Copy, B: Copy>(
|
||||
a: &[A],
|
||||
) -> Result<&[B], PodCastError> {
|
||||
// Note(Lokathor): everything with `align_of` and `size_of` will optimize away
|
||||
// after monomorphization.
|
||||
if align_of::<B>() > align_of::<A>()
|
||||
&& !is_aligned_to(a.as_ptr() as *const (), align_of::<B>())
|
||||
{
|
||||
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()) })
|
||||
} 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) })
|
||||
} else {
|
||||
Err(PodCastError::OutputSliceWouldHaveSlop)
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to convert `&mut [A]` into `&mut [B]` (possibly with a change in
|
||||
/// length).
|
||||
///
|
||||
/// As [`try_cast_slice`], but `&mut`.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn try_cast_slice_mut<A: Copy, B: Copy>(
|
||||
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>()
|
||||
&& !is_aligned_to(a.as_ptr() as *const (), align_of::<B>())
|
||||
{
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
} else if size_of::<B>() == size_of::<A>() {
|
||||
Ok(unsafe {
|
||||
core::slice::from_raw_parts_mut(a.as_mut_ptr() as *mut 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_mut_ptr() as *mut B, new_len)
|
||||
})
|
||||
} else {
|
||||
Err(PodCastError::OutputSliceWouldHaveSlop)
|
||||
}
|
||||
}
|
508
third_party/rust/bytemuck/src/lib.rs
vendored
Normal file
508
third_party/rust/bytemuck/src/lib.rs
vendored
Normal file
@ -0,0 +1,508 @@
|
||||
#![no_std]
|
||||
#![warn(missing_docs)]
|
||||
#![allow(clippy::match_like_matches_macro)]
|
||||
#![allow(clippy::uninlined_format_args)]
|
||||
#![cfg_attr(feature = "nightly_docs", feature(doc_cfg))]
|
||||
#![cfg_attr(feature = "nightly_portable_simd", feature(portable_simd))]
|
||||
#![cfg_attr(
|
||||
all(
|
||||
feature = "nightly_stdsimd",
|
||||
any(target_arch = "x86_64", target_arch = "x86")
|
||||
),
|
||||
feature(stdarch_x86_avx512)
|
||||
)]
|
||||
|
||||
//! This crate gives small utilities for casting between plain data types.
|
||||
//!
|
||||
//! ## Basics
|
||||
//!
|
||||
//! Data comes in five basic forms in Rust, so we have five basic casting
|
||||
//! functions:
|
||||
//!
|
||||
//! * `T` uses [`cast`]
|
||||
//! * `&T` uses [`cast_ref`]
|
||||
//! * `&mut T` uses [`cast_mut`]
|
||||
//! * `&[T]` uses [`cast_slice`]
|
||||
//! * `&mut [T]` uses [`cast_slice_mut`]
|
||||
//!
|
||||
//! Depending on the function, the [`NoUninit`] and/or [`AnyBitPattern`] traits
|
||||
//! are used to maintain memory safety.
|
||||
//!
|
||||
//! **Historical Note:** When the crate first started the [`Pod`] trait was used
|
||||
//! instead, and so you may hear people refer to that, but it has the strongest
|
||||
//! requirements and people eventually wanted the more fine-grained system, so
|
||||
//! here we are. All types that impl `Pod` have a blanket impl to also support
|
||||
//! `NoUninit` and `AnyBitPattern`. The traits unfortunately do not have a
|
||||
//! perfectly clean hierarchy for semver reasons.
|
||||
//!
|
||||
//! ## Failures
|
||||
//!
|
||||
//! Some casts will never fail, and other casts might fail.
|
||||
//!
|
||||
//! * `cast::<u32, f32>` always works (and [`f32::from_bits`]).
|
||||
//! * `cast_ref::<[u8; 4], u32>` might fail if the specific array reference
|
||||
//! given at runtime doesn't have alignment 4.
|
||||
//!
|
||||
//! In addition to the "normal" forms of each function, which will panic on
|
||||
//! invalid input, there's also `try_` versions which will return a `Result`.
|
||||
//!
|
||||
//! If you would like to statically ensure that a cast will work at runtime you
|
||||
//! can use the `must_cast` crate feature and the `must_` casting functions. A
|
||||
//! "must cast" that can't be statically known to be valid will cause a
|
||||
//! compilation error (and sometimes a very hard to read compilation error).
|
||||
//!
|
||||
//! ## Using Your Own Types
|
||||
//!
|
||||
//! All the functions listed above are guarded by the [`Pod`] trait, which is a
|
||||
//! sub-trait of the [`Zeroable`] trait.
|
||||
//!
|
||||
//! If you enable the crate's `derive` feature then these traits can be derived
|
||||
//! on your own types. The derive macros will perform the necessary checks on
|
||||
//! your type declaration, and trigger an error if your type does not qualify.
|
||||
//!
|
||||
//! The derive macros might not cover all edge cases, and sometimes they will
|
||||
//! error when actually everything is fine. As a last resort you can impl these
|
||||
//! traits manually. However, these traits are `unsafe`, and you should
|
||||
//! carefully read the requirements before using a manual implementation.
|
||||
//!
|
||||
//! ## Cargo Features
|
||||
//!
|
||||
//! The crate supports Rust 1.34 when no features are enabled, and so there's
|
||||
//! cargo features for thing that you might consider "obvious".
|
||||
//!
|
||||
//! The cargo features **do not** promise any particular MSRV, and they may
|
||||
//! increase their MSRV in new versions.
|
||||
//!
|
||||
//! * `derive`: Provide derive macros for the various traits.
|
||||
//! * `extern_crate_alloc`: Provide utilities for `alloc` related types such as
|
||||
//! Box and Vec.
|
||||
//! * `zeroable_maybe_uninit` and `zeroable_atomics`: Provide more [`Zeroable`]
|
||||
//! impls.
|
||||
//! * `wasm_simd` and `aarch64_simd`: Support more SIMD types.
|
||||
//! * `min_const_generics`: Provides appropriate impls for arrays of all lengths
|
||||
//! instead of just for a select list of array lengths.
|
||||
//! * `must_cast`: Provides the `must_` functions, which will compile error if
|
||||
//! the requested cast can't be statically verified.
|
||||
|
||||
#[cfg(all(target_arch = "aarch64", feature = "aarch64_simd"))]
|
||||
use core::arch::aarch64;
|
||||
#[cfg(all(target_arch = "wasm32", feature = "wasm_simd"))]
|
||||
use core::arch::wasm32;
|
||||
#[cfg(target_arch = "x86")]
|
||||
use core::arch::x86;
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
use core::arch::x86_64;
|
||||
//
|
||||
use core::{marker::*, mem::*, num::*, ptr::*};
|
||||
|
||||
// Used from macros to ensure we aren't using some locally defined name and
|
||||
// actually are referencing libcore. This also would allow pre-2018 edition
|
||||
// crates to use our macros, but I'm not sure how important that is.
|
||||
#[doc(hidden)]
|
||||
pub use ::core as __core;
|
||||
|
||||
#[cfg(not(feature = "min_const_generics"))]
|
||||
macro_rules! impl_unsafe_marker_for_array {
|
||||
( $marker:ident , $( $n:expr ),* ) => {
|
||||
$(unsafe impl<T> $marker for [T; $n] where T: $marker {})*
|
||||
}
|
||||
}
|
||||
|
||||
/// A macro to transmute between two types without requiring knowing size
|
||||
/// statically.
|
||||
macro_rules! transmute {
|
||||
($val:expr) => {
|
||||
::core::mem::transmute_copy(&::core::mem::ManuallyDrop::new($val))
|
||||
};
|
||||
}
|
||||
|
||||
/// A macro to implement marker traits for various simd types.
|
||||
/// #[allow(unused)] because the impls are only compiled on relevant platforms
|
||||
/// with relevant cargo features enabled.
|
||||
#[allow(unused)]
|
||||
macro_rules! impl_unsafe_marker_for_simd {
|
||||
($(#[cfg($cfg_predicate:meta)])? unsafe impl $trait:ident for $platform:ident :: {}) => {};
|
||||
($(#[cfg($cfg_predicate:meta)])? unsafe impl $trait:ident for $platform:ident :: { $first_type:ident $(, $types:ident)* $(,)? }) => {
|
||||
$( #[cfg($cfg_predicate)] )?
|
||||
$( #[cfg_attr(feature = "nightly_docs", doc(cfg($cfg_predicate)))] )?
|
||||
unsafe impl $trait for $platform::$first_type {}
|
||||
$( #[cfg($cfg_predicate)] )? // To prevent recursion errors if nothing is going to be expanded anyway.
|
||||
impl_unsafe_marker_for_simd!($( #[cfg($cfg_predicate)] )? unsafe impl $trait for $platform::{ $( $types ),* });
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(feature = "extern_crate_std")]
|
||||
extern crate std;
|
||||
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
extern crate alloc;
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_alloc")))]
|
||||
pub mod allocation;
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
pub use allocation::*;
|
||||
|
||||
mod anybitpattern;
|
||||
pub use anybitpattern::*;
|
||||
|
||||
pub mod checked;
|
||||
pub use checked::CheckedBitPattern;
|
||||
|
||||
mod internal;
|
||||
|
||||
mod zeroable;
|
||||
pub use zeroable::*;
|
||||
mod zeroable_in_option;
|
||||
pub use zeroable_in_option::*;
|
||||
|
||||
mod pod;
|
||||
pub use pod::*;
|
||||
mod pod_in_option;
|
||||
pub use pod_in_option::*;
|
||||
|
||||
#[cfg(feature = "must_cast")]
|
||||
mod must;
|
||||
#[cfg(feature = "must_cast")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "must_cast")))]
|
||||
pub use must::*;
|
||||
|
||||
mod no_uninit;
|
||||
pub use no_uninit::*;
|
||||
|
||||
mod contiguous;
|
||||
pub use contiguous::*;
|
||||
|
||||
mod offset_of;
|
||||
// ^ no import, the module only has a macro_rules, which are cursed and don't
|
||||
// follow normal import/export rules.
|
||||
|
||||
mod transparent;
|
||||
pub use transparent::*;
|
||||
|
||||
#[cfg(feature = "derive")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "derive")))]
|
||||
pub use bytemuck_derive::{
|
||||
AnyBitPattern, ByteEq, ByteHash, CheckedBitPattern, Contiguous, NoUninit,
|
||||
Pod, TransparentWrapper, Zeroable,
|
||||
};
|
||||
|
||||
/// The things that can go wrong when casting between [`Pod`] data forms.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum PodCastError {
|
||||
/// You tried to cast a slice to an element type with a higher alignment
|
||||
/// requirement but the slice wasn't aligned.
|
||||
TargetAlignmentGreaterAndInputNotAligned,
|
||||
/// If the element size changes then the output slice changes length
|
||||
/// accordingly. If the output slice wouldn't be a whole number of elements
|
||||
/// then the conversion fails.
|
||||
OutputSliceWouldHaveSlop,
|
||||
/// When casting a slice you can't convert between ZST elements and non-ZST
|
||||
/// elements. When casting an individual `T`, `&T`, or `&mut T` value the
|
||||
/// source size and destination size must be an exact match.
|
||||
SizeMismatch,
|
||||
/// For this type of cast the alignments must be exactly the same and they
|
||||
/// were not so now you're sad.
|
||||
///
|
||||
/// This error is generated **only** by operations that cast allocated types
|
||||
/// (such as `Box` and `Vec`), because in that case the alignment must stay
|
||||
/// exact.
|
||||
AlignmentMismatch,
|
||||
}
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
impl core::fmt::Display for PodCastError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "extern_crate_std")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_std")))]
|
||||
impl std::error::Error for PodCastError {}
|
||||
|
||||
/// Re-interprets `&T` as `&[u8]`.
|
||||
///
|
||||
/// Any ZST becomes an empty slice, and in that case the pointer value of that
|
||||
/// empty slice might not match the pointer value of the input reference.
|
||||
#[inline]
|
||||
pub fn bytes_of<T: NoUninit>(t: &T) -> &[u8] {
|
||||
unsafe { internal::bytes_of(t) }
|
||||
}
|
||||
|
||||
/// Re-interprets `&mut T` as `&mut [u8]`.
|
||||
///
|
||||
/// Any ZST becomes an empty slice, and in that case the pointer value of that
|
||||
/// empty slice might not match the pointer value of the input reference.
|
||||
#[inline]
|
||||
pub fn bytes_of_mut<T: NoUninit + AnyBitPattern>(t: &mut T) -> &mut [u8] {
|
||||
unsafe { internal::bytes_of_mut(t) }
|
||||
}
|
||||
|
||||
/// Re-interprets `&[u8]` as `&T`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is like [`try_from_bytes`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn from_bytes<T: AnyBitPattern>(s: &[u8]) -> &T {
|
||||
unsafe { internal::from_bytes(s) }
|
||||
}
|
||||
|
||||
/// Re-interprets `&mut [u8]` as `&mut T`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is like [`try_from_bytes_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn from_bytes_mut<T: NoUninit + AnyBitPattern>(s: &mut [u8]) -> &mut T {
|
||||
unsafe { internal::from_bytes_mut(s) }
|
||||
}
|
||||
|
||||
/// Reads from the bytes as if they were a `T`.
|
||||
///
|
||||
/// Unlike [`from_bytes`], the slice doesn't need to respect alignment of `T`,
|
||||
/// only sizes must match.
|
||||
///
|
||||
/// ## Failure
|
||||
/// * If the `bytes` length is not equal to `size_of::<T>()`.
|
||||
#[inline]
|
||||
pub fn try_pod_read_unaligned<T: AnyBitPattern>(
|
||||
bytes: &[u8],
|
||||
) -> Result<T, PodCastError> {
|
||||
unsafe { internal::try_pod_read_unaligned(bytes) }
|
||||
}
|
||||
|
||||
/// Reads the slice into a `T` value.
|
||||
///
|
||||
/// Unlike [`from_bytes`], the slice doesn't need to respect alignment of `T`,
|
||||
/// only sizes must match.
|
||||
///
|
||||
/// ## Panics
|
||||
/// * This is like `try_pod_read_unaligned` but will panic on failure.
|
||||
#[inline]
|
||||
pub fn pod_read_unaligned<T: AnyBitPattern>(bytes: &[u8]) -> T {
|
||||
unsafe { internal::pod_read_unaligned(bytes) }
|
||||
}
|
||||
|
||||
/// Re-interprets `&[u8]` as `&T`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the slice isn't aligned for the new type
|
||||
/// * If the slice's length isn’t exactly the size of the new type
|
||||
#[inline]
|
||||
pub fn try_from_bytes<T: AnyBitPattern>(s: &[u8]) -> Result<&T, PodCastError> {
|
||||
unsafe { internal::try_from_bytes(s) }
|
||||
}
|
||||
|
||||
/// Re-interprets `&mut [u8]` as `&mut T`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the slice isn't aligned for the new type
|
||||
/// * If the slice's length isn’t exactly the size of the new type
|
||||
#[inline]
|
||||
pub fn try_from_bytes_mut<T: NoUninit + AnyBitPattern>(
|
||||
s: &mut [u8],
|
||||
) -> Result<&mut T, PodCastError> {
|
||||
unsafe { internal::try_from_bytes_mut(s) }
|
||||
}
|
||||
|
||||
/// Cast `T` into `U`
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// * This is like [`try_cast`], but will panic on a size mismatch.
|
||||
#[inline]
|
||||
pub fn cast<A: NoUninit, B: AnyBitPattern>(a: A) -> B {
|
||||
unsafe { internal::cast(a) }
|
||||
}
|
||||
|
||||
/// Cast `&mut T` into `&mut U`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn cast_mut<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>(
|
||||
a: &mut A,
|
||||
) -> &mut B {
|
||||
unsafe { internal::cast_mut(a) }
|
||||
}
|
||||
|
||||
/// Cast `&T` into `&U`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_ref`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn cast_ref<A: NoUninit, B: AnyBitPattern>(a: &A) -> &B {
|
||||
unsafe { internal::cast_ref(a) }
|
||||
}
|
||||
|
||||
/// Cast `&[A]` into `&[B]`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_slice`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn cast_slice<A: NoUninit, B: AnyBitPattern>(a: &[A]) -> &[B] {
|
||||
unsafe { internal::cast_slice(a) }
|
||||
}
|
||||
|
||||
/// Cast `&mut [T]` into `&mut [U]`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// This is [`try_cast_slice_mut`] but will panic on error.
|
||||
#[inline]
|
||||
pub fn cast_slice_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
a: &mut [A],
|
||||
) -> &mut [B] {
|
||||
unsafe { internal::cast_slice_mut(a) }
|
||||
}
|
||||
|
||||
/// As [`align_to`](https://doc.rust-lang.org/std/primitive.slice.html#method.align_to),
|
||||
/// but safe because of the [`Pod`] bound.
|
||||
#[inline]
|
||||
pub fn pod_align_to<T: NoUninit, U: AnyBitPattern>(
|
||||
vals: &[T],
|
||||
) -> (&[T], &[U], &[T]) {
|
||||
unsafe { vals.align_to::<U>() }
|
||||
}
|
||||
|
||||
/// As [`align_to_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.align_to_mut),
|
||||
/// but safe because of the [`Pod`] bound.
|
||||
#[inline]
|
||||
pub fn pod_align_to_mut<
|
||||
T: NoUninit + AnyBitPattern,
|
||||
U: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
vals: &mut [T],
|
||||
) -> (&mut [T], &mut [U], &mut [T]) {
|
||||
unsafe { vals.align_to_mut::<U>() }
|
||||
}
|
||||
|
||||
/// Try to cast `T` into `U`.
|
||||
///
|
||||
/// Note that for this particular type of cast, alignment isn't a factor. The
|
||||
/// input value is semantically copied into the function and then returned to a
|
||||
/// new memory location which will have whatever the required alignment of the
|
||||
/// output type is.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the types don't have the same size this fails.
|
||||
#[inline]
|
||||
pub fn try_cast<A: NoUninit, B: AnyBitPattern>(
|
||||
a: A,
|
||||
) -> Result<B, PodCastError> {
|
||||
unsafe { internal::try_cast(a) }
|
||||
}
|
||||
|
||||
/// Try to convert a `&T` into `&U`.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the reference isn't aligned in the new type
|
||||
/// * If the source type and target type aren't the same size.
|
||||
#[inline]
|
||||
pub fn try_cast_ref<A: NoUninit, B: AnyBitPattern>(
|
||||
a: &A,
|
||||
) -> Result<&B, PodCastError> {
|
||||
unsafe { internal::try_cast_ref(a) }
|
||||
}
|
||||
|
||||
/// Try to convert a `&mut T` into `&mut U`.
|
||||
///
|
||||
/// As [`try_cast_ref`], but `mut`.
|
||||
#[inline]
|
||||
pub fn try_cast_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
a: &mut A,
|
||||
) -> Result<&mut B, PodCastError> {
|
||||
unsafe { internal::try_cast_mut(a) }
|
||||
}
|
||||
|
||||
/// Try to convert `&[A]` into `&[B]` (possibly with a change in length).
|
||||
///
|
||||
/// * `input.as_ptr() as usize == output.as_ptr() as usize`
|
||||
/// * `input.len() * size_of::<A>() == output.len() * size_of::<B>()`
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the target type has a greater alignment requirement and the input slice
|
||||
/// isn't aligned.
|
||||
/// * If the target element type is a different size from the current element
|
||||
/// type, and the output slice wouldn't be a whole number of elements when
|
||||
/// accounting for the size change (eg: 3 `u16` values is 1.5 `u32` values, so
|
||||
/// that's a failure).
|
||||
/// * Similarly, you can't convert between a [ZST](https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts)
|
||||
/// and a non-ZST.
|
||||
#[inline]
|
||||
pub fn try_cast_slice<A: NoUninit, B: AnyBitPattern>(
|
||||
a: &[A],
|
||||
) -> Result<&[B], PodCastError> {
|
||||
unsafe { internal::try_cast_slice(a) }
|
||||
}
|
||||
|
||||
/// Try to convert `&mut [A]` into `&mut [B]` (possibly with a change in
|
||||
/// length).
|
||||
///
|
||||
/// As [`try_cast_slice`], but `&mut`.
|
||||
#[inline]
|
||||
pub fn try_cast_slice_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
a: &mut [A],
|
||||
) -> Result<&mut [B], PodCastError> {
|
||||
unsafe { internal::try_cast_slice_mut(a) }
|
||||
}
|
||||
|
||||
/// Fill all bytes of `target` with zeroes (see [`Zeroable`]).
|
||||
///
|
||||
/// This is similar to `*target = Zeroable::zeroed()`, but guarantees that any
|
||||
/// padding bytes in `target` are zeroed as well.
|
||||
///
|
||||
/// See also [`fill_zeroes`], if you have a slice rather than a single value.
|
||||
#[inline]
|
||||
pub fn write_zeroes<T: Zeroable>(target: &mut T) {
|
||||
struct EnsureZeroWrite<T>(*mut T);
|
||||
impl<T> Drop for EnsureZeroWrite<T> {
|
||||
#[inline(always)]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
core::ptr::write_bytes(self.0, 0u8, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
let guard = EnsureZeroWrite(target);
|
||||
core::ptr::drop_in_place(guard.0);
|
||||
drop(guard);
|
||||
}
|
||||
}
|
||||
|
||||
/// Fill all bytes of `slice` with zeroes (see [`Zeroable`]).
|
||||
///
|
||||
/// This is similar to `slice.fill(Zeroable::zeroed())`, but guarantees that any
|
||||
/// padding bytes in `slice` are zeroed as well.
|
||||
///
|
||||
/// See also [`write_zeroes`], which zeroes all bytes of a single value rather
|
||||
/// than a slice.
|
||||
#[inline]
|
||||
pub fn fill_zeroes<T: Zeroable>(slice: &mut [T]) {
|
||||
if core::mem::needs_drop::<T>() {
|
||||
// If `T` needs to be dropped then we have to do this one item at a time, in
|
||||
// case one of the intermediate drops does a panic.
|
||||
slice.iter_mut().for_each(write_zeroes);
|
||||
} else {
|
||||
// Otherwise we can be really fast and just fill everthing with zeros.
|
||||
let len = core::mem::size_of_val::<[T]>(slice);
|
||||
unsafe { core::ptr::write_bytes(slice.as_mut_ptr() as *mut u8, 0u8, len) }
|
||||
}
|
||||
}
|
203
third_party/rust/bytemuck/src/must.rs
vendored
Normal file
203
third_party/rust/bytemuck/src/must.rs
vendored
Normal file
@ -0,0 +1,203 @@
|
||||
#![allow(clippy::module_name_repetitions)]
|
||||
#![allow(clippy::let_unit_value)]
|
||||
#![allow(clippy::let_underscore_untyped)]
|
||||
#![allow(clippy::ptr_as_ptr)]
|
||||
|
||||
use crate::{AnyBitPattern, NoUninit};
|
||||
use core::mem::{align_of, size_of};
|
||||
|
||||
struct Cast<A, B>((A, B));
|
||||
impl<A, B> Cast<A, B> {
|
||||
const ASSERT_ALIGN_GREATER_THAN_EQUAL: () =
|
||||
assert!(align_of::<A>() >= align_of::<B>());
|
||||
const ASSERT_SIZE_EQUAL: () = assert!(size_of::<A>() == size_of::<B>());
|
||||
const ASSERT_SIZE_MULTIPLE_OF: () = assert!(
|
||||
(size_of::<A>() == 0) == (size_of::<B>() == 0)
|
||||
&& (size_of::<A>() % size_of::<B>() == 0)
|
||||
);
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/rust-lang/miri/issues/2423.
|
||||
// Miri currently doesn't see post-monomorphization errors until runtime,
|
||||
// so `compile_fail` tests relying on post-monomorphization errors don't
|
||||
// actually fail. Instead use `should_panic` under miri as a workaround.
|
||||
#[cfg(miri)]
|
||||
macro_rules! post_mono_compile_fail_doctest {
|
||||
() => {
|
||||
"```should_panic"
|
||||
};
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
macro_rules! post_mono_compile_fail_doctest {
|
||||
() => {
|
||||
"```compile_fail,E0080"
|
||||
};
|
||||
}
|
||||
|
||||
/// Cast `A` into `B` if infalliable, or fail to compile.
|
||||
///
|
||||
/// Note that for this particular type of cast, alignment isn't a factor. The
|
||||
/// input value is semantically copied into the function and then returned to a
|
||||
/// new memory location which will have whatever the required alignment of the
|
||||
/// output type is.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the types don't have the same size this fails to compile.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// // compiles:
|
||||
/// let bytes: [u8; 2] = bytemuck::must_cast(12_u16);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// // fails to compile (size mismatch):
|
||||
/// let bytes : [u8; 3] = bytemuck::must_cast(12_u16);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn must_cast<A: NoUninit, B: AnyBitPattern>(a: A) -> B {
|
||||
let _ = Cast::<A, B>::ASSERT_SIZE_EQUAL;
|
||||
unsafe { transmute!(a) }
|
||||
}
|
||||
|
||||
/// Convert `&A` into `&B` if infalliable, or fail to compile.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the target type has a greater alignment requirement.
|
||||
/// * If the source type and target type aren't the same size.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// // compiles:
|
||||
/// let bytes: &[u8; 2] = bytemuck::must_cast_ref(&12_u16);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// // fails to compile (size mismatch):
|
||||
/// let bytes : &[u8; 3] = bytemuck::must_cast_ref(&12_u16);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// // fails to compile (alignment requirements increased):
|
||||
/// let bytes : &u16 = bytemuck::must_cast_ref(&[1u8, 2u8]);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn must_cast_ref<A: NoUninit, B: AnyBitPattern>(a: &A) -> &B {
|
||||
let _ = Cast::<A, B>::ASSERT_SIZE_EQUAL;
|
||||
let _ = Cast::<A, B>::ASSERT_ALIGN_GREATER_THAN_EQUAL;
|
||||
unsafe { &*(a as *const A as *const B) }
|
||||
}
|
||||
|
||||
/// Convert a `&mut A` into `&mut B` if infalliable, or fail to compile.
|
||||
///
|
||||
/// As [`must_cast_ref`], but `mut`.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// let mut i = 12_u16;
|
||||
/// // compiles:
|
||||
/// let bytes: &mut [u8; 2] = bytemuck::must_cast_mut(&mut i);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// # let mut bytes: &mut [u8; 2] = &mut [1, 2];
|
||||
/// // fails to compile (alignment requirements increased):
|
||||
/// let i : &mut u16 = bytemuck::must_cast_mut(bytes);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// # let mut i = 12_u16;
|
||||
/// // fails to compile (size mismatch):
|
||||
/// let bytes : &mut [u8; 3] = bytemuck::must_cast_mut(&mut i);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn must_cast_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
a: &mut A,
|
||||
) -> &mut B {
|
||||
let _ = Cast::<A, B>::ASSERT_SIZE_EQUAL;
|
||||
let _ = Cast::<A, B>::ASSERT_ALIGN_GREATER_THAN_EQUAL;
|
||||
unsafe { &mut *(a as *mut A as *mut B) }
|
||||
}
|
||||
|
||||
/// Convert `&[A]` into `&[B]` (possibly with a change in length) if
|
||||
/// infalliable, or fail to compile.
|
||||
///
|
||||
/// * `input.as_ptr() as usize == output.as_ptr() as usize`
|
||||
/// * `input.len() * size_of::<A>() == output.len() * size_of::<B>()`
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// * If the target type has a greater alignment requirement.
|
||||
/// * If the target element type doesn't evenly fit into the the current element
|
||||
/// type (eg: 3 `u16` values is 1.5 `u32` values, so that's a failure).
|
||||
/// * Similarly, you can't convert between a [ZST](https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts)
|
||||
/// and a non-ZST.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// let indicies: &[u16] = &[1, 2, 3];
|
||||
/// // compiles:
|
||||
/// let bytes: &[u8] = bytemuck::must_cast_slice(indicies);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// # let bytes : &[u8] = &[1, 0, 2, 0, 3, 0];
|
||||
/// // fails to compile (bytes.len() might not be a multiple of 2):
|
||||
/// let byte_pairs : &[[u8; 2]] = bytemuck::must_cast_slice(bytes);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// # let byte_pairs : &[[u8; 2]] = &[[1, 0], [2, 0], [3, 0]];
|
||||
/// // fails to compile (alignment requirements increased):
|
||||
/// let indicies : &[u16] = bytemuck::must_cast_slice(byte_pairs);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn must_cast_slice<A: NoUninit, B: AnyBitPattern>(a: &[A]) -> &[B] {
|
||||
let _ = Cast::<A, B>::ASSERT_SIZE_MULTIPLE_OF;
|
||||
let _ = Cast::<A, B>::ASSERT_ALIGN_GREATER_THAN_EQUAL;
|
||||
let new_len = if size_of::<A>() == size_of::<B>() {
|
||||
a.len()
|
||||
} else {
|
||||
a.len() * (size_of::<A>() / size_of::<B>())
|
||||
};
|
||||
unsafe { core::slice::from_raw_parts(a.as_ptr() as *const B, new_len) }
|
||||
}
|
||||
|
||||
/// Convert `&mut [A]` into `&mut [B]` (possibly with a change in length) if
|
||||
/// infalliable, or fail to compile.
|
||||
///
|
||||
/// As [`must_cast_slice`], but `&mut`.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// let mut indicies = [1, 2, 3];
|
||||
/// let indicies: &mut [u16] = &mut indicies;
|
||||
/// // compiles:
|
||||
/// let bytes: &mut [u8] = bytemuck::must_cast_slice_mut(indicies);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// # let mut bytes = [1, 0, 2, 0, 3, 0];
|
||||
/// # let bytes : &mut [u8] = &mut bytes[..];
|
||||
/// // fails to compile (bytes.len() might not be a multiple of 2):
|
||||
/// let byte_pairs : &mut [[u8; 2]] = bytemuck::must_cast_slice_mut(bytes);
|
||||
/// ```
|
||||
#[doc = post_mono_compile_fail_doctest!()]
|
||||
/// # let mut byte_pairs = [[1, 0], [2, 0], [3, 0]];
|
||||
/// # let byte_pairs : &mut [[u8; 2]] = &mut byte_pairs[..];
|
||||
/// // fails to compile (alignment requirements increased):
|
||||
/// let indicies : &mut [u16] = bytemuck::must_cast_slice_mut(byte_pairs);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn must_cast_slice_mut<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
a: &mut [A],
|
||||
) -> &mut [B] {
|
||||
let _ = Cast::<A, B>::ASSERT_SIZE_MULTIPLE_OF;
|
||||
let _ = Cast::<A, B>::ASSERT_ALIGN_GREATER_THAN_EQUAL;
|
||||
let new_len = if size_of::<A>() == size_of::<B>() {
|
||||
a.len()
|
||||
} else {
|
||||
a.len() * (size_of::<A>() / size_of::<B>())
|
||||
};
|
||||
unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr() as *mut B, new_len) }
|
||||
}
|
80
third_party/rust/bytemuck/src/no_uninit.rs
vendored
Normal file
80
third_party/rust/bytemuck/src/no_uninit.rs
vendored
Normal file
@ -0,0 +1,80 @@
|
||||
use crate::Pod;
|
||||
use core::num::{
|
||||
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize,
|
||||
NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
|
||||
};
|
||||
|
||||
/// Marker trait for "plain old data" types with no uninit (or padding) bytes.
|
||||
///
|
||||
/// The requirements for this is very similar to [`Pod`],
|
||||
/// except that it doesn't require that all bit patterns of the type are valid,
|
||||
/// i.e. it does not require the type to be [`Zeroable`][crate::Zeroable].
|
||||
/// This limits what you can do with a type of this kind, but also broadens the
|
||||
/// included types to things like C-style enums. Notably, you can only cast from
|
||||
/// *immutable* references to a [`NoUninit`] type into *immutable* references of
|
||||
/// any other type, no casting of mutable references or mutable references to
|
||||
/// slices etc.
|
||||
///
|
||||
/// [`Pod`] is a subset of [`NoUninit`], meaning that any `T: Pod` is also
|
||||
/// [`NoUninit`] but any `T: NoUninit` is not necessarily [`Pod`]. If possible,
|
||||
/// prefer implementing [`Pod`] directly. To get more [`Pod`]-like functionality
|
||||
/// for a type that is only [`NoUninit`], consider also implementing
|
||||
/// [`CheckedBitPattern`][crate::CheckedBitPattern].
|
||||
///
|
||||
/// # Derive
|
||||
///
|
||||
/// A `#[derive(NoUninit)]` macro is provided under the `derive` feature flag
|
||||
/// which will automatically validate the requirements of this trait and
|
||||
/// implement the trait for you for both enums and structs. This is the
|
||||
/// recommended method for implementing the trait, however it's also possible to
|
||||
/// do manually. If you implement it manually, you *must* carefully follow the
|
||||
/// below safety rules.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The same as [`Pod`] except we disregard the rule about it must
|
||||
/// allow any bit pattern (i.e. it does not need to be
|
||||
/// [`Zeroable`][crate::Zeroable]). Still, this is a quite strong guarantee
|
||||
/// about a type, so *be careful* whem implementing it manually.
|
||||
///
|
||||
/// * The type must be inhabited (eg: no
|
||||
/// [Infallible](core::convert::Infallible)).
|
||||
/// * The type must not contain any uninit (or padding) bytes, either in the
|
||||
/// middle or on the end (eg: no `#[repr(C)] struct Foo(u8, u16)`, which has
|
||||
/// padding in the middle, and also no `#[repr(C)] struct Foo(u16, u8)`, which
|
||||
/// has padding on the end).
|
||||
/// * Structs need to have all fields also be `NoUninit`.
|
||||
/// * Structs need to be `repr(C)` or `repr(transparent)`. In the case of
|
||||
/// `repr(C)`, the `packed` and `align` repr modifiers can be used as long as
|
||||
/// all other rules end up being followed.
|
||||
/// * Enums need to have an explicit `#[repr(Int)]`
|
||||
/// * Enums must have only fieldless variants
|
||||
/// * It is disallowed for types to contain pointer types, `Cell`, `UnsafeCell`,
|
||||
/// atomics, and any other forms of interior mutability.
|
||||
/// * More precisely: A shared reference to the type must allow reads, and
|
||||
/// *only* reads. RustBelt's separation logic is based on the notion that a
|
||||
/// type is allowed to define a sharing predicate, its own invariant that must
|
||||
/// hold for shared references, and this predicate is the reasoning that allow
|
||||
/// it to deal with atomic and cells etc. We require the sharing predicate to
|
||||
/// be trivial and permit only read-only access.
|
||||
/// * There's probably more, don't mess it up (I mean it).
|
||||
pub unsafe trait NoUninit: Sized + Copy + 'static {}
|
||||
|
||||
unsafe impl<T: Pod> NoUninit for T {}
|
||||
|
||||
unsafe impl NoUninit for char {}
|
||||
|
||||
unsafe impl NoUninit for bool {}
|
||||
|
||||
unsafe impl NoUninit for NonZeroU8 {}
|
||||
unsafe impl NoUninit for NonZeroI8 {}
|
||||
unsafe impl NoUninit for NonZeroU16 {}
|
||||
unsafe impl NoUninit for NonZeroI16 {}
|
||||
unsafe impl NoUninit for NonZeroU32 {}
|
||||
unsafe impl NoUninit for NonZeroI32 {}
|
||||
unsafe impl NoUninit for NonZeroU64 {}
|
||||
unsafe impl NoUninit for NonZeroI64 {}
|
||||
unsafe impl NoUninit for NonZeroU128 {}
|
||||
unsafe impl NoUninit for NonZeroI128 {}
|
||||
unsafe impl NoUninit for NonZeroUsize {}
|
||||
unsafe impl NoUninit for NonZeroIsize {}
|
135
third_party/rust/bytemuck/src/offset_of.rs
vendored
Normal file
135
third_party/rust/bytemuck/src/offset_of.rs
vendored
Normal file
@ -0,0 +1,135 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
/// Find the offset in bytes of the given `$field` of `$Type`. Requires an
|
||||
/// already initialized `$instance` value to work with.
|
||||
///
|
||||
/// This is similar to the macro from [`memoffset`](https://docs.rs/memoffset),
|
||||
/// however it uses no `unsafe` code.
|
||||
///
|
||||
/// This macro has a 3-argument and 2-argument version.
|
||||
/// * In the 3-arg version you specify an instance of the type, the type itself,
|
||||
/// and the field name.
|
||||
/// * In the 2-arg version the macro will call the [`default`](Default::default)
|
||||
/// method to make a temporary instance of the type for you.
|
||||
///
|
||||
/// The output of this macro is the byte offset of the field (as a `usize`). The
|
||||
/// calculations of the macro are fixed across the entire program, but if the
|
||||
/// type used is `repr(Rust)` then they're *not* fixed across compilations or
|
||||
/// compilers.
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// ### 3-arg Usage
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck::offset_of;
|
||||
/// // enums can't derive default, and for this example we don't pick one
|
||||
/// enum MyExampleEnum {
|
||||
/// A,
|
||||
/// B,
|
||||
/// C,
|
||||
/// }
|
||||
///
|
||||
/// // so now our struct here doesn't have Default
|
||||
/// #[repr(C)]
|
||||
/// struct MyNotDefaultType {
|
||||
/// pub counter: i32,
|
||||
/// pub some_field: MyExampleEnum,
|
||||
/// }
|
||||
///
|
||||
/// // but we provide an instance of the type and it's all good.
|
||||
/// let val = MyNotDefaultType { counter: 5, some_field: MyExampleEnum::A };
|
||||
/// assert_eq!(offset_of!(val, MyNotDefaultType, some_field), 4);
|
||||
/// ```
|
||||
///
|
||||
/// ### 2-arg Usage
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck::offset_of;
|
||||
/// #[derive(Default)]
|
||||
/// #[repr(C)]
|
||||
/// struct Vertex {
|
||||
/// pub loc: [f32; 3],
|
||||
/// pub color: [f32; 3],
|
||||
/// }
|
||||
/// // if the type impls Default the macro can make its own default instance.
|
||||
/// assert_eq!(offset_of!(Vertex, loc), 0);
|
||||
/// assert_eq!(offset_of!(Vertex, color), 12);
|
||||
/// ```
|
||||
///
|
||||
/// # Usage with `#[repr(packed)]` structs
|
||||
///
|
||||
/// Attempting to compute the offset of a `#[repr(packed)]` struct with
|
||||
/// `bytemuck::offset_of!` requires an `unsafe` block. We hope to relax this in
|
||||
/// the future, but currently it is required to work around a soundness hole in
|
||||
/// Rust (See [rust-lang/rust#27060]).
|
||||
///
|
||||
/// [rust-lang/rust#27060]: https://github.com/rust-lang/rust/issues/27060
|
||||
///
|
||||
/// <p style="background:rgba(255,181,77,0.16);padding:0.75em;">
|
||||
/// <strong>Warning:</strong> This is only true for versions of bytemuck >
|
||||
/// 1.4.0. Previous versions of
|
||||
/// <code style="background:rgba(41,24,0,0.1);">bytemuck::offset_of!</code>
|
||||
/// will only emit a warning when used on the field of a packed struct in safe
|
||||
/// code, which can lead to unsoundness.
|
||||
/// </p>
|
||||
///
|
||||
/// For example, the following will fail to compile:
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// #[repr(C, packed)]
|
||||
/// #[derive(Default)]
|
||||
/// struct Example {
|
||||
/// field: u32,
|
||||
/// }
|
||||
/// // Doesn't compile:
|
||||
/// let _offset = bytemuck::offset_of!(Example, field);
|
||||
/// ```
|
||||
///
|
||||
/// While the error message this generates will mention the
|
||||
/// `safe_packed_borrows` lint, the macro will still fail to compile even if
|
||||
/// that lint is `#[allow]`ed:
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// # #[repr(C, packed)] #[derive(Default)] struct Example { field: u32 }
|
||||
/// // Still doesn't compile:
|
||||
/// #[allow(safe_packed_borrows)]
|
||||
/// {
|
||||
/// let _offset = bytemuck::offset_of!(Example, field);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This *can* be worked around by using `unsafe`, but it is only sound to do so
|
||||
/// if you can guarantee that taking a reference to the field is sound.
|
||||
///
|
||||
/// In practice, this means it only works for fields of align(1) types, or if
|
||||
/// you know the field's offset in advance (defeating the point of `offset_of`)
|
||||
/// and can prove that the struct's alignment and the field's offset are enough
|
||||
/// to prove the field's alignment.
|
||||
///
|
||||
/// Once the `raw_ref` macros are available, a future version of this crate will
|
||||
/// use them to lift the limitations of packed structs. For the duration of the
|
||||
/// `1.x` version of this crate that will be behind an on-by-default cargo
|
||||
/// feature (to maintain minimum rust version support).
|
||||
#[macro_export]
|
||||
macro_rules! offset_of {
|
||||
($instance:expr, $Type:path, $field:tt) => {{
|
||||
#[forbid(safe_packed_borrows)]
|
||||
{
|
||||
// This helps us guard against field access going through a Deref impl.
|
||||
#[allow(clippy::unneeded_field_pattern)]
|
||||
let $Type { $field: _, .. };
|
||||
let reference: &$Type = &$instance;
|
||||
let address = reference as *const _ as usize;
|
||||
let field_pointer = &reference.$field as *const _ as usize;
|
||||
// These asserts/unwraps are compiled away at release, and defend against
|
||||
// the case where somehow a deref impl is still invoked.
|
||||
let result = field_pointer.checked_sub(address).unwrap();
|
||||
assert!(result <= $crate::__core::mem::size_of::<$Type>());
|
||||
result
|
||||
}
|
||||
}};
|
||||
($Type:path, $field:tt) => {{
|
||||
$crate::offset_of!(<$Type as Default>::default(), $Type, $field)
|
||||
}};
|
||||
}
|
165
third_party/rust/bytemuck/src/pod.rs
vendored
Normal file
165
third_party/rust/bytemuck/src/pod.rs
vendored
Normal file
@ -0,0 +1,165 @@
|
||||
use super::*;
|
||||
|
||||
/// Marker trait for "plain old data".
|
||||
///
|
||||
/// The point of this trait is that once something is marked "plain old data"
|
||||
/// you can really go to town with the bit fiddling and bit casting. Therefore,
|
||||
/// it's a relatively strong claim to make about a type. Do not add this to your
|
||||
/// type casually.
|
||||
///
|
||||
/// **Reminder:** The results of casting around bytes between data types are
|
||||
/// _endian dependant_. Little-endian machines are the most common, but
|
||||
/// big-endian machines do exist (and big-endian is also used for "network
|
||||
/// order" bytes).
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// * The type must be inhabited (eg: no
|
||||
/// [Infallible](core::convert::Infallible)).
|
||||
/// * The type must allow any bit pattern (eg: no `bool` or `char`, which have
|
||||
/// illegal bit patterns).
|
||||
/// * The type must not contain any uninit (or padding) bytes, either in the
|
||||
/// middle or on the end (eg: no `#[repr(C)] struct Foo(u8, u16)`, which has
|
||||
/// padding in the middle, and also no `#[repr(C)] struct Foo(u16, u8)`, which
|
||||
/// has padding on the end).
|
||||
/// * The type needs to have all fields also be `Pod`.
|
||||
/// * The type needs to be `repr(C)` or `repr(transparent)`. In the case of
|
||||
/// `repr(C)`, the `packed` and `align` repr modifiers can be used as long as
|
||||
/// all other rules end up being followed.
|
||||
/// * It is disallowed for types to contain pointer types, `Cell`, `UnsafeCell`,
|
||||
/// atomics, and any other forms of interior mutability.
|
||||
/// * More precisely: A shared reference to the type must allow reads, and
|
||||
/// *only* reads. RustBelt's separation logic is based on the notion that a
|
||||
/// type is allowed to define a sharing predicate, its own invariant that must
|
||||
/// hold for shared references, and this predicate is the reasoning that allow
|
||||
/// it to deal with atomic and cells etc. We require the sharing predicate to
|
||||
/// be trivial and permit only read-only access.
|
||||
pub unsafe trait Pod: Zeroable + Copy + 'static {}
|
||||
|
||||
unsafe impl Pod for () {}
|
||||
unsafe impl Pod for u8 {}
|
||||
unsafe impl Pod for i8 {}
|
||||
unsafe impl Pod for u16 {}
|
||||
unsafe impl Pod for i16 {}
|
||||
unsafe impl Pod for u32 {}
|
||||
unsafe impl Pod for i32 {}
|
||||
unsafe impl Pod for u64 {}
|
||||
unsafe impl Pod for i64 {}
|
||||
unsafe impl Pod for usize {}
|
||||
unsafe impl Pod for isize {}
|
||||
unsafe impl Pod for u128 {}
|
||||
unsafe impl Pod for i128 {}
|
||||
unsafe impl Pod for f32 {}
|
||||
unsafe impl Pod for f64 {}
|
||||
unsafe impl<T: Pod> Pod for Wrapping<T> {}
|
||||
|
||||
#[cfg(feature = "unsound_ptr_pod_impl")]
|
||||
#[cfg_attr(
|
||||
feature = "nightly_docs",
|
||||
doc(cfg(feature = "unsound_ptr_pod_impl"))
|
||||
)]
|
||||
unsafe impl<T: 'static> Pod for *mut T {}
|
||||
#[cfg(feature = "unsound_ptr_pod_impl")]
|
||||
#[cfg_attr(
|
||||
feature = "nightly_docs",
|
||||
doc(cfg(feature = "unsound_ptr_pod_impl"))
|
||||
)]
|
||||
unsafe impl<T: 'static> Pod for *const T {}
|
||||
#[cfg(feature = "unsound_ptr_pod_impl")]
|
||||
#[cfg_attr(
|
||||
feature = "nightly_docs",
|
||||
doc(cfg(feature = "unsound_ptr_pod_impl"))
|
||||
)]
|
||||
unsafe impl<T: 'static> PodInOption for NonNull<T> {}
|
||||
|
||||
unsafe impl<T: ?Sized + 'static> Pod for PhantomData<T> {}
|
||||
unsafe impl Pod for PhantomPinned {}
|
||||
unsafe impl<T: Pod> Pod for ManuallyDrop<T> {}
|
||||
|
||||
// Note(Lokathor): MaybeUninit can NEVER be Pod.
|
||||
|
||||
#[cfg(feature = "min_const_generics")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "min_const_generics")))]
|
||||
unsafe impl<T, const N: usize> Pod for [T; N] where T: Pod {}
|
||||
|
||||
#[cfg(not(feature = "min_const_generics"))]
|
||||
impl_unsafe_marker_for_array!(
|
||||
Pod, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256,
|
||||
512, 1024, 2048, 4096
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(all(target_arch = "wasm32", feature = "wasm_simd"))]
|
||||
unsafe impl Pod for wasm32::{v128}
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(all(target_arch = "aarch64", feature = "aarch64_simd"))]
|
||||
unsafe impl Pod for aarch64::{
|
||||
float32x2_t, float32x2x2_t, float32x2x3_t, float32x2x4_t, float32x4_t,
|
||||
float32x4x2_t, float32x4x3_t, float32x4x4_t, float64x1_t, float64x1x2_t,
|
||||
float64x1x3_t, float64x1x4_t, float64x2_t, float64x2x2_t, float64x2x3_t,
|
||||
float64x2x4_t, int16x4_t, int16x4x2_t, int16x4x3_t, int16x4x4_t, int16x8_t,
|
||||
int16x8x2_t, int16x8x3_t, int16x8x4_t, int32x2_t, int32x2x2_t, int32x2x3_t,
|
||||
int32x2x4_t, int32x4_t, int32x4x2_t, int32x4x3_t, int32x4x4_t, int64x1_t,
|
||||
int64x1x2_t, int64x1x3_t, int64x1x4_t, int64x2_t, int64x2x2_t, int64x2x3_t,
|
||||
int64x2x4_t, int8x16_t, int8x16x2_t, int8x16x3_t, int8x16x4_t, int8x8_t,
|
||||
int8x8x2_t, int8x8x3_t, int8x8x4_t, poly16x4_t, poly16x4x2_t, poly16x4x3_t,
|
||||
poly16x4x4_t, poly16x8_t, poly16x8x2_t, poly16x8x3_t, poly16x8x4_t,
|
||||
poly64x1_t, poly64x1x2_t, poly64x1x3_t, poly64x1x4_t, poly64x2_t,
|
||||
poly64x2x2_t, poly64x2x3_t, poly64x2x4_t, poly8x16_t, poly8x16x2_t,
|
||||
poly8x16x3_t, poly8x16x4_t, poly8x8_t, poly8x8x2_t, poly8x8x3_t, poly8x8x4_t,
|
||||
uint16x4_t, uint16x4x2_t, uint16x4x3_t, uint16x4x4_t, uint16x8_t,
|
||||
uint16x8x2_t, uint16x8x3_t, uint16x8x4_t, uint32x2_t, uint32x2x2_t,
|
||||
uint32x2x3_t, uint32x2x4_t, uint32x4_t, uint32x4x2_t, uint32x4x3_t,
|
||||
uint32x4x4_t, uint64x1_t, uint64x1x2_t, uint64x1x3_t, uint64x1x4_t,
|
||||
uint64x2_t, uint64x2x2_t, uint64x2x3_t, uint64x2x4_t, uint8x16_t,
|
||||
uint8x16x2_t, uint8x16x3_t, uint8x16x4_t, uint8x8_t, uint8x8x2_t,
|
||||
uint8x8x3_t, uint8x8x4_t,
|
||||
}
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(target_arch = "x86")]
|
||||
unsafe impl Pod for x86::{
|
||||
__m128i, __m128, __m128d,
|
||||
__m256i, __m256, __m256d,
|
||||
}
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
unsafe impl Pod for x86_64::{
|
||||
__m128i, __m128, __m128d,
|
||||
__m256i, __m256, __m256d,
|
||||
}
|
||||
);
|
||||
|
||||
#[cfg(feature = "nightly_portable_simd")]
|
||||
#[cfg_attr(
|
||||
feature = "nightly_docs",
|
||||
doc(cfg(feature = "nightly_portable_simd"))
|
||||
)]
|
||||
unsafe impl<T, const N: usize> Pod for core::simd::Simd<T, N>
|
||||
where
|
||||
T: core::simd::SimdElement + Pod,
|
||||
core::simd::LaneCount<N>: core::simd::SupportedLaneCount,
|
||||
{
|
||||
}
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(all(target_arch = "x86", feature = "nightly_stdsimd"))]
|
||||
unsafe impl Pod for x86::{
|
||||
__m128bh, __m256bh, __m512,
|
||||
__m512bh, __m512d, __m512i,
|
||||
}
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(all(target_arch = "x86_64", feature = "nightly_stdsimd"))]
|
||||
unsafe impl Pod for x86_64::{
|
||||
__m128bh, __m256bh, __m512,
|
||||
__m512bh, __m512d, __m512i,
|
||||
}
|
||||
);
|
27
third_party/rust/bytemuck/src/pod_in_option.rs
vendored
Normal file
27
third_party/rust/bytemuck/src/pod_in_option.rs
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
use super::*;
|
||||
|
||||
// Note(Lokathor): This is the neat part!!
|
||||
unsafe impl<T: PodInOption> Pod for Option<T> {}
|
||||
|
||||
/// Trait for types which are [Pod](Pod) when wrapped in
|
||||
/// [Option](core::option::Option).
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// * `Option<T>` must uphold the same invariants as [Pod](Pod).
|
||||
/// * **Reminder:** pointers are **not** pod! **Do not** mix this trait with a
|
||||
/// newtype over [NonNull](core::ptr::NonNull).
|
||||
pub unsafe trait PodInOption: ZeroableInOption + Copy + 'static {}
|
||||
|
||||
unsafe impl PodInOption for NonZeroI8 {}
|
||||
unsafe impl PodInOption for NonZeroI16 {}
|
||||
unsafe impl PodInOption for NonZeroI32 {}
|
||||
unsafe impl PodInOption for NonZeroI64 {}
|
||||
unsafe impl PodInOption for NonZeroI128 {}
|
||||
unsafe impl PodInOption for NonZeroIsize {}
|
||||
unsafe impl PodInOption for NonZeroU8 {}
|
||||
unsafe impl PodInOption for NonZeroU16 {}
|
||||
unsafe impl PodInOption for NonZeroU32 {}
|
||||
unsafe impl PodInOption for NonZeroU64 {}
|
||||
unsafe impl PodInOption for NonZeroU128 {}
|
||||
unsafe impl PodInOption for NonZeroUsize {}
|
288
third_party/rust/bytemuck/src/transparent.rs
vendored
Normal file
288
third_party/rust/bytemuck/src/transparent.rs
vendored
Normal file
@ -0,0 +1,288 @@
|
||||
use super::*;
|
||||
|
||||
/// A trait which indicates that a type is a `#[repr(transparent)]` wrapper
|
||||
/// around the `Inner` value.
|
||||
///
|
||||
/// This allows safely copy transmuting between the `Inner` type and the
|
||||
/// `TransparentWrapper` type. Functions like `wrap_{}` convert from the inner
|
||||
/// type to the wrapper type and `peel_{}` functions do the inverse conversion
|
||||
/// from the wrapper type to the inner type. We deliberately do not call the
|
||||
/// wrapper-removing methods "unwrap" because at this point that word is too
|
||||
/// strongly tied to the Option/ Result methods.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The safety contract of `TransparentWrapper` is relatively simple:
|
||||
///
|
||||
/// For a given `Wrapper` which implements `TransparentWrapper<Inner>`:
|
||||
///
|
||||
/// 1. `Wrapper` must be a wrapper around `Inner` with an identical data
|
||||
/// representations. This either means that it must be a
|
||||
/// `#[repr(transparent)]` struct which contains a either a field of type
|
||||
/// `Inner` (or a field of some other transparent wrapper for `Inner`) as
|
||||
/// the only non-ZST field.
|
||||
///
|
||||
/// 2. Any fields *other* than the `Inner` field must be trivially constructable
|
||||
/// ZSTs, for example `PhantomData`, `PhantomPinned`, etc. (When deriving
|
||||
/// `TransparentWrapper` on a type with ZST fields, the ZST fields must be
|
||||
/// [`Zeroable`]).
|
||||
///
|
||||
/// 3. The `Wrapper` may not impose additional alignment requirements over
|
||||
/// `Inner`.
|
||||
/// - Note: this is currently guaranteed by `repr(transparent)`, but there
|
||||
/// have been discussions of lifting it, so it's stated here explicitly.
|
||||
///
|
||||
/// 4. All functions on `TransparentWrapper` **may not** be overridden.
|
||||
///
|
||||
/// ## Caveats
|
||||
///
|
||||
/// If the wrapper imposes additional constraints upon the inner type which are
|
||||
/// required for safety, it's responsible for ensuring those still hold -- this
|
||||
/// generally requires preventing access to instances of the inner type, as
|
||||
/// implementing `TransparentWrapper<U> for T` means anybody can call
|
||||
/// `T::cast_ref(any_instance_of_u)`.
|
||||
///
|
||||
/// For example, it would be invalid to implement TransparentWrapper for `str`
|
||||
/// to implement `TransparentWrapper` around `[u8]` because of this.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ## Basic
|
||||
///
|
||||
/// ```
|
||||
/// use bytemuck::TransparentWrapper;
|
||||
/// # #[derive(Default)]
|
||||
/// # struct SomeStruct(u32);
|
||||
///
|
||||
/// #[repr(transparent)]
|
||||
/// struct MyWrapper(SomeStruct);
|
||||
///
|
||||
/// unsafe impl TransparentWrapper<SomeStruct> for MyWrapper {}
|
||||
///
|
||||
/// // interpret a reference to &SomeStruct as a &MyWrapper
|
||||
/// let thing = SomeStruct::default();
|
||||
/// let inner_ref: &MyWrapper = MyWrapper::wrap_ref(&thing);
|
||||
///
|
||||
/// // Works with &mut too.
|
||||
/// let mut mut_thing = SomeStruct::default();
|
||||
/// let inner_mut: &mut MyWrapper = MyWrapper::wrap_mut(&mut mut_thing);
|
||||
///
|
||||
/// # let _ = (inner_ref, inner_mut); // silence warnings
|
||||
/// ```
|
||||
///
|
||||
/// ## Use with dynamically sized types
|
||||
///
|
||||
/// ```
|
||||
/// use bytemuck::TransparentWrapper;
|
||||
///
|
||||
/// #[repr(transparent)]
|
||||
/// struct Slice<T>([T]);
|
||||
///
|
||||
/// unsafe impl<T> TransparentWrapper<[T]> for Slice<T> {}
|
||||
///
|
||||
/// let s = Slice::wrap_ref(&[1u32, 2, 3]);
|
||||
/// assert_eq!(&s.0, &[1, 2, 3]);
|
||||
///
|
||||
/// let mut buf = [1, 2, 3u8];
|
||||
/// let sm = Slice::wrap_mut(&mut buf);
|
||||
/// ```
|
||||
///
|
||||
/// ## Deriving
|
||||
///
|
||||
/// When deriving, the non-wrapped fields must uphold all the normal requirements,
|
||||
/// and must also be `Zeroable`.
|
||||
///
|
||||
#[cfg_attr(feature = "derive", doc = "```")]
|
||||
#[cfg_attr(
|
||||
not(feature = "derive"),
|
||||
doc = "```ignore
|
||||
// This example requires the `derive` feature."
|
||||
)]
|
||||
/// use bytemuck::TransparentWrapper;
|
||||
/// use std::marker::PhantomData;
|
||||
///
|
||||
/// #[derive(TransparentWrapper)]
|
||||
/// #[repr(transparent)]
|
||||
/// #[transparent(usize)]
|
||||
/// struct Wrapper<T: ?Sized>(usize, PhantomData<T>); // PhantomData<T> implements Zeroable for all T
|
||||
/// ```
|
||||
///
|
||||
/// Here, an error will occur, because `MyZst` does not implement `Zeroable`.
|
||||
///
|
||||
#[cfg_attr(feature = "derive", doc = "```compile_fail")]
|
||||
#[cfg_attr(
|
||||
not(feature = "derive"),
|
||||
doc = "```ignore
|
||||
// This example requires the `derive` feature."
|
||||
)]
|
||||
/// use bytemuck::TransparentWrapper;
|
||||
/// struct MyZst;
|
||||
///
|
||||
/// #[derive(TransparentWrapper)]
|
||||
/// #[repr(transparent)]
|
||||
/// #[transparent(usize)]
|
||||
/// struct Wrapper(usize, MyZst); // MyZst does not implement Zeroable
|
||||
/// ```
|
||||
pub unsafe trait TransparentWrapper<Inner: ?Sized> {
|
||||
/// Convert the inner type into the wrapper type.
|
||||
#[inline]
|
||||
fn wrap(s: Inner) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
Inner: Sized,
|
||||
{
|
||||
// SAFETY: The unsafe contract requires that `Self` and `Inner` have
|
||||
// identical representations.
|
||||
unsafe { transmute!(s) }
|
||||
}
|
||||
|
||||
/// Convert a reference to the inner type into a reference to the wrapper
|
||||
/// type.
|
||||
#[inline]
|
||||
fn wrap_ref(s: &Inner) -> &Self {
|
||||
unsafe {
|
||||
assert!(size_of::<*const Inner>() == size_of::<*const Self>());
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the sizes are unspecified.
|
||||
//
|
||||
// SAFETY: The unsafe contract requires that these two have
|
||||
// identical representations.
|
||||
let inner_ptr = s as *const Inner;
|
||||
let wrapper_ptr: *const Self = transmute!(inner_ptr);
|
||||
&*wrapper_ptr
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a mutable reference to the inner type into a mutable reference to
|
||||
/// the wrapper type.
|
||||
#[inline]
|
||||
fn wrap_mut(s: &mut Inner) -> &mut Self {
|
||||
unsafe {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the sizes are unspecified.
|
||||
//
|
||||
// SAFETY: The unsafe contract requires that these two have
|
||||
// identical representations.
|
||||
let inner_ptr = s as *mut Inner;
|
||||
let wrapper_ptr: *mut Self = transmute!(inner_ptr);
|
||||
&mut *wrapper_ptr
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a slice to the inner type into a slice to the wrapper type.
|
||||
#[inline]
|
||||
fn wrap_slice(s: &[Inner]) -> &[Self]
|
||||
where
|
||||
Self: Sized,
|
||||
Inner: Sized,
|
||||
{
|
||||
unsafe {
|
||||
assert!(size_of::<*const Inner>() == size_of::<*const Self>());
|
||||
assert!(align_of::<*const Inner>() == align_of::<*const Self>());
|
||||
// SAFETY: The unsafe contract requires that these two have
|
||||
// identical representations (size and alignment).
|
||||
core::slice::from_raw_parts(s.as_ptr() as *const Self, s.len())
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a mutable slice to the inner type into a mutable slice to the
|
||||
/// wrapper type.
|
||||
#[inline]
|
||||
fn wrap_slice_mut(s: &mut [Inner]) -> &mut [Self]
|
||||
where
|
||||
Self: Sized,
|
||||
Inner: Sized,
|
||||
{
|
||||
unsafe {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
assert!(align_of::<*mut Inner>() == align_of::<*mut Self>());
|
||||
// SAFETY: The unsafe contract requires that these two have
|
||||
// identical representations (size and alignment).
|
||||
core::slice::from_raw_parts_mut(s.as_mut_ptr() as *mut Self, s.len())
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert the wrapper type into the inner type.
|
||||
#[inline]
|
||||
fn peel(s: Self) -> Inner
|
||||
where
|
||||
Self: Sized,
|
||||
Inner: Sized,
|
||||
{
|
||||
unsafe { transmute!(s) }
|
||||
}
|
||||
|
||||
/// Convert a reference to the wrapper type into a reference to the inner
|
||||
/// type.
|
||||
#[inline]
|
||||
fn peel_ref(s: &Self) -> &Inner {
|
||||
unsafe {
|
||||
assert!(size_of::<*const Inner>() == size_of::<*const Self>());
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the sizes are unspecified.
|
||||
//
|
||||
// SAFETY: The unsafe contract requires that these two have
|
||||
// identical representations.
|
||||
let wrapper_ptr = s as *const Self;
|
||||
let inner_ptr: *const Inner = transmute!(wrapper_ptr);
|
||||
&*inner_ptr
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a mutable reference to the wrapper type into a mutable reference
|
||||
/// to the inner type.
|
||||
#[inline]
|
||||
fn peel_mut(s: &mut Self) -> &mut Inner {
|
||||
unsafe {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
// A pointer cast doesn't work here because rustc can't tell that
|
||||
// the vtables match (because of the `?Sized` restriction relaxation).
|
||||
// A `transmute` doesn't work because the sizes are unspecified.
|
||||
//
|
||||
// SAFETY: The unsafe contract requires that these two have
|
||||
// identical representations.
|
||||
let wrapper_ptr = s as *mut Self;
|
||||
let inner_ptr: *mut Inner = transmute!(wrapper_ptr);
|
||||
&mut *inner_ptr
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a slice to the wrapped type into a slice to the inner type.
|
||||
#[inline]
|
||||
fn peel_slice(s: &[Self]) -> &[Inner]
|
||||
where
|
||||
Self: Sized,
|
||||
Inner: Sized,
|
||||
{
|
||||
unsafe {
|
||||
assert!(size_of::<*const Inner>() == size_of::<*const Self>());
|
||||
assert!(align_of::<*const Inner>() == align_of::<*const Self>());
|
||||
// SAFETY: The unsafe contract requires that these two have
|
||||
// identical representations (size and alignment).
|
||||
core::slice::from_raw_parts(s.as_ptr() as *const Inner, s.len())
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a mutable slice to the wrapped type into a mutable slice to the
|
||||
/// inner type.
|
||||
#[inline]
|
||||
fn peel_slice_mut(s: &mut [Self]) -> &mut [Inner]
|
||||
where
|
||||
Self: Sized,
|
||||
Inner: Sized,
|
||||
{
|
||||
unsafe {
|
||||
assert!(size_of::<*mut Inner>() == size_of::<*mut Self>());
|
||||
assert!(align_of::<*mut Inner>() == align_of::<*mut Self>());
|
||||
// SAFETY: The unsafe contract requires that these two have
|
||||
// identical representations (size and alignment).
|
||||
core::slice::from_raw_parts_mut(s.as_mut_ptr() as *mut Inner, s.len())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> TransparentWrapper<T> for core::num::Wrapping<T> {}
|
245
third_party/rust/bytemuck/src/zeroable.rs
vendored
Normal file
245
third_party/rust/bytemuck/src/zeroable.rs
vendored
Normal file
@ -0,0 +1,245 @@
|
||||
use super::*;
|
||||
|
||||
/// Trait for types that can be safely created with
|
||||
/// [`zeroed`](core::mem::zeroed).
|
||||
///
|
||||
/// An all-zeroes value may or may not be the same value as the
|
||||
/// [Default](core::default::Default) value of the type.
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// * Your type must be inhabited (eg: no
|
||||
/// [Infallible](core::convert::Infallible)).
|
||||
/// * Your type must be allowed to be an "all zeroes" bit pattern (eg: no
|
||||
/// [`NonNull<T>`](core::ptr::NonNull)).
|
||||
///
|
||||
/// ## Features
|
||||
///
|
||||
/// Some `impl`s are feature gated due to the MSRV policy:
|
||||
///
|
||||
/// * `MaybeUninit<T>` was not available in 1.34.0, but is available under the
|
||||
/// `zeroable_maybe_uninit` feature flag.
|
||||
/// * `Atomic*` types require Rust 1.60.0 or later to work on certain platforms,
|
||||
/// but is available under the `zeroable_atomics` feature flag.
|
||||
/// * `[T; N]` for arbitrary `N` requires the `min_const_generics` feature flag.
|
||||
pub unsafe trait Zeroable: Sized {
|
||||
/// Calls [`zeroed`](core::mem::zeroed).
|
||||
///
|
||||
/// This is a trait method so that you can write `MyType::zeroed()` in your
|
||||
/// code. It is a contract of this trait that if you implement it on your type
|
||||
/// you **must not** override this method.
|
||||
#[inline]
|
||||
fn zeroed() -> Self {
|
||||
unsafe { core::mem::zeroed() }
|
||||
}
|
||||
}
|
||||
unsafe impl Zeroable for () {}
|
||||
unsafe impl Zeroable for bool {}
|
||||
unsafe impl Zeroable for char {}
|
||||
unsafe impl Zeroable for u8 {}
|
||||
unsafe impl Zeroable for i8 {}
|
||||
unsafe impl Zeroable for u16 {}
|
||||
unsafe impl Zeroable for i16 {}
|
||||
unsafe impl Zeroable for u32 {}
|
||||
unsafe impl Zeroable for i32 {}
|
||||
unsafe impl Zeroable for u64 {}
|
||||
unsafe impl Zeroable for i64 {}
|
||||
unsafe impl Zeroable for usize {}
|
||||
unsafe impl Zeroable for isize {}
|
||||
unsafe impl Zeroable for u128 {}
|
||||
unsafe impl Zeroable for i128 {}
|
||||
unsafe impl Zeroable for f32 {}
|
||||
unsafe impl Zeroable for f64 {}
|
||||
unsafe impl<T: Zeroable> Zeroable for Wrapping<T> {}
|
||||
unsafe impl<T: Zeroable> Zeroable for core::cmp::Reverse<T> {}
|
||||
|
||||
// Note: we can't implement this for all `T: ?Sized` types because it would
|
||||
// create NULL pointers for vtables.
|
||||
// Maybe one day this could be changed to be implemented for
|
||||
// `T: ?Sized where <T as core::ptr::Pointee>::Metadata: Zeroable`.
|
||||
unsafe impl<T> Zeroable for *mut T {}
|
||||
unsafe impl<T> Zeroable for *const T {}
|
||||
unsafe impl<T> Zeroable for *mut [T] {}
|
||||
unsafe impl<T> Zeroable for *const [T] {}
|
||||
unsafe impl Zeroable for *mut str {}
|
||||
unsafe impl Zeroable for *const str {}
|
||||
|
||||
unsafe impl<T: ?Sized> Zeroable for PhantomData<T> {}
|
||||
unsafe impl Zeroable for PhantomPinned {}
|
||||
unsafe impl<T: Zeroable> Zeroable for ManuallyDrop<T> {}
|
||||
unsafe impl<T: Zeroable> Zeroable for core::cell::UnsafeCell<T> {}
|
||||
unsafe impl<T: Zeroable> Zeroable for core::cell::Cell<T> {}
|
||||
|
||||
#[cfg(feature = "zeroable_atomics")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "zeroable_atomics")))]
|
||||
mod atomic_impls {
|
||||
use super::Zeroable;
|
||||
|
||||
#[cfg(target_has_atomic = "8")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicBool {}
|
||||
#[cfg(target_has_atomic = "8")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicU8 {}
|
||||
#[cfg(target_has_atomic = "8")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicI8 {}
|
||||
|
||||
#[cfg(target_has_atomic = "16")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicU16 {}
|
||||
#[cfg(target_has_atomic = "16")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicI16 {}
|
||||
|
||||
#[cfg(target_has_atomic = "32")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicU32 {}
|
||||
#[cfg(target_has_atomic = "32")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicI32 {}
|
||||
|
||||
#[cfg(target_has_atomic = "64")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicU64 {}
|
||||
#[cfg(target_has_atomic = "64")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicI64 {}
|
||||
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicUsize {}
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
unsafe impl Zeroable for core::sync::atomic::AtomicIsize {}
|
||||
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
unsafe impl<T> Zeroable for core::sync::atomic::AtomicPtr<T> {}
|
||||
}
|
||||
|
||||
#[cfg(feature = "zeroable_maybe_uninit")]
|
||||
#[cfg_attr(
|
||||
feature = "nightly_docs",
|
||||
doc(cfg(feature = "zeroable_maybe_uninit"))
|
||||
)]
|
||||
unsafe impl<T> Zeroable for core::mem::MaybeUninit<T> {}
|
||||
|
||||
unsafe impl<A: Zeroable> Zeroable for (A,) {}
|
||||
unsafe impl<A: Zeroable, B: Zeroable> Zeroable for (A, B) {}
|
||||
unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable> Zeroable for (A, B, C) {}
|
||||
unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable, D: Zeroable> Zeroable
|
||||
for (A, B, C, D)
|
||||
{
|
||||
}
|
||||
unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable, D: Zeroable, E: Zeroable>
|
||||
Zeroable for (A, B, C, D, E)
|
||||
{
|
||||
}
|
||||
unsafe impl<
|
||||
A: Zeroable,
|
||||
B: Zeroable,
|
||||
C: Zeroable,
|
||||
D: Zeroable,
|
||||
E: Zeroable,
|
||||
F: Zeroable,
|
||||
> Zeroable for (A, B, C, D, E, F)
|
||||
{
|
||||
}
|
||||
unsafe impl<
|
||||
A: Zeroable,
|
||||
B: Zeroable,
|
||||
C: Zeroable,
|
||||
D: Zeroable,
|
||||
E: Zeroable,
|
||||
F: Zeroable,
|
||||
G: Zeroable,
|
||||
> Zeroable for (A, B, C, D, E, F, G)
|
||||
{
|
||||
}
|
||||
unsafe impl<
|
||||
A: Zeroable,
|
||||
B: Zeroable,
|
||||
C: Zeroable,
|
||||
D: Zeroable,
|
||||
E: Zeroable,
|
||||
F: Zeroable,
|
||||
G: Zeroable,
|
||||
H: Zeroable,
|
||||
> Zeroable for (A, B, C, D, E, F, G, H)
|
||||
{
|
||||
}
|
||||
|
||||
#[cfg(feature = "min_const_generics")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "min_const_generics")))]
|
||||
unsafe impl<T, const N: usize> Zeroable for [T; N] where T: Zeroable {}
|
||||
|
||||
#[cfg(not(feature = "min_const_generics"))]
|
||||
impl_unsafe_marker_for_array!(
|
||||
Zeroable, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
||||
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256,
|
||||
512, 1024, 2048, 4096
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(all(target_arch = "wasm32", feature = "wasm_simd"))]
|
||||
unsafe impl Zeroable for wasm32::{v128}
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(all(target_arch = "aarch64", feature = "aarch64_simd"))]
|
||||
unsafe impl Zeroable for aarch64::{
|
||||
float32x2_t, float32x2x2_t, float32x2x3_t, float32x2x4_t, float32x4_t,
|
||||
float32x4x2_t, float32x4x3_t, float32x4x4_t, float64x1_t, float64x1x2_t,
|
||||
float64x1x3_t, float64x1x4_t, float64x2_t, float64x2x2_t, float64x2x3_t,
|
||||
float64x2x4_t, int16x4_t, int16x4x2_t, int16x4x3_t, int16x4x4_t, int16x8_t,
|
||||
int16x8x2_t, int16x8x3_t, int16x8x4_t, int32x2_t, int32x2x2_t, int32x2x3_t,
|
||||
int32x2x4_t, int32x4_t, int32x4x2_t, int32x4x3_t, int32x4x4_t, int64x1_t,
|
||||
int64x1x2_t, int64x1x3_t, int64x1x4_t, int64x2_t, int64x2x2_t, int64x2x3_t,
|
||||
int64x2x4_t, int8x16_t, int8x16x2_t, int8x16x3_t, int8x16x4_t, int8x8_t,
|
||||
int8x8x2_t, int8x8x3_t, int8x8x4_t, poly16x4_t, poly16x4x2_t, poly16x4x3_t,
|
||||
poly16x4x4_t, poly16x8_t, poly16x8x2_t, poly16x8x3_t, poly16x8x4_t,
|
||||
poly64x1_t, poly64x1x2_t, poly64x1x3_t, poly64x1x4_t, poly64x2_t,
|
||||
poly64x2x2_t, poly64x2x3_t, poly64x2x4_t, poly8x16_t, poly8x16x2_t,
|
||||
poly8x16x3_t, poly8x16x4_t, poly8x8_t, poly8x8x2_t, poly8x8x3_t, poly8x8x4_t,
|
||||
uint16x4_t, uint16x4x2_t, uint16x4x3_t, uint16x4x4_t, uint16x8_t,
|
||||
uint16x8x2_t, uint16x8x3_t, uint16x8x4_t, uint32x2_t, uint32x2x2_t,
|
||||
uint32x2x3_t, uint32x2x4_t, uint32x4_t, uint32x4x2_t, uint32x4x3_t,
|
||||
uint32x4x4_t, uint64x1_t, uint64x1x2_t, uint64x1x3_t, uint64x1x4_t,
|
||||
uint64x2_t, uint64x2x2_t, uint64x2x3_t, uint64x2x4_t, uint8x16_t,
|
||||
uint8x16x2_t, uint8x16x3_t, uint8x16x4_t, uint8x8_t, uint8x8x2_t,
|
||||
uint8x8x3_t, uint8x8x4_t,
|
||||
}
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(target_arch = "x86")]
|
||||
unsafe impl Zeroable for x86::{
|
||||
__m128i, __m128, __m128d,
|
||||
__m256i, __m256, __m256d,
|
||||
}
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
unsafe impl Zeroable for x86_64::{
|
||||
__m128i, __m128, __m128d,
|
||||
__m256i, __m256, __m256d,
|
||||
}
|
||||
);
|
||||
|
||||
#[cfg(feature = "nightly_portable_simd")]
|
||||
#[cfg_attr(
|
||||
feature = "nightly_docs",
|
||||
doc(cfg(feature = "nightly_portable_simd"))
|
||||
)]
|
||||
unsafe impl<T, const N: usize> Zeroable for core::simd::Simd<T, N>
|
||||
where
|
||||
T: core::simd::SimdElement + Zeroable,
|
||||
core::simd::LaneCount<N>: core::simd::SupportedLaneCount,
|
||||
{
|
||||
}
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(all(target_arch = "x86", feature = "nightly_stdsimd"))]
|
||||
unsafe impl Zeroable for x86::{
|
||||
__m128bh, __m256bh, __m512,
|
||||
__m512bh, __m512d, __m512i,
|
||||
}
|
||||
);
|
||||
|
||||
impl_unsafe_marker_for_simd!(
|
||||
#[cfg(all(target_arch = "x86_64", feature = "nightly_stdsimd"))]
|
||||
unsafe impl Zeroable for x86_64::{
|
||||
__m128bh, __m256bh, __m512,
|
||||
__m512bh, __m512d, __m512i,
|
||||
}
|
||||
);
|
35
third_party/rust/bytemuck/src/zeroable_in_option.rs
vendored
Normal file
35
third_party/rust/bytemuck/src/zeroable_in_option.rs
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
use super::*;
|
||||
|
||||
// Note(Lokathor): This is the neat part!!
|
||||
unsafe impl<T: ZeroableInOption> Zeroable for Option<T> {}
|
||||
|
||||
/// Trait for types which are [Zeroable](Zeroable) when wrapped in
|
||||
/// [Option](core::option::Option).
|
||||
///
|
||||
/// ## Safety
|
||||
///
|
||||
/// * `Option<YourType>` must uphold the same invariants as
|
||||
/// [Zeroable](Zeroable).
|
||||
pub unsafe trait ZeroableInOption: Sized {}
|
||||
|
||||
unsafe impl ZeroableInOption for NonZeroI8 {}
|
||||
unsafe impl ZeroableInOption for NonZeroI16 {}
|
||||
unsafe impl ZeroableInOption for NonZeroI32 {}
|
||||
unsafe impl ZeroableInOption for NonZeroI64 {}
|
||||
unsafe impl ZeroableInOption for NonZeroI128 {}
|
||||
unsafe impl ZeroableInOption for NonZeroIsize {}
|
||||
unsafe impl ZeroableInOption for NonZeroU8 {}
|
||||
unsafe impl ZeroableInOption for NonZeroU16 {}
|
||||
unsafe impl ZeroableInOption for NonZeroU32 {}
|
||||
unsafe impl ZeroableInOption for NonZeroU64 {}
|
||||
unsafe impl ZeroableInOption for NonZeroU128 {}
|
||||
unsafe impl ZeroableInOption for NonZeroUsize {}
|
||||
|
||||
// Note: this does not create NULL vtable because we get `None` anyway.
|
||||
unsafe impl<T: ?Sized> ZeroableInOption for NonNull<T> {}
|
||||
unsafe impl<T: ?Sized> ZeroableInOption for &'_ T {}
|
||||
unsafe impl<T: ?Sized> ZeroableInOption for &'_ mut T {}
|
||||
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_alloc")))]
|
||||
unsafe impl<T: ?Sized> ZeroableInOption for alloc::boxed::Box<T> {}
|
12
third_party/rust/bytemuck/tests/array_tests.rs
vendored
Normal file
12
third_party/rust/bytemuck/tests/array_tests.rs
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
#[test]
|
||||
pub fn test_cast_array() {
|
||||
let x = [0u32, 1u32, 2u32];
|
||||
let _: [u16; 6] = bytemuck::cast(x);
|
||||
}
|
||||
|
||||
#[cfg(feature = "min_const_generics")]
|
||||
#[test]
|
||||
pub fn test_cast_long_array() {
|
||||
let x = [0u32; 65];
|
||||
let _: [u16; 130] = bytemuck::cast(x);
|
||||
}
|
197
third_party/rust/bytemuck/tests/cast_slice_tests.rs
vendored
Normal file
197
third_party/rust/bytemuck/tests/cast_slice_tests.rs
vendored
Normal file
@ -0,0 +1,197 @@
|
||||
#![allow(clippy::unnecessary_cast)]
|
||||
#![allow(clippy::manual_slice_size_calculation)]
|
||||
|
||||
use core::mem::size_of;
|
||||
|
||||
use bytemuck::*;
|
||||
|
||||
#[test]
|
||||
fn test_try_cast_slice() {
|
||||
// some align4 data
|
||||
let u32_slice: &[u32] = &[4, 5, 6];
|
||||
// the same data as align1
|
||||
let the_bytes: &[u8] = try_cast_slice(u32_slice).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
u32_slice.as_ptr() as *const u32 as usize,
|
||||
the_bytes.as_ptr() as *const u8 as usize
|
||||
);
|
||||
assert_eq!(
|
||||
u32_slice.len() * size_of::<u32>(),
|
||||
the_bytes.len() * size_of::<u8>()
|
||||
);
|
||||
|
||||
// by taking one byte off the front, we're definitely mis-aligned for u32.
|
||||
let mis_aligned_bytes = &the_bytes[1..];
|
||||
assert_eq!(
|
||||
try_cast_slice::<u8, u32>(mis_aligned_bytes),
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
);
|
||||
|
||||
// by taking one byte off the end, we're aligned but would have slop bytes for
|
||||
// u32
|
||||
let the_bytes_len_minus1 = the_bytes.len() - 1;
|
||||
let slop_bytes = &the_bytes[..the_bytes_len_minus1];
|
||||
assert_eq!(
|
||||
try_cast_slice::<u8, u32>(slop_bytes),
|
||||
Err(PodCastError::OutputSliceWouldHaveSlop)
|
||||
);
|
||||
|
||||
// if we don't mess with it we can up-alignment cast
|
||||
try_cast_slice::<u8, u32>(the_bytes).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_cast_slice_mut() {
|
||||
// some align4 data
|
||||
let u32_slice: &mut [u32] = &mut [4, 5, 6];
|
||||
let u32_len = u32_slice.len();
|
||||
let u32_ptr = u32_slice.as_ptr();
|
||||
|
||||
// the same data as align1
|
||||
let the_bytes: &mut [u8] = try_cast_slice_mut(u32_slice).unwrap();
|
||||
let the_bytes_len = the_bytes.len();
|
||||
let the_bytes_ptr = the_bytes.as_ptr();
|
||||
|
||||
assert_eq!(
|
||||
u32_ptr as *const u32 as usize,
|
||||
the_bytes_ptr as *const u8 as usize
|
||||
);
|
||||
assert_eq!(u32_len * size_of::<u32>(), the_bytes_len * size_of::<u8>());
|
||||
|
||||
// by taking one byte off the front, we're definitely mis-aligned for u32.
|
||||
let mis_aligned_bytes = &mut the_bytes[1..];
|
||||
assert_eq!(
|
||||
try_cast_slice_mut::<u8, u32>(mis_aligned_bytes),
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
);
|
||||
|
||||
// by taking one byte off the end, we're aligned but would have slop bytes for
|
||||
// u32
|
||||
let the_bytes_len_minus1 = the_bytes.len() - 1;
|
||||
let slop_bytes = &mut the_bytes[..the_bytes_len_minus1];
|
||||
assert_eq!(
|
||||
try_cast_slice_mut::<u8, u32>(slop_bytes),
|
||||
Err(PodCastError::OutputSliceWouldHaveSlop)
|
||||
);
|
||||
|
||||
// if we don't mess with it we can up-alignment cast
|
||||
try_cast_slice_mut::<u8, u32>(the_bytes).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_types() {
|
||||
let _: i32 = cast(1.0_f32);
|
||||
let _: &mut i32 = cast_mut(&mut 1.0_f32);
|
||||
let _: &i32 = cast_ref(&1.0_f32);
|
||||
let _: &[i32] = cast_slice(&[1.0_f32]);
|
||||
let _: &mut [i32] = cast_slice_mut(&mut [1.0_f32]);
|
||||
//
|
||||
let _: Result<i32, PodCastError> = try_cast(1.0_f32);
|
||||
let _: Result<&mut i32, PodCastError> = try_cast_mut(&mut 1.0_f32);
|
||||
let _: Result<&i32, PodCastError> = try_cast_ref(&1.0_f32);
|
||||
let _: Result<&[i32], PodCastError> = try_cast_slice(&[1.0_f32]);
|
||||
let _: Result<&mut [i32], PodCastError> = try_cast_slice_mut(&mut [1.0_f32]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytes_of() {
|
||||
assert_eq!(bytes_of(&0xaabbccdd_u32), &0xaabbccdd_u32.to_ne_bytes());
|
||||
assert_eq!(
|
||||
bytes_of_mut(&mut 0xaabbccdd_u32),
|
||||
&mut 0xaabbccdd_u32.to_ne_bytes()
|
||||
);
|
||||
let mut a = 0xaabbccdd_u32;
|
||||
let a_addr = &a as *const _ as usize;
|
||||
// ensure addresses match.
|
||||
assert_eq!(bytes_of(&a).as_ptr() as usize, a_addr);
|
||||
assert_eq!(bytes_of_mut(&mut a).as_ptr() as usize, a_addr);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_bytes() {
|
||||
let u32s = [0xaabbccdd, 0x11223344_u32];
|
||||
let bytes = bytemuck::cast_slice::<u32, u8>(&u32s);
|
||||
assert_eq!(try_from_bytes::<u32>(&bytes[..4]), Ok(&u32s[0]));
|
||||
assert_eq!(
|
||||
try_from_bytes::<u32>(&bytes[..5]),
|
||||
Err(PodCastError::SizeMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
try_from_bytes::<u32>(&bytes[..3]),
|
||||
Err(PodCastError::SizeMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
try_from_bytes::<u32>(&bytes[1..5]),
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_bytes_mut() {
|
||||
let mut abcd = 0xaabbccdd;
|
||||
let mut u32s = [abcd, 0x11223344_u32];
|
||||
let bytes = bytemuck::cast_slice_mut::<u32, u8>(&mut u32s);
|
||||
assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..4]), Ok(&mut abcd));
|
||||
assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..4]), Ok(&mut abcd));
|
||||
assert_eq!(
|
||||
try_from_bytes_mut::<u32>(&mut bytes[..5]),
|
||||
Err(PodCastError::SizeMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
try_from_bytes_mut::<u32>(&mut bytes[..3]),
|
||||
Err(PodCastError::SizeMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
try_from_bytes::<u32>(&bytes[1..5]),
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_bytes() {
|
||||
let abcd = 0xaabbccdd_u32;
|
||||
let aligned_bytes = bytemuck::bytes_of(&abcd);
|
||||
assert_eq!(from_bytes::<u32>(aligned_bytes), &abcd);
|
||||
assert!(core::ptr::eq(from_bytes(aligned_bytes), &abcd));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_bytes_mut() {
|
||||
let mut a = 0xaabbccdd_u32;
|
||||
let a_addr = &a as *const _ as usize;
|
||||
let aligned_bytes = bytemuck::bytes_of_mut(&mut a);
|
||||
assert_eq!(*from_bytes_mut::<u32>(aligned_bytes), 0xaabbccdd_u32);
|
||||
assert_eq!(
|
||||
from_bytes_mut::<u32>(aligned_bytes) as *const u32 as usize,
|
||||
a_addr
|
||||
);
|
||||
}
|
||||
|
||||
// like #[should_panic], but can be a part of another test, instead of requiring
|
||||
// it to be it's own test.
|
||||
macro_rules! should_panic {
|
||||
($ex:expr) => {
|
||||
assert!(
|
||||
std::panic::catch_unwind(|| {
|
||||
let _ = $ex;
|
||||
})
|
||||
.is_err(),
|
||||
concat!("should have panicked: `", stringify!($ex), "`")
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_panics() {
|
||||
should_panic!(cast_slice::<u8, u32>(&[1u8, 2u8]));
|
||||
should_panic!(cast_slice_mut::<u8, u32>(&mut [1u8, 2u8]));
|
||||
should_panic!(from_bytes::<u32>(&[1u8, 2]));
|
||||
should_panic!(from_bytes::<u32>(&[1u8, 2, 3, 4, 5]));
|
||||
should_panic!(from_bytes_mut::<u32>(&mut [1u8, 2]));
|
||||
should_panic!(from_bytes_mut::<u32>(&mut [1u8, 2, 3, 4, 5]));
|
||||
// use cast_slice on some u32s to get some align>=4 bytes, so we can know
|
||||
// we'll give from_bytes unaligned ones.
|
||||
let aligned_bytes = bytemuck::cast_slice::<u32, u8>(&[0, 0]);
|
||||
should_panic!(from_bytes::<u32>(&aligned_bytes[1..5]));
|
||||
}
|
419
third_party/rust/bytemuck/tests/checked_tests.rs
vendored
Normal file
419
third_party/rust/bytemuck/tests/checked_tests.rs
vendored
Normal file
@ -0,0 +1,419 @@
|
||||
#![allow(clippy::unnecessary_cast)]
|
||||
#![allow(clippy::manual_slice_size_calculation)]
|
||||
|
||||
use core::{
|
||||
mem::size_of,
|
||||
num::{NonZeroU32, NonZeroU8},
|
||||
};
|
||||
|
||||
use bytemuck::{checked::CheckedCastError, *};
|
||||
|
||||
#[test]
|
||||
fn test_try_cast_slice() {
|
||||
// some align4 data
|
||||
let nonzero_u32_slice: &[NonZeroU32] = &[
|
||||
NonZeroU32::new(4).unwrap(),
|
||||
NonZeroU32::new(5).unwrap(),
|
||||
NonZeroU32::new(6).unwrap(),
|
||||
];
|
||||
|
||||
// contains bytes with invalid bitpattern for NonZeroU8
|
||||
assert_eq!(
|
||||
checked::try_cast_slice::<NonZeroU32, NonZeroU8>(nonzero_u32_slice),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
|
||||
// the same data as align1
|
||||
let the_bytes: &[u8] = checked::try_cast_slice(nonzero_u32_slice).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
nonzero_u32_slice.as_ptr() as *const NonZeroU32 as usize,
|
||||
the_bytes.as_ptr() as *const u8 as usize
|
||||
);
|
||||
assert_eq!(
|
||||
nonzero_u32_slice.len() * size_of::<NonZeroU32>(),
|
||||
the_bytes.len() * size_of::<u8>()
|
||||
);
|
||||
|
||||
// by taking one byte off the front, we're definitely mis-aligned for
|
||||
// NonZeroU32.
|
||||
let mis_aligned_bytes = &the_bytes[1..];
|
||||
assert_eq!(
|
||||
checked::try_cast_slice::<u8, NonZeroU32>(mis_aligned_bytes),
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
|
||||
// by taking one byte off the end, we're aligned but would have slop bytes for
|
||||
// NonZeroU32
|
||||
let the_bytes_len_minus1 = the_bytes.len() - 1;
|
||||
let slop_bytes = &the_bytes[..the_bytes_len_minus1];
|
||||
assert_eq!(
|
||||
checked::try_cast_slice::<u8, NonZeroU32>(slop_bytes),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::OutputSliceWouldHaveSlop))
|
||||
);
|
||||
|
||||
// if we don't mess with it we can up-alignment cast
|
||||
checked::try_cast_slice::<u8, NonZeroU32>(the_bytes).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_cast_slice_mut() {
|
||||
// some align4 data
|
||||
let u32_slice: &mut [u32] = &mut [4, 5, 6];
|
||||
|
||||
// contains bytes with invalid bitpattern for NonZeroU8
|
||||
assert_eq!(
|
||||
checked::try_cast_slice_mut::<u32, NonZeroU8>(u32_slice),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
|
||||
// some align4 data
|
||||
let u32_slice: &mut [u32] = &mut [0x4444_4444, 0x5555_5555, 0x6666_6666];
|
||||
let u32_len = u32_slice.len();
|
||||
let u32_ptr = u32_slice.as_ptr();
|
||||
|
||||
// the same data as align1, nonzero bytes
|
||||
let the_nonzero_bytes: &mut [NonZeroU8] =
|
||||
checked::try_cast_slice_mut(u32_slice).unwrap();
|
||||
let the_nonzero_bytes_len = the_nonzero_bytes.len();
|
||||
let the_nonzero_bytes_ptr = the_nonzero_bytes.as_ptr();
|
||||
|
||||
assert_eq!(
|
||||
u32_ptr as *const u32 as usize,
|
||||
the_nonzero_bytes_ptr as *const NonZeroU8 as usize
|
||||
);
|
||||
assert_eq!(
|
||||
u32_len * size_of::<u32>(),
|
||||
the_nonzero_bytes_len * size_of::<NonZeroU8>()
|
||||
);
|
||||
|
||||
// the same data as align1
|
||||
let the_bytes: &mut [u8] = checked::try_cast_slice_mut(u32_slice).unwrap();
|
||||
let the_bytes_len = the_bytes.len();
|
||||
let the_bytes_ptr = the_bytes.as_ptr();
|
||||
|
||||
assert_eq!(
|
||||
u32_ptr as *const u32 as usize,
|
||||
the_bytes_ptr as *const u8 as usize
|
||||
);
|
||||
assert_eq!(
|
||||
u32_len * size_of::<u32>(),
|
||||
the_bytes_len * size_of::<NonZeroU8>()
|
||||
);
|
||||
|
||||
// by taking one byte off the front, we're definitely mis-aligned for u32.
|
||||
let mis_aligned_bytes = &mut the_bytes[1..];
|
||||
assert_eq!(
|
||||
checked::try_cast_slice_mut::<u8, NonZeroU32>(mis_aligned_bytes),
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
|
||||
// by taking one byte off the end, we're aligned but would have slop bytes for
|
||||
// NonZeroU32
|
||||
let the_bytes_len_minus1 = the_bytes.len() - 1;
|
||||
let slop_bytes = &mut the_bytes[..the_bytes_len_minus1];
|
||||
assert_eq!(
|
||||
checked::try_cast_slice_mut::<u8, NonZeroU32>(slop_bytes),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::OutputSliceWouldHaveSlop))
|
||||
);
|
||||
|
||||
// if we don't mess with it we can up-alignment cast, since there are no
|
||||
// zeroes in the original slice
|
||||
checked::try_cast_slice_mut::<u8, NonZeroU32>(the_bytes).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_types() {
|
||||
let _: NonZeroU32 = checked::cast(1.0_f32);
|
||||
let _: &mut NonZeroU32 = checked::cast_mut(&mut 1.0_f32);
|
||||
let _: &NonZeroU32 = checked::cast_ref(&1.0_f32);
|
||||
let _: &[NonZeroU32] = checked::cast_slice(&[1.0_f32]);
|
||||
let _: &mut [NonZeroU32] = checked::cast_slice_mut(&mut [1.0_f32]);
|
||||
//
|
||||
let _: Result<NonZeroU32, CheckedCastError> = checked::try_cast(1.0_f32);
|
||||
let _: Result<&mut NonZeroU32, CheckedCastError> =
|
||||
checked::try_cast_mut(&mut 1.0_f32);
|
||||
let _: Result<&NonZeroU32, CheckedCastError> =
|
||||
checked::try_cast_ref(&1.0_f32);
|
||||
let _: Result<&[NonZeroU32], CheckedCastError> =
|
||||
checked::try_cast_slice(&[1.0_f32]);
|
||||
let _: Result<&mut [NonZeroU32], CheckedCastError> =
|
||||
checked::try_cast_slice_mut(&mut [1.0_f32]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_pod_read_unaligned() {
|
||||
let u32s = [0xaabbccdd, 0x11223344_u32];
|
||||
let bytes = bytemuck::checked::cast_slice::<u32, u8>(&u32s);
|
||||
|
||||
#[cfg(target_endian = "big")]
|
||||
assert_eq!(
|
||||
checked::try_pod_read_unaligned::<NonZeroU32>(&bytes[1..5]),
|
||||
Ok(NonZeroU32::new(0xbbccdd11).unwrap())
|
||||
);
|
||||
#[cfg(target_endian = "little")]
|
||||
assert_eq!(
|
||||
checked::try_pod_read_unaligned::<NonZeroU32>(&bytes[1..5]),
|
||||
Ok(NonZeroU32::new(0x44aabbcc).unwrap())
|
||||
);
|
||||
|
||||
let u32s = [0; 2];
|
||||
let bytes = bytemuck::checked::cast_slice::<u32, u8>(&u32s);
|
||||
|
||||
assert_eq!(
|
||||
checked::try_pod_read_unaligned::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_bytes() {
|
||||
let nonzero_u32s = [
|
||||
NonZeroU32::new(0xaabbccdd).unwrap(),
|
||||
NonZeroU32::new(0x11223344).unwrap(),
|
||||
];
|
||||
let bytes = bytemuck::checked::cast_slice::<NonZeroU32, u8>(&nonzero_u32s);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..4]),
|
||||
Ok(&nonzero_u32s[0])
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..3]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
|
||||
let zero_u32s = [0, 0x11223344_u32];
|
||||
let bytes = bytemuck::checked::cast_slice::<u32, u8>(&zero_u32s);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..4]),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[4..]),
|
||||
Ok(&NonZeroU32::new(zero_u32s[1]).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..3]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_bytes_mut() {
|
||||
let a = 0xaabbccdd_u32;
|
||||
let b = 0x11223344_u32;
|
||||
let mut u32s = [a, b];
|
||||
let bytes = bytemuck::checked::cast_slice_mut::<u32, u8>(&mut u32s);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..4]),
|
||||
Ok(&mut NonZeroU32::new(a).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[4..]),
|
||||
Ok(&mut NonZeroU32::new(b).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..3]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
|
||||
let mut u32s = [0, b];
|
||||
let bytes = bytemuck::checked::cast_slice_mut::<u32, u8>(&mut u32s);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..4]),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[4..]),
|
||||
Ok(&mut NonZeroU32::new(b).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..3]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_bytes() {
|
||||
let abcd = 0xaabbccdd_u32;
|
||||
let aligned_bytes = bytemuck::bytes_of(&abcd);
|
||||
assert_eq!(
|
||||
checked::from_bytes::<NonZeroU32>(aligned_bytes),
|
||||
&NonZeroU32::new(abcd).unwrap()
|
||||
);
|
||||
assert!(core::ptr::eq(
|
||||
checked::from_bytes(aligned_bytes) as *const NonZeroU32 as *const u32,
|
||||
&abcd
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_bytes_mut() {
|
||||
let mut a = 0xaabbccdd_u32;
|
||||
let a_addr = &a as *const _ as usize;
|
||||
let aligned_bytes = bytemuck::bytes_of_mut(&mut a);
|
||||
assert_eq!(
|
||||
*checked::from_bytes_mut::<NonZeroU32>(aligned_bytes),
|
||||
NonZeroU32::new(0xaabbccdd).unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
checked::from_bytes_mut::<NonZeroU32>(aligned_bytes) as *const NonZeroU32
|
||||
as usize,
|
||||
a_addr
|
||||
);
|
||||
}
|
||||
|
||||
// like #[should_panic], but can be a part of another test, instead of requiring
|
||||
// it to be it's own test.
|
||||
macro_rules! should_panic {
|
||||
($ex:expr) => {
|
||||
assert!(
|
||||
std::panic::catch_unwind(|| {
|
||||
let _ = $ex;
|
||||
})
|
||||
.is_err(),
|
||||
concat!("should have panicked: `", stringify!($ex), "`")
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_panics() {
|
||||
should_panic!(checked::cast::<u32, NonZeroU32>(0));
|
||||
should_panic!(checked::cast_ref::<u32, NonZeroU32>(&0));
|
||||
should_panic!(checked::cast_mut::<u32, NonZeroU32>(&mut 0));
|
||||
should_panic!(checked::cast_slice::<u8, NonZeroU32>(&[1u8, 2u8]));
|
||||
should_panic!(checked::cast_slice_mut::<u8, NonZeroU32>(&mut [1u8, 2u8]));
|
||||
should_panic!(checked::from_bytes::<NonZeroU32>(&[1u8, 2]));
|
||||
should_panic!(checked::from_bytes::<NonZeroU32>(&[1u8, 2, 3, 4, 5]));
|
||||
should_panic!(checked::from_bytes_mut::<NonZeroU32>(&mut [1u8, 2]));
|
||||
should_panic!(checked::from_bytes_mut::<NonZeroU32>(&mut [1u8, 2, 3, 4, 5]));
|
||||
// use cast_slice on some u32s to get some align>=4 bytes, so we can know
|
||||
// we'll give from_bytes unaligned ones.
|
||||
let aligned_bytes = bytemuck::cast_slice::<u32, u8>(&[0, 0]);
|
||||
should_panic!(checked::from_bytes::<NonZeroU32>(aligned_bytes));
|
||||
should_panic!(checked::from_bytes::<NonZeroU32>(&aligned_bytes[1..5]));
|
||||
should_panic!(checked::pod_read_unaligned::<NonZeroU32>(
|
||||
&aligned_bytes[1..5]
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char() {
|
||||
assert_eq!(checked::try_cast::<u32, char>(0), Ok('\0'));
|
||||
assert_eq!(checked::try_cast::<u32, char>(0xd7ff), Ok('\u{d7ff}'));
|
||||
assert_eq!(
|
||||
checked::try_cast::<u32, char>(0xd800),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_cast::<u32, char>(0xdfff),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(checked::try_cast::<u32, char>(0xe000), Ok('\u{e000}'));
|
||||
assert_eq!(checked::try_cast::<u32, char>(0x10ffff), Ok('\u{10ffff}'));
|
||||
assert_eq!(
|
||||
checked::try_cast::<u32, char>(0x110000),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_cast::<u32, char>(-1i32 as u32),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool() {
|
||||
assert_eq!(checked::try_cast::<u8, bool>(0), Ok(false));
|
||||
assert_eq!(checked::try_cast::<u8, bool>(1), Ok(true));
|
||||
for i in 2..=255 {
|
||||
assert_eq!(
|
||||
checked::try_cast::<u8, bool>(i),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(checked::try_from_bytes::<bool>(&[1]), Ok(&true));
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<bool>(&[3]),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<bool>(&[0, 1]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_nonzero() {
|
||||
use core::num::*;
|
||||
macro_rules! test_nonzero {
|
||||
($nonzero:ty: $primitive:ty) => {
|
||||
assert_eq!(
|
||||
checked::try_cast::<$primitive, $nonzero>(0),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_cast::<$primitive, $nonzero>(1),
|
||||
Ok(<$nonzero>::new(1).unwrap())
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
test_nonzero!(NonZeroU8: u8);
|
||||
test_nonzero!(NonZeroI8: i8);
|
||||
test_nonzero!(NonZeroU16: u16);
|
||||
test_nonzero!(NonZeroI16: i16);
|
||||
test_nonzero!(NonZeroU32: u32);
|
||||
test_nonzero!(NonZeroI32: i32);
|
||||
test_nonzero!(NonZeroU64: u64);
|
||||
test_nonzero!(NonZeroI64: i64);
|
||||
test_nonzero!(NonZeroU128: u128);
|
||||
test_nonzero!(NonZeroI128: i128);
|
||||
test_nonzero!(NonZeroUsize: usize);
|
||||
test_nonzero!(NonZeroIsize: isize);
|
||||
}
|
77
third_party/rust/bytemuck/tests/derive.rs
vendored
Normal file
77
third_party/rust/bytemuck/tests/derive.rs
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
#![cfg(feature = "derive")]
|
||||
#![allow(dead_code)]
|
||||
|
||||
use bytemuck::{ByteEq, ByteHash, Pod, TransparentWrapper, Zeroable};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[derive(Copy, Clone, Pod, Zeroable, ByteEq, ByteHash)]
|
||||
#[repr(C)]
|
||||
struct Test {
|
||||
a: u16,
|
||||
b: u16,
|
||||
}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
struct TransparentSingle {
|
||||
a: u16,
|
||||
}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
#[transparent(u16)]
|
||||
struct TransparentWithZeroSized {
|
||||
a: u16,
|
||||
b: (),
|
||||
}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
struct TransparentWithGeneric<T: ?Sized> {
|
||||
a: T,
|
||||
}
|
||||
|
||||
/// Ensuring that no additional bounds are emitted.
|
||||
/// See https://github.com/Lokathor/bytemuck/issues/145
|
||||
fn test_generic<T>(x: T) -> TransparentWithGeneric<T> {
|
||||
TransparentWithGeneric::wrap(x)
|
||||
}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
#[transparent(T)]
|
||||
struct TransparentWithGenericAndZeroSized<T: ?Sized> {
|
||||
a: (),
|
||||
b: T,
|
||||
}
|
||||
|
||||
/// Ensuring that no additional bounds are emitted.
|
||||
/// See https://github.com/Lokathor/bytemuck/issues/145
|
||||
fn test_generic_with_zst<T>(x: T) -> TransparentWithGenericAndZeroSized<T> {
|
||||
TransparentWithGenericAndZeroSized::wrap(x)
|
||||
}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
struct TransparentUnsized {
|
||||
a: dyn std::fmt::Debug,
|
||||
}
|
||||
|
||||
type DynDebug = dyn std::fmt::Debug;
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
#[transparent(DynDebug)]
|
||||
struct TransparentUnsizedWithZeroSized {
|
||||
a: (),
|
||||
b: DynDebug,
|
||||
}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
#[transparent(DynDebug)]
|
||||
struct TransparentUnsizedWithGenericZeroSizeds<T: ?Sized, U: ?Sized> {
|
||||
a: PhantomData<T>,
|
||||
b: PhantomData<U>,
|
||||
c: DynDebug,
|
||||
}
|
124
third_party/rust/bytemuck/tests/doc_tests.rs
vendored
Normal file
124
third_party/rust/bytemuck/tests/doc_tests.rs
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
#![allow(clippy::disallowed_names)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
//! Cargo miri doesn't run doctests yet, so we duplicate these here. It's
|
||||
//! probably not that important to sweat keeping these perfectly up to date, but
|
||||
//! we should try to catch the cases where the primary tests are doctests.
|
||||
use bytemuck::*;
|
||||
|
||||
// Miri doesn't run on doctests, so... copypaste to the rescue.
|
||||
#[test]
|
||||
fn test_transparent_slice() {
|
||||
#[repr(transparent)]
|
||||
struct Slice<T>([T]);
|
||||
|
||||
unsafe impl<T> TransparentWrapper<[T]> for Slice<T> {}
|
||||
|
||||
let s = Slice::wrap_ref(&[1u32, 2, 3]);
|
||||
assert_eq!(&s.0, &[1, 2, 3]);
|
||||
|
||||
let mut buf = [1, 2, 3u8];
|
||||
let _sm = Slice::wrap_mut(&mut buf);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transparent_basic() {
|
||||
#[derive(Default)]
|
||||
struct SomeStruct(u32);
|
||||
|
||||
#[repr(transparent)]
|
||||
struct MyWrapper(SomeStruct);
|
||||
|
||||
unsafe impl TransparentWrapper<SomeStruct> for MyWrapper {}
|
||||
|
||||
// interpret a reference to &SomeStruct as a &MyWrapper
|
||||
let thing = SomeStruct::default();
|
||||
let wrapped_ref: &MyWrapper = MyWrapper::wrap_ref(&thing);
|
||||
|
||||
// Works with &mut too.
|
||||
let mut mut_thing = SomeStruct::default();
|
||||
let wrapped_mut: &mut MyWrapper = MyWrapper::wrap_mut(&mut mut_thing);
|
||||
let _ = (wrapped_ref, wrapped_mut);
|
||||
}
|
||||
|
||||
// Work around miri not running doctests
|
||||
#[test]
|
||||
fn test_contiguous_doc() {
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
enum Foo {
|
||||
A = 0,
|
||||
B = 1,
|
||||
C = 2,
|
||||
D = 3,
|
||||
E = 4,
|
||||
}
|
||||
unsafe impl Contiguous for Foo {
|
||||
type Int = u8;
|
||||
const MIN_VALUE: u8 = Foo::A as u8;
|
||||
const MAX_VALUE: u8 = Foo::E as u8;
|
||||
}
|
||||
|
||||
assert_eq!(Foo::from_integer(3).unwrap(), Foo::D);
|
||||
assert_eq!(Foo::from_integer(8), None);
|
||||
assert_eq!(Foo::C.into_integer(), 2);
|
||||
assert_eq!(Foo::B.into_integer(), Foo::B as u8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_offsetof_vertex() {
|
||||
#[repr(C)]
|
||||
struct Vertex {
|
||||
pos: [f32; 2],
|
||||
uv: [u16; 2],
|
||||
color: [u8; 4],
|
||||
}
|
||||
unsafe impl Zeroable for Vertex {}
|
||||
|
||||
let pos = offset_of!(Zeroable::zeroed(), Vertex, pos);
|
||||
let uv = offset_of!(Zeroable::zeroed(), Vertex, uv);
|
||||
let color = offset_of!(Zeroable::zeroed(), Vertex, color);
|
||||
|
||||
assert_eq!(pos, 0);
|
||||
assert_eq!(uv, 8);
|
||||
assert_eq!(color, 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_offsetof_nonpod() {
|
||||
#[derive(Default)]
|
||||
struct Foo {
|
||||
a: u8,
|
||||
b: &'static str,
|
||||
c: i32,
|
||||
}
|
||||
|
||||
let a_offset = offset_of!(Default::default(), Foo, a);
|
||||
let b_offset = offset_of!(Default::default(), Foo, b);
|
||||
let c_offset = offset_of!(Default::default(), Foo, c);
|
||||
|
||||
assert_ne!(a_offset, b_offset);
|
||||
assert_ne!(b_offset, c_offset);
|
||||
// We can't check against hardcoded values for a repr(Rust) type,
|
||||
// but prove to ourself this way.
|
||||
|
||||
let foo = Foo::default();
|
||||
// Note: offsets are in bytes.
|
||||
let as_bytes = &foo as *const _ as *const u8;
|
||||
|
||||
// We're using wrapping_offset here because it's not worth
|
||||
// the unsafe block, but it would be valid to use `add` instead,
|
||||
// as it cannot overflow.
|
||||
assert_eq!(
|
||||
&foo.a as *const _ as usize,
|
||||
as_bytes.wrapping_add(a_offset) as usize
|
||||
);
|
||||
assert_eq!(
|
||||
&foo.b as *const _ as usize,
|
||||
as_bytes.wrapping_add(b_offset) as usize
|
||||
);
|
||||
assert_eq!(
|
||||
&foo.c as *const _ as usize,
|
||||
as_bytes.wrapping_add(c_offset) as usize
|
||||
);
|
||||
}
|
60
third_party/rust/bytemuck/tests/offset_of_tests.rs
vendored
Normal file
60
third_party/rust/bytemuck/tests/offset_of_tests.rs
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
#![allow(clippy::disallowed_names)]
|
||||
use bytemuck::{offset_of, Zeroable};
|
||||
|
||||
#[test]
|
||||
fn test_offset_of_vertex() {
|
||||
#[repr(C)]
|
||||
struct Vertex {
|
||||
pos: [f32; 2],
|
||||
uv: [u16; 2],
|
||||
color: [u8; 4],
|
||||
}
|
||||
unsafe impl Zeroable for Vertex {}
|
||||
|
||||
let pos = offset_of!(Zeroable::zeroed(), Vertex, pos);
|
||||
let uv = offset_of!(Zeroable::zeroed(), Vertex, uv);
|
||||
let color = offset_of!(Zeroable::zeroed(), Vertex, color);
|
||||
|
||||
assert_eq!(pos, 0);
|
||||
assert_eq!(uv, 8);
|
||||
assert_eq!(color, 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_offset_of_foo() {
|
||||
#[derive(Default)]
|
||||
struct Foo {
|
||||
a: u8,
|
||||
b: &'static str,
|
||||
c: i32,
|
||||
}
|
||||
|
||||
let a_offset = offset_of!(Default::default(), Foo, a);
|
||||
let b_offset = offset_of!(Default::default(), Foo, b);
|
||||
let c_offset = offset_of!(Default::default(), Foo, c);
|
||||
|
||||
assert_ne!(a_offset, b_offset);
|
||||
assert_ne!(b_offset, c_offset);
|
||||
// We can't check against hardcoded values for a repr(Rust) type,
|
||||
// but prove to ourself this way.
|
||||
|
||||
let foo = Foo::default();
|
||||
// Note: offsets are in bytes.
|
||||
let as_bytes = &foo as *const _ as *const u8;
|
||||
|
||||
// we're using wrapping_offset here because it's not worth
|
||||
// the unsafe block, but it would be valid to use `add` instead,
|
||||
// as it cannot overflow.
|
||||
assert_eq!(
|
||||
&foo.a as *const _ as usize,
|
||||
as_bytes.wrapping_add(a_offset) as usize
|
||||
);
|
||||
assert_eq!(
|
||||
&foo.b as *const _ as usize,
|
||||
as_bytes.wrapping_add(b_offset) as usize
|
||||
);
|
||||
assert_eq!(
|
||||
&foo.c as *const _ as usize,
|
||||
as_bytes.wrapping_add(c_offset) as usize
|
||||
);
|
||||
}
|
107
third_party/rust/bytemuck/tests/std_tests.rs
vendored
Normal file
107
third_party/rust/bytemuck/tests/std_tests.rs
vendored
Normal file
@ -0,0 +1,107 @@
|
||||
#![allow(clippy::uninlined_format_args)]
|
||||
#![allow(unused_imports)]
|
||||
//! The integration tests seem to always have `std` linked, so things that would
|
||||
//! depend on that can go here.
|
||||
|
||||
use bytemuck::*;
|
||||
use core::num::NonZeroU8;
|
||||
|
||||
#[test]
|
||||
fn test_transparent_vtabled() {
|
||||
use core::fmt::Display;
|
||||
|
||||
#[repr(transparent)]
|
||||
struct DisplayTraitObj(dyn Display);
|
||||
|
||||
unsafe impl TransparentWrapper<dyn Display> for DisplayTraitObj {}
|
||||
|
||||
impl Display for DisplayTraitObj {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
let v = DisplayTraitObj::wrap_ref(&5i32);
|
||||
let s = format!("{}", v);
|
||||
assert_eq!(s, "5");
|
||||
|
||||
let mut x = 100i32;
|
||||
let v_mut = DisplayTraitObj::wrap_mut(&mut x);
|
||||
let s = format!("{}", v_mut);
|
||||
assert_eq!(s, "100");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
fn test_large_box_alloc() {
|
||||
type SuperPage = [[u8; 4096]; 4096];
|
||||
let _: Box<SuperPage> = try_zeroed_box().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
fn test_zero_sized_box_alloc() {
|
||||
#[repr(align(4096))]
|
||||
struct Empty;
|
||||
unsafe impl Zeroable for Empty {}
|
||||
let _: Box<Empty> = try_zeroed_box().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
fn test_try_from_box_bytes() {
|
||||
// Different layout: target alignment is greater than source alignment.
|
||||
assert_eq!(
|
||||
try_from_box_bytes::<u32>(Box::new([0u8; 4]).into()).map_err(|(x, _)| x),
|
||||
Err(PodCastError::AlignmentMismatch)
|
||||
);
|
||||
|
||||
// Different layout: target alignment is less than source alignment.
|
||||
assert_eq!(
|
||||
try_from_box_bytes::<u32>(Box::new(0u64).into()).map_err(|(x, _)| x),
|
||||
Err(PodCastError::AlignmentMismatch)
|
||||
);
|
||||
|
||||
// Different layout: target size is greater than source size.
|
||||
assert_eq!(
|
||||
try_from_box_bytes::<[u32; 2]>(Box::new(0u32).into()).map_err(|(x, _)| x),
|
||||
Err(PodCastError::SizeMismatch)
|
||||
);
|
||||
|
||||
// Different layout: target size is less than source size.
|
||||
assert_eq!(
|
||||
try_from_box_bytes::<u32>(Box::new([0u32; 2]).into()).map_err(|(x, _)| x),
|
||||
Err(PodCastError::SizeMismatch)
|
||||
);
|
||||
|
||||
// Round trip: alignment is equal to size.
|
||||
assert_eq!(*from_box_bytes::<u32>(Box::new(1000u32).into()), 1000u32);
|
||||
|
||||
// Round trip: alignment is divider of size.
|
||||
assert_eq!(&*from_box_bytes::<[u8; 5]>(Box::new(*b"hello").into()), b"hello");
|
||||
|
||||
// It's ok for T to have uninitialized bytes.
|
||||
#[cfg(feature = "derive")]
|
||||
{
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, AnyBitPattern)]
|
||||
struct Foo(u8, u16);
|
||||
assert_eq!(
|
||||
*from_box_bytes::<Foo>(Box::new([0xc5c5u16; 2]).into()),
|
||||
Foo(0xc5u8, 0xc5c5u16)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
fn test_box_bytes_of() {
|
||||
assert_eq!(&*box_bytes_of(Box::new(*b"hello")), b"hello");
|
||||
|
||||
#[cfg(target_endian = "big")]
|
||||
assert_eq!(&*box_bytes_of(Box::new(0x12345678)), b"\x12\x34\x56\x78");
|
||||
#[cfg(target_endian = "little")]
|
||||
assert_eq!(&*box_bytes_of(Box::new(0x12345678)), b"\x78\x56\x34\x12");
|
||||
|
||||
// It's ok for T to have invalid bit patterns.
|
||||
assert_eq!(&*box_bytes_of(Box::new(NonZeroU8::new(0xc5))), b"\xc5");
|
||||
}
|
116
third_party/rust/bytemuck/tests/transparent.rs
vendored
Normal file
116
third_party/rust/bytemuck/tests/transparent.rs
vendored
Normal file
@ -0,0 +1,116 @@
|
||||
// Currently this test doesn't actually check the output of the functions.
|
||||
// It's only here for miri to check for any potential undefined behaviour.
|
||||
// TODO: check function results
|
||||
|
||||
#[test]
|
||||
fn test_transparent_wrapper() {
|
||||
// An external type defined in a different crate.
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
struct Foreign(u8);
|
||||
|
||||
use bytemuck::TransparentWrapper;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[repr(transparent)]
|
||||
struct Wrapper(Foreign);
|
||||
|
||||
unsafe impl TransparentWrapper<Foreign> for Wrapper {}
|
||||
|
||||
// Traits can be implemented on crate-local wrapper.
|
||||
unsafe impl bytemuck::Zeroable for Wrapper {}
|
||||
unsafe impl bytemuck::Pod for Wrapper {}
|
||||
|
||||
impl PartialEq<u8> for Foreign {
|
||||
fn eq(&self, &other: &u8) -> bool {
|
||||
self.0 == other
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<u8> for Wrapper {
|
||||
fn eq(&self, &other: &u8) -> bool {
|
||||
self.0 == other
|
||||
}
|
||||
}
|
||||
|
||||
let _: u8 = bytemuck::cast(Wrapper::wrap(Foreign::default()));
|
||||
let _: Foreign = Wrapper::peel(bytemuck::cast(u8::default()));
|
||||
|
||||
let _: &u8 = bytemuck::cast_ref(Wrapper::wrap_ref(&Foreign::default()));
|
||||
let _: &Foreign = Wrapper::peel_ref(bytemuck::cast_ref(&u8::default()));
|
||||
|
||||
let _: &mut u8 =
|
||||
bytemuck::cast_mut(Wrapper::wrap_mut(&mut Foreign::default()));
|
||||
let _: &mut Foreign =
|
||||
Wrapper::peel_mut(bytemuck::cast_mut(&mut u8::default()));
|
||||
|
||||
let _: &[u8] =
|
||||
bytemuck::cast_slice(Wrapper::wrap_slice(&[Foreign::default()]));
|
||||
let _: &[Foreign] =
|
||||
Wrapper::peel_slice(bytemuck::cast_slice(&[u8::default()]));
|
||||
|
||||
let _: &mut [u8] =
|
||||
bytemuck::cast_slice_mut(Wrapper::wrap_slice_mut(
|
||||
&mut [Foreign::default()],
|
||||
));
|
||||
let _: &mut [Foreign] =
|
||||
Wrapper::peel_slice_mut(bytemuck::cast_slice_mut(&mut [u8::default()]));
|
||||
|
||||
let _: &[u8] = bytemuck::bytes_of(Wrapper::wrap_ref(&Foreign::default()));
|
||||
let _: &Foreign = Wrapper::peel_ref(bytemuck::from_bytes(&[u8::default()]));
|
||||
|
||||
let _: &mut [u8] =
|
||||
bytemuck::bytes_of_mut(Wrapper::wrap_mut(&mut Foreign::default()));
|
||||
let _: &mut Foreign =
|
||||
Wrapper::peel_mut(bytemuck::from_bytes_mut(&mut [u8::default()]));
|
||||
|
||||
// not sure if this is the right usage
|
||||
let _ =
|
||||
bytemuck::pod_align_to::<_, u8>(Wrapper::wrap_slice(&[Foreign::default()]));
|
||||
// counterpart?
|
||||
|
||||
// not sure if this is the right usage
|
||||
let _ = bytemuck::pod_align_to_mut::<_, u8>(Wrapper::wrap_slice_mut(&mut [
|
||||
Foreign::default(),
|
||||
]));
|
||||
// counterpart?
|
||||
|
||||
#[cfg(feature = "extern_crate_alloc")]
|
||||
{
|
||||
use bytemuck::allocation::TransparentWrapperAlloc;
|
||||
use std::rc::Rc;
|
||||
|
||||
let a: Vec<Foreign> = vec![Foreign::default(); 2];
|
||||
|
||||
let b: Vec<Wrapper> = Wrapper::wrap_vec(a);
|
||||
assert_eq!(b, [0, 0]);
|
||||
|
||||
let c: Vec<Foreign> = Wrapper::peel_vec(b);
|
||||
assert_eq!(c, [0, 0]);
|
||||
|
||||
let d: Box<Foreign> = Box::new(Foreign::default());
|
||||
|
||||
let e: Box<Wrapper> = Wrapper::wrap_box(d);
|
||||
assert_eq!(&*e, &0);
|
||||
let f: Box<Foreign> = Wrapper::peel_box(e);
|
||||
assert_eq!(&*f, &0);
|
||||
|
||||
let g: Rc<Foreign> = Rc::new(Foreign::default());
|
||||
|
||||
let h: Rc<Wrapper> = Wrapper::wrap_rc(g);
|
||||
assert_eq!(&*h, &0);
|
||||
let i: Rc<Foreign> = Wrapper::peel_rc(h);
|
||||
assert_eq!(&*i, &0);
|
||||
|
||||
#[cfg(target_has_atomic = "ptr")]
|
||||
{
|
||||
use std::sync::Arc;
|
||||
|
||||
let j: Arc<Foreign> = Arc::new(Foreign::default());
|
||||
|
||||
let k: Arc<Wrapper> = Wrapper::wrap_arc(j);
|
||||
assert_eq!(&*k, &0);
|
||||
let l: Arc<Foreign> = Wrapper::peel_arc(k);
|
||||
assert_eq!(&*l, &0);
|
||||
}
|
||||
}
|
||||
}
|
13
third_party/rust/bytemuck/tests/wrapper_forgets.rs
vendored
Normal file
13
third_party/rust/bytemuck/tests/wrapper_forgets.rs
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
use bytemuck::TransparentWrapper;
|
||||
|
||||
#[repr(transparent)]
|
||||
struct Wrap(Box<u32>);
|
||||
|
||||
// SAFETY: it's #[repr(transparent)]
|
||||
unsafe impl TransparentWrapper<Box<u32>> for Wrap {}
|
||||
|
||||
fn main() {
|
||||
let value = Box::new(5);
|
||||
// This used to duplicate the wrapped value, creating a double free :(
|
||||
Wrap::wrap(value);
|
||||
}
|
1
third_party/rust/bytemuck_derive/.cargo-checksum.json
vendored
Normal file
1
third_party/rust/bytemuck_derive/.cargo-checksum.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"files":{"Cargo.toml":"c87eab9ea5fc46d5e449ddee9cf271d2e0bfe64a4972a8b593936a5845ebbaf1","LICENSE-APACHE":"870e20c217d15bcfcbe53d7c5867cd8fac44a4ca0b41fc1eb843557e16063eba","LICENSE-MIT":"0b2d108c9c686a74ac312990ee8377902756a2a081a7af3b0f9d68abf0a8f1a1","LICENSE-ZLIB":"682b4c81b85e83ce6cc6e1ace38fdd97aeb4de0e972bd2b44aa0916c54af8c96","README.md":"c44fcbb0a6555b948e7c0b26313ecdc5f3079ebd1ae74aadcc42fd1ba1245540","changelog.md":"70a32751e189a01bab0c2bc03f8cc11c0025469f29f68bd608aaf0f3ff6b90f4","src/lib.rs":"0c2941080a69a9ed6655fa3f94fdc85c5aeb6e7d8af080042cd7a2a9c5b80424","src/traits.rs":"a96d498f9e1d3050df1adfbcdd86a3258a900a325127699759b210bdb16a0617","tests/basic.rs":"980f46ba184d07b25de599e3de1d95a2a21e1270b3c9916a8046c71eabed5baf"},"package":"4da9a32f3fed317401fa3c862968128267c3106685286e15d5aaa3d7389c2f60"}
|
44
third_party/rust/bytemuck_derive/Cargo.toml
vendored
Normal file
44
third_party/rust/bytemuck_derive/Cargo.toml
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2018"
|
||||
name = "bytemuck_derive"
|
||||
version = "1.6.0"
|
||||
authors = ["Lokathor <zefria@gmail.com>"]
|
||||
description = "derive proc-macros for `bytemuck`"
|
||||
readme = "README.md"
|
||||
keywords = [
|
||||
"transmute",
|
||||
"bytes",
|
||||
"casting",
|
||||
]
|
||||
categories = [
|
||||
"encoding",
|
||||
"no-std",
|
||||
]
|
||||
license = "Zlib OR Apache-2.0 OR MIT"
|
||||
repository = "https://github.com/Lokathor/bytemuck"
|
||||
|
||||
[lib]
|
||||
name = "bytemuck_derive"
|
||||
proc-macro = true
|
||||
|
||||
[dependencies.proc-macro2]
|
||||
version = "1.0.60"
|
||||
|
||||
[dependencies.quote]
|
||||
version = "1"
|
||||
|
||||
[dependencies.syn]
|
||||
version = "2.0.1"
|
||||
|
||||
[dev-dependencies]
|
61
third_party/rust/bytemuck_derive/LICENSE-APACHE
vendored
Normal file
61
third_party/rust/bytemuck_derive/LICENSE-APACHE
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
9
third_party/rust/bytemuck_derive/LICENSE-MIT
vendored
Normal file
9
third_party/rust/bytemuck_derive/LICENSE-MIT
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Daniel "Lokathor" Gee.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
11
third_party/rust/bytemuck_derive/LICENSE-ZLIB
vendored
Normal file
11
third_party/rust/bytemuck_derive/LICENSE-ZLIB
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
Copyright (c) 2019 Daniel "Lokathor" Gee.
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source distribution.
|
10
third_party/rust/bytemuck_derive/README.md
vendored
Normal file
10
third_party/rust/bytemuck_derive/README.md
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
# bytemuck_derive
|
||||
|
||||
Derive macros for [bytemuck](https://docs.rs/bytemuck) traits.
|
||||
|
||||
MSRV: None!
|
||||
|
||||
This is an opt-in bonus feature for `bytemuck` that doesn't particularly do
|
||||
anything you couldn't do yourself, and so MSRV is not a strong consideration for
|
||||
this crate.
|
72
third_party/rust/bytemuck_derive/changelog.md
vendored
Normal file
72
third_party/rust/bytemuck_derive/changelog.md
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
|
||||
## `bytemuck_derive` changelog
|
||||
|
||||
## 1.6.0
|
||||
|
||||
* This allows `CheckedBitPattern` to be derived for enums with fields.
|
||||
The repr must be one of the following:
|
||||
* `#[repr(C)]`
|
||||
* `#[repr(C, int)]`
|
||||
* `#[repr(int)]`
|
||||
* `#[repr(transparent)]`
|
||||
|
||||
## 1.5.0
|
||||
|
||||
* The `Zeroable` derive now allows custom bounds. See the rustdoc for an explanation.
|
||||
|
||||
## 1.4.1
|
||||
|
||||
* Move the `syn` dependency to use version 2.
|
||||
This should not affect the public API in any way.
|
||||
|
||||
## 1.4.0
|
||||
|
||||
* `ByteEq` and `ByteHash` derives will make `Eq` and `Hash` impls that treat the
|
||||
value as a `&[u8]` during equality checks and hashing. This provides a large
|
||||
codegen improvement for some types.
|
||||
* Derives of `repr(int)` enums should now accept byte literal values as the
|
||||
discriminant.
|
||||
|
||||
## 1.3.0
|
||||
|
||||
* Allow `repr(transparent)` to be used generically in `derive(Pod)`.
|
||||
|
||||
## 1.2.1
|
||||
|
||||
* Fixed a regression of the `align(N)` attribute that occurred during otherwise
|
||||
routine cleanup.
|
||||
|
||||
## 1.2.0
|
||||
|
||||
* Apparently our minimum required version of `syn` went up without anyone
|
||||
noticing for a while. Because of a bump in our `syn` requirements, we're also
|
||||
issuing this minor version bump in the `bytemuck_derive` crate. Because it's
|
||||
possible to *reduce* the minimum required version of a dep in only a patch
|
||||
release, I'm going to ratchet the required version of `syn` all the way up to
|
||||
"current" (1.0.99). If absolutely necessary we could probably reduce the
|
||||
minimum `syn` version again in a patch release for 1.2, but I don't want to
|
||||
play this dance too much so I'd rather make each jump as big as can possibly
|
||||
be. [Issue 122](https://github.com/Lokathor/bytemuck/issues/122). **Note:**
|
||||
While the core `bytemuck` crate continues to keep building on rustc-1.34.0,
|
||||
the `bytemuck_derive` crate is considered an opt-in bonus feature (which
|
||||
doesn't do anything you couldn't trivially do yourself) and so it does not
|
||||
support a specific MSRV.
|
||||
|
||||
## 1.1.1
|
||||
|
||||
* Adjusted the license files to use full files rather than symlinks.
|
||||
[PR](https://github.com/Lokathor/bytemuck/pull/118)
|
||||
The license is unchanged, just no more symlinks.
|
||||
|
||||
## 1.1.0
|
||||
|
||||
* Updated to work with `bytemuck-1.9.0`
|
||||
|
||||
## 1.0.1
|
||||
|
||||
* [yanchith](https://github.com/yanchith) fixed the derive checks code to make clippy more happy.
|
||||
[PR 45](https://github.com/Lokathor/bytemuck/pull/45)
|
||||
|
||||
## 1.0.0
|
||||
|
||||
* Initial stable release.
|
632
third_party/rust/bytemuck_derive/src/lib.rs
vendored
Normal file
632
third_party/rust/bytemuck_derive/src/lib.rs
vendored
Normal file
@ -0,0 +1,632 @@
|
||||
//! Derive macros for [bytemuck](https://docs.rs/bytemuck) traits.
|
||||
|
||||
extern crate proc_macro;
|
||||
|
||||
mod traits;
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, DeriveInput, Result};
|
||||
|
||||
use crate::traits::{
|
||||
bytemuck_crate_name, AnyBitPattern, CheckedBitPattern, Contiguous, Derivable,
|
||||
NoUninit, Pod, TransparentWrapper, Zeroable,
|
||||
};
|
||||
|
||||
/// Derive the `Pod` trait for a struct
|
||||
///
|
||||
/// The macro ensures that the struct follows all the the safety requirements
|
||||
/// for the `Pod` trait.
|
||||
///
|
||||
/// The following constraints need to be satisfied for the macro to succeed
|
||||
///
|
||||
/// - All fields in the struct must implement `Pod`
|
||||
/// - The struct must be `#[repr(C)]` or `#[repr(transparent)]`
|
||||
/// - The struct must not contain any padding bytes
|
||||
/// - The struct contains no generic parameters, if it is not
|
||||
/// `#[repr(transparent)]`
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// # use std::marker::PhantomData;
|
||||
/// # use bytemuck_derive::{Pod, Zeroable};
|
||||
/// #[derive(Copy, Clone, Pod, Zeroable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Test {
|
||||
/// a: u16,
|
||||
/// b: u16,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Copy, Clone, Pod, Zeroable)]
|
||||
/// #[repr(transparent)]
|
||||
/// struct Generic<A, B> {
|
||||
/// a: A,
|
||||
/// b: PhantomData<B>,
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// If the struct is generic, it must be `#[repr(transparent)]` also.
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// # use bytemuck::{Pod, Zeroable};
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #[derive(Copy, Clone, Pod, Zeroable)]
|
||||
/// #[repr(C)] // must be `#[repr(transparent)]`
|
||||
/// struct Generic<A> {
|
||||
/// a: A,
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// If the struct is generic and `#[repr(transparent)]`, then it is only `Pod`
|
||||
/// when all of its generics are `Pod`, not just its fields.
|
||||
///
|
||||
/// ```
|
||||
/// # use bytemuck::{Pod, Zeroable};
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #[derive(Copy, Clone, Pod, Zeroable)]
|
||||
/// #[repr(transparent)]
|
||||
/// struct Generic<A, B> {
|
||||
/// a: A,
|
||||
/// b: PhantomData<B>,
|
||||
/// }
|
||||
///
|
||||
/// let _: u32 = bytemuck::cast(Generic { a: 4u32, b: PhantomData::<u32> });
|
||||
/// ```
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// # use bytemuck::{Pod, Zeroable};
|
||||
/// # use std::marker::PhantomData;
|
||||
/// # #[derive(Copy, Clone, Pod, Zeroable)]
|
||||
/// # #[repr(transparent)]
|
||||
/// # struct Generic<A, B> {
|
||||
/// # a: A,
|
||||
/// # b: PhantomData<B>,
|
||||
/// # }
|
||||
/// struct NotPod;
|
||||
///
|
||||
/// let _: u32 = bytemuck::cast(Generic { a: 4u32, b: PhantomData::<NotPod> });
|
||||
/// ```
|
||||
#[proc_macro_derive(Pod, attributes(bytemuck))]
|
||||
pub fn derive_pod(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let expanded =
|
||||
derive_marker_trait::<Pod>(parse_macro_input!(input as DeriveInput));
|
||||
|
||||
proc_macro::TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Derive the `AnyBitPattern` trait for a struct
|
||||
///
|
||||
/// The macro ensures that the struct follows all the the safety requirements
|
||||
/// for the `AnyBitPattern` trait.
|
||||
///
|
||||
/// The following constraints need to be satisfied for the macro to succeed
|
||||
///
|
||||
/// - All fields in the struct must to implement `AnyBitPattern`
|
||||
#[proc_macro_derive(AnyBitPattern, attributes(bytemuck))]
|
||||
pub fn derive_anybitpattern(
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let expanded = derive_marker_trait::<AnyBitPattern>(parse_macro_input!(
|
||||
input as DeriveInput
|
||||
));
|
||||
|
||||
proc_macro::TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Derive the `Zeroable` trait for a struct
|
||||
///
|
||||
/// The macro ensures that the struct follows all the the safety requirements
|
||||
/// for the `Zeroable` trait.
|
||||
///
|
||||
/// The following constraints need to be satisfied for the macro to succeed
|
||||
///
|
||||
/// - All fields in the struct must to implement `Zeroable`
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck_derive::{Zeroable};
|
||||
/// #[derive(Copy, Clone, Zeroable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Test {
|
||||
/// a: u16,
|
||||
/// b: u16,
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Custom bounds
|
||||
///
|
||||
/// Custom bounds for the derived `Zeroable` impl can be given using the
|
||||
/// `#[zeroable(bound = "")]` helper attribute.
|
||||
///
|
||||
/// Using this attribute additionally opts-in to "perfect derive" semantics,
|
||||
/// where instead of adding bounds for each generic type parameter, bounds are
|
||||
/// added for each field's type.
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck::Zeroable;
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #[derive(Clone, Zeroable)]
|
||||
/// #[zeroable(bound = "")]
|
||||
/// struct AlwaysZeroable<T> {
|
||||
/// a: PhantomData<T>,
|
||||
/// }
|
||||
///
|
||||
/// AlwaysZeroable::<std::num::NonZeroU8>::zeroed();
|
||||
/// ```
|
||||
///
|
||||
/// ```rust,compile_fail
|
||||
/// # use bytemuck::Zeroable;
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #[derive(Clone, Zeroable)]
|
||||
/// #[zeroable(bound = "T: Copy")]
|
||||
/// struct ZeroableWhenTIsCopy<T> {
|
||||
/// a: PhantomData<T>,
|
||||
/// }
|
||||
///
|
||||
/// ZeroableWhenTIsCopy::<String>::zeroed();
|
||||
/// ```
|
||||
///
|
||||
/// The restriction that all fields must be Zeroable is still applied, and this
|
||||
/// is enforced using the mentioned "perfect derive" semantics.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck::Zeroable;
|
||||
/// #[derive(Clone, Zeroable)]
|
||||
/// #[zeroable(bound = "")]
|
||||
/// struct ZeroableWhenTIsZeroable<T> {
|
||||
/// a: T,
|
||||
/// }
|
||||
/// ZeroableWhenTIsZeroable::<u32>::zeroed();
|
||||
/// ```
|
||||
///
|
||||
/// ```rust,compile_fail
|
||||
/// # use bytemuck::Zeroable;
|
||||
/// # #[derive(Clone, Zeroable)]
|
||||
/// # #[zeroable(bound = "")]
|
||||
/// # struct ZeroableWhenTIsZeroable<T> {
|
||||
/// # a: T,
|
||||
/// # }
|
||||
/// ZeroableWhenTIsZeroable::<String>::zeroed();
|
||||
/// ```
|
||||
#[proc_macro_derive(Zeroable, attributes(bytemuck, zeroable))]
|
||||
pub fn derive_zeroable(
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let expanded =
|
||||
derive_marker_trait::<Zeroable>(parse_macro_input!(input as DeriveInput));
|
||||
|
||||
proc_macro::TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Derive the `NoUninit` trait for a struct or enum
|
||||
///
|
||||
/// The macro ensures that the type follows all the the safety requirements
|
||||
/// for the `NoUninit` trait.
|
||||
///
|
||||
/// The following constraints need to be satisfied for the macro to succeed
|
||||
/// (the rest of the constraints are guaranteed by the `NoUninit` subtrait
|
||||
/// bounds, i.e. the type must be `Sized + Copy + 'static`):
|
||||
///
|
||||
/// If applied to a struct:
|
||||
/// - All fields in the struct must implement `NoUninit`
|
||||
/// - The struct must be `#[repr(C)]` or `#[repr(transparent)]`
|
||||
/// - The struct must not contain any padding bytes
|
||||
/// - The struct must contain no generic parameters
|
||||
///
|
||||
/// If applied to an enum:
|
||||
/// - The enum must be explicit `#[repr(Int)]`, `#[repr(C)]`, or both
|
||||
/// - All variants must be fieldless
|
||||
/// - The enum must contain no generic parameters
|
||||
#[proc_macro_derive(NoUninit)]
|
||||
pub fn derive_no_uninit(
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let expanded =
|
||||
derive_marker_trait::<NoUninit>(parse_macro_input!(input as DeriveInput));
|
||||
|
||||
proc_macro::TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Derive the `CheckedBitPattern` trait for a struct or enum.
|
||||
///
|
||||
/// The macro ensures that the type follows all the the safety requirements
|
||||
/// for the `CheckedBitPattern` trait and derives the required `Bits` type
|
||||
/// definition and `is_valid_bit_pattern` method for the type automatically.
|
||||
///
|
||||
/// The following constraints need to be satisfied for the macro to succeed:
|
||||
///
|
||||
/// If applied to a struct:
|
||||
/// - All fields must implement `CheckedBitPattern`
|
||||
/// - The struct must be `#[repr(C)]` or `#[repr(transparent)]`
|
||||
/// - The struct must contain no generic parameters
|
||||
///
|
||||
/// If applied to an enum:
|
||||
/// - The enum must be explicit `#[repr(Int)]`
|
||||
/// - All fields in variants must implement `CheckedBitPattern`
|
||||
/// - The enum must contain no generic parameters
|
||||
#[proc_macro_derive(CheckedBitPattern)]
|
||||
pub fn derive_maybe_pod(
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let expanded = derive_marker_trait::<CheckedBitPattern>(parse_macro_input!(
|
||||
input as DeriveInput
|
||||
));
|
||||
|
||||
proc_macro::TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Derive the `TransparentWrapper` trait for a struct
|
||||
///
|
||||
/// The macro ensures that the struct follows all the the safety requirements
|
||||
/// for the `TransparentWrapper` trait.
|
||||
///
|
||||
/// The following constraints need to be satisfied for the macro to succeed
|
||||
///
|
||||
/// - The struct must be `#[repr(transparent)]`
|
||||
/// - The struct must contain the `Wrapped` type
|
||||
/// - Any ZST fields must be [`Zeroable`][derive@Zeroable].
|
||||
///
|
||||
/// If the struct only contains a single field, the `Wrapped` type will
|
||||
/// automatically be determined. If there is more then one field in the struct,
|
||||
/// you need to specify the `Wrapped` type using `#[transparent(T)]`
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck_derive::TransparentWrapper;
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #[derive(Copy, Clone, TransparentWrapper)]
|
||||
/// #[repr(transparent)]
|
||||
/// #[transparent(u16)]
|
||||
/// struct Test<T> {
|
||||
/// inner: u16,
|
||||
/// extra: PhantomData<T>,
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// If the struct contains more than one field, the `Wrapped` type must be
|
||||
/// explicitly specified.
|
||||
///
|
||||
/// ```rust,compile_fail
|
||||
/// # use bytemuck_derive::TransparentWrapper;
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #[derive(Copy, Clone, TransparentWrapper)]
|
||||
/// #[repr(transparent)]
|
||||
/// // missing `#[transparent(u16)]`
|
||||
/// struct Test<T> {
|
||||
/// inner: u16,
|
||||
/// extra: PhantomData<T>,
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Any ZST fields must be `Zeroable`.
|
||||
///
|
||||
/// ```rust,compile_fail
|
||||
/// # use bytemuck_derive::TransparentWrapper;
|
||||
/// # use std::marker::PhantomData;
|
||||
/// struct NonTransparentSafeZST;
|
||||
///
|
||||
/// #[derive(TransparentWrapper)]
|
||||
/// #[repr(transparent)]
|
||||
/// #[transparent(u16)]
|
||||
/// struct Test<T> {
|
||||
/// inner: u16,
|
||||
/// extra: PhantomData<T>,
|
||||
/// another_extra: NonTransparentSafeZST, // not `Zeroable`
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_derive(TransparentWrapper, attributes(bytemuck, transparent))]
|
||||
pub fn derive_transparent(
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let expanded = derive_marker_trait::<TransparentWrapper>(parse_macro_input!(
|
||||
input as DeriveInput
|
||||
));
|
||||
|
||||
proc_macro::TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Derive the `Contiguous` trait for an enum
|
||||
///
|
||||
/// The macro ensures that the enum follows all the the safety requirements
|
||||
/// for the `Contiguous` trait.
|
||||
///
|
||||
/// The following constraints need to be satisfied for the macro to succeed
|
||||
///
|
||||
/// - The enum must be `#[repr(Int)]`
|
||||
/// - The enum must be fieldless
|
||||
/// - The enum discriminants must form a contiguous range
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck_derive::{Contiguous};
|
||||
///
|
||||
/// #[derive(Copy, Clone, Contiguous)]
|
||||
/// #[repr(u8)]
|
||||
/// enum Test {
|
||||
/// A = 0,
|
||||
/// B = 1,
|
||||
/// C = 2,
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_derive(Contiguous)]
|
||||
pub fn derive_contiguous(
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let expanded =
|
||||
derive_marker_trait::<Contiguous>(parse_macro_input!(input as DeriveInput));
|
||||
|
||||
proc_macro::TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Derive the `PartialEq` and `Eq` trait for a type
|
||||
///
|
||||
/// The macro implements `PartialEq` and `Eq` by casting both sides of the
|
||||
/// comparison to a byte slice and then compares those.
|
||||
///
|
||||
/// ## Warning
|
||||
///
|
||||
/// Since this implements a byte wise comparison, the behavior of floating point
|
||||
/// numbers does not match their usual comparison behavior. Additionally other
|
||||
/// custom comparison behaviors of the individual fields are also ignored. This
|
||||
/// also does not implement `StructuralPartialEq` / `StructuralEq` like
|
||||
/// `PartialEq` / `Eq` would. This means you can't pattern match on the values.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck_derive::{ByteEq, NoUninit};
|
||||
/// #[derive(Copy, Clone, NoUninit, ByteEq)]
|
||||
/// #[repr(C)]
|
||||
/// struct Test {
|
||||
/// a: u32,
|
||||
/// b: char,
|
||||
/// c: f32,
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_derive(ByteEq)]
|
||||
pub fn derive_byte_eq(
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let crate_name = bytemuck_crate_name(&input);
|
||||
let ident = input.ident;
|
||||
|
||||
proc_macro::TokenStream::from(quote! {
|
||||
impl ::core::cmp::PartialEq for #ident {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
#crate_name::bytes_of(self) == #crate_name::bytes_of(other)
|
||||
}
|
||||
}
|
||||
impl ::core::cmp::Eq for #ident { }
|
||||
})
|
||||
}
|
||||
|
||||
/// Derive the `Hash` trait for a type
|
||||
///
|
||||
/// The macro implements `Hash` by casting the value to a byte slice and hashing
|
||||
/// that.
|
||||
///
|
||||
/// ## Warning
|
||||
///
|
||||
/// The hash does not match the standard library's `Hash` derive.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use bytemuck_derive::{ByteHash, NoUninit};
|
||||
/// #[derive(Copy, Clone, NoUninit, ByteHash)]
|
||||
/// #[repr(C)]
|
||||
/// struct Test {
|
||||
/// a: u32,
|
||||
/// b: char,
|
||||
/// c: f32,
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_derive(ByteHash)]
|
||||
pub fn derive_byte_hash(
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let crate_name = bytemuck_crate_name(&input);
|
||||
let ident = input.ident;
|
||||
|
||||
proc_macro::TokenStream::from(quote! {
|
||||
impl ::core::hash::Hash for #ident {
|
||||
#[inline]
|
||||
fn hash<H: ::core::hash::Hasher>(&self, state: &mut H) {
|
||||
::core::hash::Hash::hash_slice(#crate_name::bytes_of(self), state)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn hash_slice<H: ::core::hash::Hasher>(data: &[Self], state: &mut H) {
|
||||
::core::hash::Hash::hash_slice(#crate_name::cast_slice::<_, u8>(data), state)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Basic wrapper for error handling
|
||||
fn derive_marker_trait<Trait: Derivable>(input: DeriveInput) -> TokenStream {
|
||||
derive_marker_trait_inner::<Trait>(input)
|
||||
.unwrap_or_else(|err| err.into_compile_error())
|
||||
}
|
||||
|
||||
/// Find `#[name(key = "value")]` helper attributes on the struct, and return
|
||||
/// their `"value"`s parsed with `parser`.
|
||||
///
|
||||
/// Returns an error if any attributes with the given `name` do not match the
|
||||
/// expected format. Returns `Ok([])` if no attributes with `name` are found.
|
||||
fn find_and_parse_helper_attributes<P: syn::parse::Parser + Copy>(
|
||||
attributes: &[syn::Attribute], name: &str, key: &str, parser: P,
|
||||
example_value: &str, invalid_value_msg: &str,
|
||||
) -> Result<Vec<P::Output>> {
|
||||
let invalid_format_msg =
|
||||
format!("{name} attribute must be `{name}({key} = \"{example_value}\")`",);
|
||||
let values_to_check = attributes.iter().filter_map(|attr| match &attr.meta {
|
||||
// If a `Path` matches our `name`, return an error, else ignore it.
|
||||
// e.g. `#[zeroable]`
|
||||
syn::Meta::Path(path) => path
|
||||
.is_ident(name)
|
||||
.then(|| Err(syn::Error::new_spanned(path, &invalid_format_msg))),
|
||||
// If a `NameValue` matches our `name`, return an error, else ignore it.
|
||||
// e.g. `#[zeroable = "hello"]`
|
||||
syn::Meta::NameValue(namevalue) => {
|
||||
namevalue.path.is_ident(name).then(|| {
|
||||
Err(syn::Error::new_spanned(&namevalue.path, &invalid_format_msg))
|
||||
})
|
||||
}
|
||||
// If a `List` matches our `name`, match its contents to our format, else
|
||||
// ignore it. If its contents match our format, return the value, else
|
||||
// return an error.
|
||||
syn::Meta::List(list) => list.path.is_ident(name).then(|| {
|
||||
let namevalue: syn::MetaNameValue = syn::parse2(list.tokens.clone())
|
||||
.map_err(|_| {
|
||||
syn::Error::new_spanned(&list.tokens, &invalid_format_msg)
|
||||
})?;
|
||||
if namevalue.path.is_ident(key) {
|
||||
match namevalue.value {
|
||||
syn::Expr::Lit(syn::ExprLit {
|
||||
lit: syn::Lit::Str(strlit), ..
|
||||
}) => Ok(strlit),
|
||||
_ => {
|
||||
Err(syn::Error::new_spanned(&namevalue.path, &invalid_format_msg))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Err(syn::Error::new_spanned(&namevalue.path, &invalid_format_msg))
|
||||
}
|
||||
}),
|
||||
});
|
||||
// Parse each value found with the given parser, and return them if no errors
|
||||
// occur.
|
||||
values_to_check
|
||||
.map(|lit| {
|
||||
let lit = lit?;
|
||||
lit.parse_with(parser).map_err(|err| {
|
||||
syn::Error::new_spanned(&lit, format!("{invalid_value_msg}: {err}"))
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn derive_marker_trait_inner<Trait: Derivable>(
|
||||
mut input: DeriveInput,
|
||||
) -> Result<TokenStream> {
|
||||
let crate_name = bytemuck_crate_name(&input);
|
||||
let trait_ = Trait::ident(&input, &crate_name)?;
|
||||
// If this trait allows explicit bounds, and any explicit bounds were given,
|
||||
// then use those explicit bounds. Else, apply the default bounds (bound
|
||||
// each generic type on this trait).
|
||||
if let Some(name) = Trait::explicit_bounds_attribute_name() {
|
||||
// See if any explicit bounds were given in attributes.
|
||||
let explicit_bounds = find_and_parse_helper_attributes(
|
||||
&input.attrs,
|
||||
name,
|
||||
"bound",
|
||||
<syn::punctuated::Punctuated<syn::WherePredicate, syn::Token![,]>>::parse_terminated,
|
||||
"Type: Trait",
|
||||
"invalid where predicate",
|
||||
)?;
|
||||
|
||||
if !explicit_bounds.is_empty() {
|
||||
// Explicit bounds were given.
|
||||
// Enforce explicitly given bounds, and emit "perfect derive" (i.e. add
|
||||
// bounds for each field's type).
|
||||
let explicit_bounds = explicit_bounds
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect::<Vec<syn::WherePredicate>>();
|
||||
|
||||
let predicates = &mut input.generics.make_where_clause().predicates;
|
||||
|
||||
predicates.extend(explicit_bounds);
|
||||
|
||||
let fields = match &input.data {
|
||||
syn::Data::Struct(syn::DataStruct { fields, .. }) => fields.clone(),
|
||||
syn::Data::Union(_) => {
|
||||
return Err(syn::Error::new_spanned(
|
||||
trait_,
|
||||
&"perfect derive is not supported for unions",
|
||||
));
|
||||
}
|
||||
syn::Data::Enum(_) => {
|
||||
return Err(syn::Error::new_spanned(
|
||||
trait_,
|
||||
&"perfect derive is not supported for enums",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
for field in fields {
|
||||
let ty = field.ty;
|
||||
predicates.push(syn::parse_quote!(
|
||||
#ty: #trait_
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// No explicit bounds were given.
|
||||
// Enforce trait bound on all type generics.
|
||||
add_trait_marker(&mut input.generics, &trait_);
|
||||
}
|
||||
} else {
|
||||
// This trait does not allow explicit bounds.
|
||||
// Enforce trait bound on all type generics.
|
||||
add_trait_marker(&mut input.generics, &trait_);
|
||||
}
|
||||
|
||||
let name = &input.ident;
|
||||
|
||||
let (impl_generics, ty_generics, where_clause) =
|
||||
input.generics.split_for_impl();
|
||||
|
||||
Trait::check_attributes(&input.data, &input.attrs)?;
|
||||
let asserts = Trait::asserts(&input, &crate_name)?;
|
||||
let (trait_impl_extras, trait_impl) = Trait::trait_impl(&input, &crate_name)?;
|
||||
|
||||
let implies_trait = if let Some(implies_trait) =
|
||||
Trait::implies_trait(&crate_name)
|
||||
{
|
||||
quote!(unsafe impl #impl_generics #implies_trait for #name #ty_generics #where_clause {})
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
|
||||
let where_clause =
|
||||
if Trait::requires_where_clause() { where_clause } else { None };
|
||||
|
||||
Ok(quote! {
|
||||
#asserts
|
||||
|
||||
#trait_impl_extras
|
||||
|
||||
unsafe impl #impl_generics #trait_ for #name #ty_generics #where_clause {
|
||||
#trait_impl
|
||||
}
|
||||
|
||||
#implies_trait
|
||||
})
|
||||
}
|
||||
|
||||
/// Add a trait marker to the generics if it is not already present
|
||||
fn add_trait_marker(generics: &mut syn::Generics, trait_name: &syn::Path) {
|
||||
// Get each generic type parameter.
|
||||
let type_params = generics
|
||||
.type_params()
|
||||
.map(|param| ¶m.ident)
|
||||
.map(|param| {
|
||||
syn::parse_quote!(
|
||||
#param: #trait_name
|
||||
)
|
||||
})
|
||||
.collect::<Vec<syn::WherePredicate>>();
|
||||
|
||||
generics.make_where_clause().predicates.extend(type_params);
|
||||
}
|
1265
third_party/rust/bytemuck_derive/src/traits.rs
vendored
Normal file
1265
third_party/rust/bytemuck_derive/src/traits.rs
vendored
Normal file
File diff suppressed because it is too large
Load Diff
452
third_party/rust/bytemuck_derive/tests/basic.rs
vendored
Normal file
452
third_party/rust/bytemuck_derive/tests/basic.rs
vendored
Normal file
@ -0,0 +1,452 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use bytemuck::{
|
||||
AnyBitPattern, CheckedBitPattern, Contiguous, NoUninit, Pod,
|
||||
TransparentWrapper, Zeroable, checked::CheckedCastError,
|
||||
};
|
||||
use std::marker::{PhantomData, PhantomPinned};
|
||||
|
||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
struct Test {
|
||||
a: u16,
|
||||
b: u16,
|
||||
}
|
||||
|
||||
#[derive(Pod, Zeroable)]
|
||||
#[repr(C, packed)]
|
||||
struct GenericPackedStruct<T: Pod> {
|
||||
a: u32,
|
||||
b: T,
|
||||
c: u32,
|
||||
}
|
||||
|
||||
impl<T: Pod> Clone for GenericPackedStruct<T> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Pod> Copy for GenericPackedStruct<T> {}
|
||||
|
||||
#[derive(Pod, Zeroable)]
|
||||
#[repr(C, packed(1))]
|
||||
struct GenericPackedStructExplicitPackedAlignment<T: Pod> {
|
||||
a: u32,
|
||||
b: T,
|
||||
c: u32,
|
||||
}
|
||||
|
||||
impl<T: Pod> Clone for GenericPackedStructExplicitPackedAlignment<T> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Pod> Copy for GenericPackedStructExplicitPackedAlignment<T> {}
|
||||
|
||||
#[derive(Zeroable)]
|
||||
struct ZeroGeneric<T: bytemuck::Zeroable> {
|
||||
a: T,
|
||||
}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
struct TransparentSingle {
|
||||
a: u16,
|
||||
}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
#[transparent(u16)]
|
||||
struct TransparentWithZeroSized<T> {
|
||||
a: u16,
|
||||
b: PhantomData<T>,
|
||||
}
|
||||
|
||||
struct MyZst<T>(PhantomData<T>, [u8; 0], PhantomPinned);
|
||||
unsafe impl<T> Zeroable for MyZst<T> {}
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
#[transparent(u16)]
|
||||
struct TransparentTupleWithCustomZeroSized<T>(u16, MyZst<T>);
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Contiguous)]
|
||||
enum ContiguousWithValues {
|
||||
A = 0,
|
||||
B = 1,
|
||||
C = 2,
|
||||
D = 3,
|
||||
E = 4,
|
||||
}
|
||||
|
||||
#[repr(i8)]
|
||||
#[derive(Clone, Copy, Contiguous)]
|
||||
enum ContiguousWithImplicitValues {
|
||||
A = -10,
|
||||
B,
|
||||
C,
|
||||
D,
|
||||
E,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, NoUninit)]
|
||||
#[repr(C)]
|
||||
struct NoUninitTest {
|
||||
a: u16,
|
||||
b: u16,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, AnyBitPattern)]
|
||||
#[repr(C)]
|
||||
union UnionTestAnyBitPattern {
|
||||
a: u8,
|
||||
b: u16,
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, NoUninit, CheckedBitPattern, PartialEq, Eq)]
|
||||
enum CheckedBitPatternEnumWithValues {
|
||||
A = 0,
|
||||
B = 1,
|
||||
C = 2,
|
||||
D = 3,
|
||||
E = 4,
|
||||
}
|
||||
|
||||
#[repr(i8)]
|
||||
#[derive(Clone, Copy, NoUninit, CheckedBitPattern)]
|
||||
enum CheckedBitPatternEnumWithImplicitValues {
|
||||
A = -10,
|
||||
B,
|
||||
C,
|
||||
D,
|
||||
E,
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, NoUninit, CheckedBitPattern, PartialEq, Eq)]
|
||||
enum CheckedBitPatternEnumNonContiguous {
|
||||
A = 1,
|
||||
B = 8,
|
||||
C = 2,
|
||||
D = 3,
|
||||
E = 56,
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, NoUninit, CheckedBitPattern, PartialEq, Eq)]
|
||||
enum CheckedBitPatternEnumByteLit {
|
||||
A = b'A',
|
||||
B = b'B',
|
||||
C = b'C',
|
||||
D = b'D',
|
||||
E = b'E',
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, NoUninit, CheckedBitPattern, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
struct CheckedBitPatternStruct {
|
||||
a: u8,
|
||||
b: CheckedBitPatternEnumNonContiguous,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, AnyBitPattern, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
struct AnyBitPatternTest<A: AnyBitPattern, B: AnyBitPattern> {
|
||||
a: A,
|
||||
b: B,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, CheckedBitPattern)]
|
||||
#[repr(C, align(8))]
|
||||
struct CheckedBitPatternAlignedStruct {
|
||||
a: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
enum CheckedBitPatternCDefaultDiscriminantEnumWithFields {
|
||||
A(u64),
|
||||
B { c: u64 },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)]
|
||||
#[repr(C, u8)]
|
||||
enum CheckedBitPatternCEnumWithFields {
|
||||
A(u32),
|
||||
B { c: u32 },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
enum CheckedBitPatternIntEnumWithFields {
|
||||
A(u8),
|
||||
B { c: u32 },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)]
|
||||
#[repr(transparent)]
|
||||
enum CheckedBitPatternTransparentEnumWithFields {
|
||||
A { b: u32 },
|
||||
}
|
||||
|
||||
// size 24, align 8.
|
||||
// first byte always the u8 discriminant, then 7 bytes of padding until the payload union since the align of the payload
|
||||
// is the greatest of the align of all the variants, which is 8 (from CheckedBitPatternCDefaultDiscriminantEnumWithFields)
|
||||
#[derive(Debug, Clone, Copy, CheckedBitPattern, PartialEq, Eq)]
|
||||
#[repr(C, u8)]
|
||||
enum CheckedBitPatternEnumNested {
|
||||
A(CheckedBitPatternCEnumWithFields),
|
||||
B(CheckedBitPatternCDefaultDiscriminantEnumWithFields),
|
||||
}
|
||||
|
||||
/// ```compile_fail
|
||||
/// use bytemuck::{Pod, Zeroable};
|
||||
///
|
||||
/// #[derive(Pod, Zeroable)]
|
||||
/// #[repr(transparent)]
|
||||
/// struct TransparentSingle<T>(T);
|
||||
///
|
||||
/// struct NotPod(u32);
|
||||
///
|
||||
/// let _: u32 = bytemuck::cast(TransparentSingle(NotPod(0u32)));
|
||||
/// ```
|
||||
#[derive(
|
||||
Debug, Copy, Clone, PartialEq, Eq, Pod, Zeroable, TransparentWrapper,
|
||||
)]
|
||||
#[repr(transparent)]
|
||||
struct NewtypeWrapperTest<T>(T);
|
||||
|
||||
#[test]
|
||||
fn fails_cast_contiguous() {
|
||||
let can_cast = CheckedBitPatternEnumWithValues::is_valid_bit_pattern(&5);
|
||||
assert!(!can_cast);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_cast_contiguous() {
|
||||
let res =
|
||||
bytemuck::checked::from_bytes::<CheckedBitPatternEnumWithValues>(&[2u8]);
|
||||
assert_eq!(*res, CheckedBitPatternEnumWithValues::C);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fails_cast_noncontiguous() {
|
||||
let can_cast = CheckedBitPatternEnumNonContiguous::is_valid_bit_pattern(&4);
|
||||
assert!(!can_cast);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_cast_noncontiguous() {
|
||||
let res =
|
||||
bytemuck::checked::from_bytes::<CheckedBitPatternEnumNonContiguous>(&[
|
||||
56u8,
|
||||
]);
|
||||
assert_eq!(*res, CheckedBitPatternEnumNonContiguous::E);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fails_cast_bytelit() {
|
||||
let can_cast = CheckedBitPatternEnumByteLit::is_valid_bit_pattern(&b'a');
|
||||
assert!(!can_cast);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_cast_bytelit() {
|
||||
let res =
|
||||
bytemuck::checked::cast_slice::<u8, CheckedBitPatternEnumByteLit>(b"CAB");
|
||||
assert_eq!(
|
||||
res,
|
||||
[
|
||||
CheckedBitPatternEnumByteLit::C,
|
||||
CheckedBitPatternEnumByteLit::A,
|
||||
CheckedBitPatternEnumByteLit::B
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fails_cast_struct() {
|
||||
let pod = [0u8, 24u8];
|
||||
let res = bytemuck::checked::try_from_bytes::<CheckedBitPatternStruct>(&pod);
|
||||
assert!(res.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_cast_struct() {
|
||||
let pod = [0u8, 8u8];
|
||||
let res = bytemuck::checked::from_bytes::<CheckedBitPatternStruct>(&pod);
|
||||
assert_eq!(
|
||||
*res,
|
||||
CheckedBitPatternStruct { a: 0, b: CheckedBitPatternEnumNonContiguous::B }
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anybitpattern_implies_zeroable() {
|
||||
let test = AnyBitPatternTest::<isize, usize>::zeroed();
|
||||
assert_eq!(test, AnyBitPatternTest { a: 0isize, b: 0usize });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkedbitpattern_try_pod_read_unaligned() {
|
||||
let pod = [0u8];
|
||||
let res = bytemuck::checked::try_pod_read_unaligned::<
|
||||
CheckedBitPatternEnumWithValues,
|
||||
>(&pod);
|
||||
assert!(res.is_ok());
|
||||
|
||||
let pod = [5u8];
|
||||
let res = bytemuck::checked::try_pod_read_unaligned::<
|
||||
CheckedBitPatternEnumWithValues,
|
||||
>(&pod);
|
||||
assert!(res.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkedbitpattern_aligned_struct() {
|
||||
let pod = [0u8; 8];
|
||||
bytemuck::checked::pod_read_unaligned::<CheckedBitPatternAlignedStruct>(&pod);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkedbitpattern_c_default_discriminant_enum_with_fields() {
|
||||
let pod = [
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0x55,
|
||||
0x55, 0x55, 0x55, 0xcc,
|
||||
];
|
||||
let value = bytemuck::checked::pod_read_unaligned::<
|
||||
CheckedBitPatternCDefaultDiscriminantEnumWithFields,
|
||||
>(&pod);
|
||||
assert_eq!(
|
||||
value,
|
||||
CheckedBitPatternCDefaultDiscriminantEnumWithFields::A(0xcc555555555555cc)
|
||||
);
|
||||
|
||||
let pod = [
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0x55,
|
||||
0x55, 0x55, 0x55, 0xcc,
|
||||
];
|
||||
let value = bytemuck::checked::pod_read_unaligned::<
|
||||
CheckedBitPatternCDefaultDiscriminantEnumWithFields,
|
||||
>(&pod);
|
||||
assert_eq!(
|
||||
value,
|
||||
CheckedBitPatternCDefaultDiscriminantEnumWithFields::B {
|
||||
c: 0xcc555555555555cc
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkedbitpattern_c_enum_with_fields() {
|
||||
let pod = [0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0xcc];
|
||||
let value = bytemuck::checked::pod_read_unaligned::<
|
||||
CheckedBitPatternCEnumWithFields,
|
||||
>(&pod);
|
||||
assert_eq!(value, CheckedBitPatternCEnumWithFields::A(0xcc5555cc));
|
||||
|
||||
let pod = [0x01, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0xcc];
|
||||
let value = bytemuck::checked::pod_read_unaligned::<
|
||||
CheckedBitPatternCEnumWithFields,
|
||||
>(&pod);
|
||||
assert_eq!(value, CheckedBitPatternCEnumWithFields::B { c: 0xcc5555cc });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkedbitpattern_int_enum_with_fields() {
|
||||
let pod = [0x00, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||
let value = bytemuck::checked::pod_read_unaligned::<
|
||||
CheckedBitPatternIntEnumWithFields,
|
||||
>(&pod);
|
||||
assert_eq!(value, CheckedBitPatternIntEnumWithFields::A(0x55));
|
||||
|
||||
let pod = [0x01, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0xcc];
|
||||
let value = bytemuck::checked::pod_read_unaligned::<
|
||||
CheckedBitPatternIntEnumWithFields,
|
||||
>(&pod);
|
||||
assert_eq!(value, CheckedBitPatternIntEnumWithFields::B { c: 0xcc5555cc });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkedbitpattern_nested_enum_with_fields() {
|
||||
// total size 24 bytes. first byte always the u8 discriminant.
|
||||
|
||||
#[repr(C, align(8))]
|
||||
struct Align8Bytes([u8; 24]);
|
||||
|
||||
// first we'll check variantA, nested variant A
|
||||
let pod = Align8Bytes([
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // byte 0 discriminant = 0 = variant A, bytes 1-7 irrelevant padding.
|
||||
0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0xcc, // bytes 8-15 are the nested CheckedBitPatternCEnumWithFields,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes 16-23 padding
|
||||
]);
|
||||
let value = bytemuck::checked::from_bytes::<
|
||||
CheckedBitPatternEnumNested,
|
||||
>(&pod.0);
|
||||
assert_eq!(value, &CheckedBitPatternEnumNested::A(CheckedBitPatternCEnumWithFields::A(0xcc5555cc)));
|
||||
|
||||
// next we'll check invalid first discriminant fails
|
||||
let pod = Align8Bytes([
|
||||
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // byte 0 discriminant = 2 = invalid, bytes 1-7 padding
|
||||
0x00, 0x00, 0x00, 0x00, 0xcc, 0x55, 0x55, 0xcc, // bytes 8-15 are the nested CheckedBitPatternCEnumWithFields = A,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes 16-23 padding
|
||||
]);
|
||||
let result = bytemuck::checked::try_from_bytes::<
|
||||
CheckedBitPatternEnumNested,
|
||||
>(&pod.0);
|
||||
assert_eq!(result, Err(CheckedCastError::InvalidBitPattern));
|
||||
|
||||
|
||||
// next we'll check variant B, nested variant B
|
||||
let pod = Align8Bytes([
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // byte 0 discriminant = 1 = variant B, bytes 1-7 padding
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes 8-15 is C int size discriminant of CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 1 (LE byte order) = variant B
|
||||
0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xcc, // bytes 16-13 is the data contained in nested variant B
|
||||
]);
|
||||
let value = bytemuck::checked::from_bytes::<
|
||||
CheckedBitPatternEnumNested,
|
||||
>(&pod.0);
|
||||
assert_eq!(
|
||||
value,
|
||||
&CheckedBitPatternEnumNested::B(CheckedBitPatternCDefaultDiscriminantEnumWithFields::B {
|
||||
c: 0xcc555555555555cc
|
||||
})
|
||||
);
|
||||
|
||||
// finally we'll check variant B, nested invalid discriminant
|
||||
let pod = Align8Bytes([
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 1 discriminant = variant B, bytes 1-7 padding
|
||||
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bytes 8-15 is C int size discriminant of CheckedBitPatternCDefaultDiscrimimantEnumWithFields, 0x08 is invalid
|
||||
0xcc, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xcc, // bytes 16-13 is the data contained in nested variant B
|
||||
]);
|
||||
let result = bytemuck::checked::try_from_bytes::<
|
||||
CheckedBitPatternEnumNested,
|
||||
>(&pod.0);
|
||||
assert_eq!(result, Err(CheckedCastError::InvalidBitPattern));
|
||||
}
|
||||
#[test]
|
||||
fn checkedbitpattern_transparent_enum_with_fields() {
|
||||
let pod = [0xcc, 0x55, 0x55, 0xcc];
|
||||
let value = bytemuck::checked::pod_read_unaligned::<
|
||||
CheckedBitPatternTransparentEnumWithFields,
|
||||
>(&pod);
|
||||
assert_eq!(
|
||||
value,
|
||||
CheckedBitPatternTransparentEnumWithFields::A { b: 0xcc5555cc }
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
#[repr(C, align(16))]
|
||||
struct Issue127 {}
|
||||
|
||||
use bytemuck as reexport_name;
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, bytemuck::ByteEq)]
|
||||
#[bytemuck(crate = "reexport_name")]
|
||||
#[repr(C)]
|
||||
struct Issue93 {}
|
||||
|
Loading…
Reference in New Issue
Block a user