Coverage Report

Created: 2026-06-07 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasmtime/cranelift/entity/src/packed_option.rs
Line
Count
Source
1
//! Compact representation of `Option<T>` for types with a reserved value.
2
//!
3
//! Small Cranelift types like the 32-bit entity references are often used in tables and linked
4
//! lists where an `Option<T>` is needed. Unfortunately, that would double the size of the tables
5
//! because `Option<T>` is twice as big as `T`.
6
//!
7
//! This module provides a `PackedOption<T>` for types that have a reserved value that can be used
8
//! to represent `None`.
9
10
use core::{fmt, mem};
11
use wasmtime_core::{alloc::TryClone, error::OutOfMemory};
12
13
#[cfg(feature = "enable-serde")]
14
use serde_derive::{Deserialize, Serialize};
15
16
/// Types that have a reserved value which can't be created any other way.
17
pub trait ReservedValue {
18
    /// Create an instance of the reserved value.
19
    fn reserved_value() -> Self;
20
    /// Checks whether value is the reserved one.
21
    fn is_reserved_value(&self) -> bool;
22
}
23
24
/// Packed representation of `Option<T>`.
25
///
26
/// This is a wrapper around a `T`, using `T::reserved_value` to represent
27
/// `None`.
28
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
29
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
30
#[repr(transparent)]
31
pub struct PackedOption<T: ReservedValue>(T);
32
33
impl<T> TryClone for PackedOption<T>
34
where
35
    T: ReservedValue + TryClone,
36
{
37
    fn try_clone(&self) -> Result<Self, OutOfMemory> {
38
        Ok(Self(self.0.try_clone()?))
39
    }
40
}
41
42
impl<T: ReservedValue> PackedOption<T> {
43
    /// Const constructor wrapping a raw `T`.
44
    ///
45
    /// To create `None`, pass `T::reserved_value()`. To create `Some(val)`,
46
    /// pass a non-reserved `val`.
47
85.6M
    pub const fn new(val: T) -> Self {
48
85.6M
        Self(val)
49
85.6M
    }
50
51
    /// Returns `true` if the packed option is a `None` value.
52
7.03G
    pub fn is_none(&self) -> bool {
53
7.03G
        self.0.is_reserved_value()
54
7.03G
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::VMSharedTypeIndex>>::is_none
Line
Count
Source
52
4.62M
    pub fn is_none(&self) -> bool {
53
4.62M
        self.0.is_reserved_value()
54
4.62M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::graphs::scc::Scc>>::is_none
Line
Count
Source
52
117k
    pub fn is_none(&self) -> bool {
53
117k
        self.0.is_reserved_value()
54
117k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_internal_cranelift::translate::heap::Heap>>::is_none
Line
Count
Source
52
2.12M
    pub fn is_none(&self) -> bool {
53
2.12M
        self.0.is_reserved_value()
54
2.12M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::SigRef>>::is_none
Line
Count
Source
52
1.96M
    pub fn is_none(&self) -> bool {
53
1.96M
        self.0.is_reserved_value()
54
1.96M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::FuncRef>>::is_none
Line
Count
Source
52
670k
    pub fn is_none(&self) -> bool {
53
670k
        self.0.is_reserved_value()
54
670k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_bforest::Node>>::is_none
Line
Count
Source
52
409M
    pub fn is_none(&self) -> bool {
53
409M
        self.0.is_reserved_value()
54
409M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::loop_analysis::Loop>>::is_none
Line
Count
Source
52
42.5M
    pub fn is_none(&self) -> bool {
53
42.5M
        self.0.is_reserved_value()
54
42.5M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::UserExternalNameRef>>::is_none
Line
Count
Source
52
1.21M
    pub fn is_none(&self) -> bool {
53
1.21M
        self.0.is_reserved_value()
54
1.21M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Inst>>::is_none
Line
Count
Source
52
4.24G
    pub fn is_none(&self) -> bool {
53
4.24G
        self.0.is_reserved_value()
54
4.24G
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block>>::is_none
Line
Count
Source
52
2.13G
    pub fn is_none(&self) -> bool {
53
2.13G
        self.0.is_reserved_value()
54
2.13G
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Value>>::is_none
Line
Count
Source
52
116M
    pub fn is_none(&self) -> bool {
53
116M
        self.0.is_reserved_value()
54
116M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Constant>>::is_none
Line
Count
Source
52
2.78k
    pub fn is_none(&self) -> bool {
53
2.78k
        self.0.is_reserved_value()
54
2.78k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::memflags::AliasRegion>>::is_none
Line
Count
Source
52
69.8M
    pub fn is_none(&self) -> bool {
53
69.8M
        self.0.is_reserved_value()
54
69.8M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::StaticModuleIndex>>::is_none
Line
Count
Source
52
43.6k
    pub fn is_none(&self) -> bool {
53
43.6k
        self.0.is_reserved_value()
54
43.6k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::ModuleInternedTypeIndex>>::is_none
Line
Count
Source
52
7.87M
    pub fn is_none(&self) -> bool {
53
7.87M
        self.0.is_reserved_value()
54
7.87M
    }
55
56
    /// Returns `true` if the packed option is a `Some` value.
57
103M
    pub fn is_some(&self) -> bool {
58
103M
        !self.0.is_reserved_value()
59
103M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::ModuleInternedTypeIndex>>::is_some
Line
Count
Source
57
1.35M
    pub fn is_some(&self) -> bool {
58
1.35M
        !self.0.is_reserved_value()
59
1.35M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Inst>>::is_some
Line
Count
Source
57
4.93M
    pub fn is_some(&self) -> bool {
58
4.93M
        !self.0.is_reserved_value()
59
4.93M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block>>::is_some
Line
Count
Source
57
96.8M
    pub fn is_some(&self) -> bool {
58
96.8M
        !self.0.is_reserved_value()
59
96.8M
    }
60
61
    /// Expand the packed option into a normal `Option`.
62
6.82G
    pub fn expand(self) -> Option<T> {
63
6.82G
        if self.is_none() { None } else { Some(self.0) }
64
6.82G
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::VMSharedTypeIndex>>::expand
Line
Count
Source
62
4.62M
    pub fn expand(self) -> Option<T> {
63
4.62M
        if self.is_none() { None } else { Some(self.0) }
64
4.62M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::graphs::scc::Scc>>::expand
Line
Count
Source
62
117k
    pub fn expand(self) -> Option<T> {
63
117k
        if self.is_none() { None } else { Some(self.0) }
64
117k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_internal_cranelift::translate::heap::Heap>>::expand
Line
Count
Source
62
2.12M
    pub fn expand(self) -> Option<T> {
63
2.12M
        if self.is_none() { None } else { Some(self.0) }
64
2.12M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::SigRef>>::expand
Line
Count
Source
62
1.96M
    pub fn expand(self) -> Option<T> {
63
1.96M
        if self.is_none() { None } else { Some(self.0) }
64
1.96M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::FuncRef>>::expand
Line
Count
Source
62
670k
    pub fn expand(self) -> Option<T> {
63
670k
        if self.is_none() { None } else { Some(self.0) }
64
670k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_bforest::Node>>::expand
Line
Count
Source
62
409M
    pub fn expand(self) -> Option<T> {
63
409M
        if self.is_none() { None } else { Some(self.0) }
64
409M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::loop_analysis::Loop>>::expand
Line
Count
Source
62
42.5M
    pub fn expand(self) -> Option<T> {
63
42.5M
        if self.is_none() { None } else { Some(self.0) }
64
42.5M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::UserExternalNameRef>>::expand
Line
Count
Source
62
1.21M
    pub fn expand(self) -> Option<T> {
63
1.21M
        if self.is_none() { None } else { Some(self.0) }
64
1.21M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Inst>>::expand
Line
Count
Source
62
4.03G
    pub fn expand(self) -> Option<T> {
63
4.03G
        if self.is_none() { None } else { Some(self.0) }
64
4.03G
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block>>::expand
Line
Count
Source
62
2.13G
    pub fn expand(self) -> Option<T> {
63
2.13G
        if self.is_none() { None } else { Some(self.0) }
64
2.13G
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Value>>::expand
Line
Count
Source
62
116M
    pub fn expand(self) -> Option<T> {
63
116M
        if self.is_none() { None } else { Some(self.0) }
64
116M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Constant>>::expand
Line
Count
Source
62
2.78k
    pub fn expand(self) -> Option<T> {
63
2.78k
        if self.is_none() { None } else { Some(self.0) }
64
2.78k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::memflags::AliasRegion>>::expand
Line
Count
Source
62
69.8M
    pub fn expand(self) -> Option<T> {
63
69.8M
        if self.is_none() { None } else { Some(self.0) }
64
69.8M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::StaticModuleIndex>>::expand
Line
Count
Source
62
43.6k
    pub fn expand(self) -> Option<T> {
63
43.6k
        if self.is_none() { None } else { Some(self.0) }
64
43.6k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::ModuleInternedTypeIndex>>::expand
Line
Count
Source
62
7.87M
    pub fn expand(self) -> Option<T> {
63
7.87M
        if self.is_none() { None } else { Some(self.0) }
64
7.87M
    }
65
66
    /// Maps a `PackedOption<T>` to `Option<U>` by applying a function to a contained value.
67
0
    pub fn map<U, F>(self, f: F) -> Option<U>
68
0
    where
69
0
        F: FnOnce(T) -> U,
70
    {
71
0
        self.expand().map(f)
72
0
    }
Unexecuted instantiation: <cranelift_entity::packed_option::PackedOption<cranelift_bforest::Node>>::map::<wasmtime_internal_core::slab::Id, <cranelift_bforest::map::MapCursorRaw<wasmtime::runtime::code::StoreCodePC, wasmtime_internal_core::slab::Id>>::goto_first::{closure#0}>
Unexecuted instantiation: <cranelift_entity::packed_option::PackedOption<cranelift_bforest::Node>>::map::<wasmtime_internal_core::slab::Id, <cranelift_bforest::map::MapCursorRaw<usize, wasmtime_internal_core::slab::Id>>::goto_first::{closure#0}>
73
74
    /// Unwrap a packed `Some` value or panic.
75
    #[track_caller]
76
521M
    pub fn unwrap(self) -> T {
77
521M
        self.expand().unwrap()
78
521M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::graphs::scc::Scc>>::unwrap
Line
Count
Source
76
117k
    pub fn unwrap(self) -> T {
77
117k
        self.expand().unwrap()
78
117k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Inst>>::unwrap
Line
Count
Source
76
182M
    pub fn unwrap(self) -> T {
77
182M
        self.expand().unwrap()
78
182M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block>>::unwrap
Line
Count
Source
76
339M
    pub fn unwrap(self) -> T {
77
339M
        self.expand().unwrap()
78
339M
    }
Unexecuted instantiation: <cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::ModuleInternedTypeIndex>>::unwrap
79
80
    /// Unwrap a packed `Some` value or panic.
81
    #[track_caller]
82
247M
    pub fn expect(self, msg: &str) -> T {
83
247M
        self.expand().expect(msg)
84
247M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::UserExternalNameRef>>::expect
Line
Count
Source
82
1.21M
    pub fn expect(self, msg: &str) -> T {
83
1.21M
        self.expand().expect(msg)
84
1.21M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block>>::expect
Line
Count
Source
82
246M
    pub fn expect(self, msg: &str) -> T {
83
246M
        self.expand().expect(msg)
84
246M
    }
85
86
    /// Takes the value out of the packed option, leaving a `None` in its place.
87
202M
    pub fn take(&mut self) -> Option<T> {
88
202M
        mem::replace(self, None.into()).expand()
89
202M
    }
90
}
91
92
impl<T: ReservedValue> Default for PackedOption<T> {
93
    /// Create a default packed option representing `None`.
94
613M
    fn default() -> Self {
95
613M
        Self(T::reserved_value())
96
613M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::VMSharedTypeIndex> as core::default::Default>::default
Line
Count
Source
94
110k
    fn default() -> Self {
95
110k
        Self(T::reserved_value())
96
110k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::graphs::scc::Scc> as core::default::Default>::default
Line
Count
Source
94
43.8k
    fn default() -> Self {
95
43.8k
        Self(T::reserved_value())
96
43.8k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_internal_cranelift::translate::heap::Heap> as core::default::Default>::default
Line
Count
Source
94
1.12M
    fn default() -> Self {
95
1.12M
        Self(T::reserved_value())
96
1.12M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::SigRef> as core::default::Default>::default
Line
Count
Source
94
1.12M
    fn default() -> Self {
95
1.12M
        Self(T::reserved_value())
96
1.12M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::FuncRef> as core::default::Default>::default
Line
Count
Source
94
2.24M
    fn default() -> Self {
95
2.24M
        Self(T::reserved_value())
96
2.24M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_bforest::Node> as core::default::Default>::default
Line
Count
Source
94
208M
    fn default() -> Self {
95
208M
        Self(T::reserved_value())
96
208M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::loop_analysis::Loop> as core::default::Default>::default
Line
Count
Source
94
1.35M
    fn default() -> Self {
95
1.35M
        Self(T::reserved_value())
96
1.35M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::UserExternalNameRef> as core::default::Default>::default
Line
Count
Source
94
430k
    fn default() -> Self {
95
430k
        Self(T::reserved_value())
96
430k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Inst> as core::default::Default>::default
Line
Count
Source
94
233M
    fn default() -> Self {
95
233M
        Self(T::reserved_value())
96
233M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block> as core::default::Default>::default
Line
Count
Source
94
162M
    fn default() -> Self {
95
162M
        Self(T::reserved_value())
96
162M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Value> as core::default::Default>::default
Line
Count
Source
94
1.51M
    fn default() -> Self {
95
1.51M
        Self(T::reserved_value())
96
1.51M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Constant> as core::default::Default>::default
Line
Count
Source
94
430k
    fn default() -> Self {
95
430k
        Self(T::reserved_value())
96
430k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::memflags::AliasRegion> as core::default::Default>::default
Line
Count
Source
94
137k
    fn default() -> Self {
95
137k
        Self(T::reserved_value())
96
137k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::StaticModuleIndex> as core::default::Default>::default
Line
Count
Source
94
215
    fn default() -> Self {
95
215
        Self(T::reserved_value())
96
215
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::ModuleInternedTypeIndex> as core::default::Default>::default
Line
Count
Source
94
532k
    fn default() -> Self {
95
532k
        Self(T::reserved_value())
96
532k
    }
97
}
98
99
impl<T: ReservedValue> From<T> for PackedOption<T> {
100
    /// Convert `t` into a packed `Some(x)`.
101
1.43G
    fn from(t: T) -> Self {
102
1.43G
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
1.43G
        Self(t)
107
1.43G
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::VMSharedTypeIndex> as core::convert::From<wasmtime_environ::types::VMSharedTypeIndex>>::from
Line
Count
Source
101
629k
    fn from(t: T) -> Self {
102
629k
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
629k
        Self(t)
107
629k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::graphs::scc::Scc> as core::convert::From<wasmtime_environ::graphs::scc::Scc>>::from
Line
Count
Source
101
430k
    fn from(t: T) -> Self {
102
430k
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
430k
        Self(t)
107
430k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_internal_cranelift::translate::heap::Heap> as core::convert::From<wasmtime_internal_cranelift::translate::heap::Heap>>::from
Line
Count
Source
101
162k
    fn from(t: T) -> Self {
102
162k
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
162k
        Self(t)
107
162k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::SigRef> as core::convert::From<cranelift_codegen::ir::entities::SigRef>>::from
Line
Count
Source
101
312k
    fn from(t: T) -> Self {
102
312k
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
312k
        Self(t)
107
312k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::FuncRef> as core::convert::From<cranelift_codegen::ir::entities::FuncRef>>::from
Line
Count
Source
101
221k
    fn from(t: T) -> Self {
102
221k
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
221k
        Self(t)
107
221k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_bforest::Node> as core::convert::From<cranelift_bforest::Node>>::from
Line
Count
Source
101
151M
    fn from(t: T) -> Self {
102
151M
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
151M
        Self(t)
107
151M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::loop_analysis::Loop> as core::convert::From<cranelift_codegen::loop_analysis::Loop>>::from
Line
Count
Source
101
1.88M
    fn from(t: T) -> Self {
102
1.88M
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
1.88M
        Self(t)
107
1.88M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::UserExternalNameRef> as core::convert::From<cranelift_codegen::ir::entities::UserExternalNameRef>>::from
Line
Count
Source
101
696k
    fn from(t: T) -> Self {
102
696k
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
696k
        Self(t)
107
696k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Inst> as core::convert::From<cranelift_codegen::ir::entities::Inst>>::from
Line
Count
Source
101
724M
    fn from(t: T) -> Self {
102
724M
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
724M
        Self(t)
107
724M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block> as core::convert::From<cranelift_codegen::ir::entities::Block>>::from
Line
Count
Source
101
452M
    fn from(t: T) -> Self {
102
452M
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
452M
        Self(t)
107
452M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Value> as core::convert::From<cranelift_codegen::ir::entities::Value>>::from
Line
Count
Source
101
92.6M
    fn from(t: T) -> Self {
102
92.6M
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
92.6M
        Self(t)
107
92.6M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Constant> as core::convert::From<cranelift_codegen::ir::entities::Constant>>::from
Line
Count
Source
101
1.72k
    fn from(t: T) -> Self {
102
1.72k
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
1.72k
        Self(t)
107
1.72k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::memflags::AliasRegion> as core::convert::From<cranelift_codegen::ir::memflags::AliasRegion>>::from
Line
Count
Source
101
8.60M
    fn from(t: T) -> Self {
102
8.60M
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
8.60M
        Self(t)
107
8.60M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::StaticModuleIndex> as core::convert::From<wasmtime_environ::types::StaticModuleIndex>>::from
Line
Count
Source
101
39.5k
    fn from(t: T) -> Self {
102
39.5k
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
39.5k
        Self(t)
107
39.5k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::ModuleInternedTypeIndex> as core::convert::From<wasmtime_environ::types::ModuleInternedTypeIndex>>::from
Line
Count
Source
101
3.36M
    fn from(t: T) -> Self {
102
3.36M
        debug_assert!(
103
0
            !t.is_reserved_value(),
104
            "Can't make a PackedOption from the reserved value."
105
        );
106
3.36M
        Self(t)
107
3.36M
    }
108
}
109
110
impl<T: ReservedValue> From<Option<T>> for PackedOption<T> {
111
    /// Convert an option into its packed equivalent.
112
615M
    fn from(opt: Option<T>) -> Self {
113
615M
        match opt {
114
566M
            None => Self::default(),
115
49.0M
            Some(t) => t.into(),
116
        }
117
615M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::VMSharedTypeIndex> as core::convert::From<core::option::Option<wasmtime_environ::types::VMSharedTypeIndex>>>::from
Line
Count
Source
112
629k
    fn from(opt: Option<T>) -> Self {
113
629k
        match opt {
114
0
            None => Self::default(),
115
629k
            Some(t) => t.into(),
116
        }
117
629k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::graphs::scc::Scc> as core::convert::From<core::option::Option<wasmtime_environ::graphs::scc::Scc>>>::from
Line
Count
Source
112
430k
    fn from(opt: Option<T>) -> Self {
113
430k
        match opt {
114
0
            None => Self::default(),
115
430k
            Some(t) => t.into(),
116
        }
117
430k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_internal_cranelift::translate::heap::Heap> as core::convert::From<core::option::Option<wasmtime_internal_cranelift::translate::heap::Heap>>>::from
Line
Count
Source
112
162k
    fn from(opt: Option<T>) -> Self {
113
162k
        match opt {
114
0
            None => Self::default(),
115
162k
            Some(t) => t.into(),
116
        }
117
162k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::SigRef> as core::convert::From<core::option::Option<cranelift_codegen::ir::entities::SigRef>>>::from
Line
Count
Source
112
312k
    fn from(opt: Option<T>) -> Self {
113
312k
        match opt {
114
0
            None => Self::default(),
115
312k
            Some(t) => t.into(),
116
        }
117
312k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::FuncRef> as core::convert::From<core::option::Option<cranelift_codegen::ir::entities::FuncRef>>>::from
Line
Count
Source
112
221k
    fn from(opt: Option<T>) -> Self {
113
221k
        match opt {
114
0
            None => Self::default(),
115
221k
            Some(t) => t.into(),
116
        }
117
221k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_bforest::Node> as core::convert::From<core::option::Option<cranelift_bforest::Node>>>::from
Line
Count
Source
112
208M
    fn from(opt: Option<T>) -> Self {
113
208M
        match opt {
114
208M
            None => Self::default(),
115
825k
            Some(t) => t.into(),
116
        }
117
208M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::loop_analysis::Loop> as core::convert::From<core::option::Option<cranelift_codegen::loop_analysis::Loop>>>::from
Line
Count
Source
112
259k
    fn from(opt: Option<T>) -> Self {
113
259k
        match opt {
114
259k
            None => Self::default(),
115
0
            Some(t) => t.into(),
116
        }
117
259k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::UserExternalNameRef> as core::convert::From<core::option::Option<cranelift_codegen::ir::entities::UserExternalNameRef>>>::from
Line
Count
Source
112
696k
    fn from(opt: Option<T>) -> Self {
113
696k
        match opt {
114
0
            None => Self::default(),
115
696k
            Some(t) => t.into(),
116
        }
117
696k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Inst> as core::convert::From<core::option::Option<cranelift_codegen::ir::entities::Inst>>>::from
Line
Count
Source
112
219M
    fn from(opt: Option<T>) -> Self {
113
219M
        match opt {
114
219M
            None => Self::default(),
115
0
            Some(t) => t.into(),
116
        }
117
219M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block> as core::convert::From<core::option::Option<cranelift_codegen::ir::entities::Block>>>::from
Line
Count
Source
112
157M
    fn from(opt: Option<T>) -> Self {
113
157M
        match opt {
114
137M
            None => Self::default(),
115
19.8M
            Some(t) => t.into(),
116
        }
117
157M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Value> as core::convert::From<core::option::Option<cranelift_codegen::ir::entities::Value>>>::from
Line
Count
Source
112
13.9M
    fn from(opt: Option<T>) -> Self {
113
13.9M
        match opt {
114
0
            None => Self::default(),
115
13.9M
            Some(t) => t.into(),
116
        }
117
13.9M
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Constant> as core::convert::From<core::option::Option<cranelift_codegen::ir::entities::Constant>>>::from
Line
Count
Source
112
1.72k
    fn from(opt: Option<T>) -> Self {
113
1.72k
        match opt {
114
0
            None => Self::default(),
115
1.72k
            Some(t) => t.into(),
116
        }
117
1.72k
    }
<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::memflags::AliasRegion> as core::convert::From<core::option::Option<cranelift_codegen::ir::memflags::AliasRegion>>>::from
Line
Count
Source
112
8.73M
    fn from(opt: Option<T>) -> Self {
113
8.73M
        match opt {
114
137k
            None => Self::default(),
115
8.60M
            Some(t) => t.into(),
116
        }
117
8.73M
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::StaticModuleIndex> as core::convert::From<core::option::Option<wasmtime_environ::types::StaticModuleIndex>>>::from
Line
Count
Source
112
39.8k
    fn from(opt: Option<T>) -> Self {
113
39.8k
        match opt {
114
215
            None => Self::default(),
115
39.5k
            Some(t) => t.into(),
116
        }
117
39.8k
    }
<cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::ModuleInternedTypeIndex> as core::convert::From<core::option::Option<wasmtime_environ::types::ModuleInternedTypeIndex>>>::from
Line
Count
Source
112
3.36M
    fn from(opt: Option<T>) -> Self {
113
3.36M
        match opt {
114
0
            None => Self::default(),
115
3.36M
            Some(t) => t.into(),
116
        }
117
3.36M
    }
118
}
119
120
impl<T: ReservedValue> From<PackedOption<T>> for Option<T> {
121
3.04G
    fn from(packed: PackedOption<T>) -> Option<T> {
122
3.04G
        packed.expand()
123
3.04G
    }
<core::option::Option<wasmtime_internal_cranelift::translate::heap::Heap> as core::convert::From<cranelift_entity::packed_option::PackedOption<wasmtime_internal_cranelift::translate::heap::Heap>>>::from
Line
Count
Source
121
2.12M
    fn from(packed: PackedOption<T>) -> Option<T> {
122
2.12M
        packed.expand()
123
2.12M
    }
<core::option::Option<cranelift_codegen::ir::entities::SigRef> as core::convert::From<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::SigRef>>>::from
Line
Count
Source
121
1.96M
    fn from(packed: PackedOption<T>) -> Option<T> {
122
1.96M
        packed.expand()
123
1.96M
    }
<core::option::Option<cranelift_codegen::ir::entities::FuncRef> as core::convert::From<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::FuncRef>>>::from
Line
Count
Source
121
670k
    fn from(packed: PackedOption<T>) -> Option<T> {
122
670k
        packed.expand()
123
670k
    }
<core::option::Option<cranelift_codegen::loop_analysis::Loop> as core::convert::From<cranelift_entity::packed_option::PackedOption<cranelift_codegen::loop_analysis::Loop>>>::from
Line
Count
Source
121
259k
    fn from(packed: PackedOption<T>) -> Option<T> {
122
259k
        packed.expand()
123
259k
    }
<core::option::Option<cranelift_codegen::ir::entities::Inst> as core::convert::From<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Inst>>>::from
Line
Count
Source
121
1.98G
    fn from(packed: PackedOption<T>) -> Option<T> {
122
1.98G
        packed.expand()
123
1.98G
    }
<core::option::Option<cranelift_codegen::ir::entities::Block> as core::convert::From<cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::entities::Block>>>::from
Line
Count
Source
121
1.05G
    fn from(packed: PackedOption<T>) -> Option<T> {
122
1.05G
        packed.expand()
123
1.05G
    }
124
}
125
126
impl<T> fmt::Debug for PackedOption<T>
127
where
128
    T: ReservedValue + fmt::Debug,
129
{
130
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131
0
        if self.is_none() {
132
0
            write!(f, "None")
133
        } else {
134
0
            write!(f, "Some({:?})", self.0)
135
        }
136
0
    }
Unexecuted instantiation: <cranelift_entity::packed_option::PackedOption<wasmtime_environ::types::ModuleInternedTypeIndex> as core::fmt::Debug>::fmt
Unexecuted instantiation: <cranelift_entity::packed_option::PackedOption<cranelift_codegen::ir::memflags::AliasRegion> as core::fmt::Debug>::fmt
137
}
138
139
#[cfg(test)]
140
mod tests {
141
    use super::*;
142
143
    // Dummy entity class, with no Copy or Clone.
144
    #[derive(Debug, PartialEq, Eq)]
145
    struct NoC(u32);
146
147
    impl ReservedValue for NoC {
148
        fn reserved_value() -> Self {
149
            NoC(13)
150
        }
151
152
        fn is_reserved_value(&self) -> bool {
153
            self.0 == 13
154
        }
155
    }
156
157
    #[test]
158
    fn moves() {
159
        let x = NoC(3);
160
        let somex: PackedOption<NoC> = x.into();
161
        assert!(!somex.is_none());
162
        assert_eq!(somex.expand(), Some(NoC(3)));
163
164
        let none: PackedOption<NoC> = None.into();
165
        assert!(none.is_none());
166
        assert_eq!(none.expand(), None);
167
    }
168
169
    // Dummy entity class, with Copy.
170
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
171
    struct Ent(u32);
172
173
    impl ReservedValue for Ent {
174
        fn reserved_value() -> Self {
175
            Ent(13)
176
        }
177
178
        fn is_reserved_value(&self) -> bool {
179
            self.0 == 13
180
        }
181
    }
182
183
    #[test]
184
    fn copies() {
185
        let x = Ent(2);
186
        let some: PackedOption<Ent> = x.into();
187
        assert_eq!(some.expand(), x.into());
188
        assert_eq!(some, x.into());
189
    }
190
}