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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use super::*;
use crate::AllocatedShape;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Result
////////////////////////////////////////////////////////////////////////////////////////////////////
impl<const BORROW: bool> Partial<'_, BORROW> {
/// Begin building the Ok variant of a Result
pub fn begin_ok(mut self) -> Result<Self, ReflectError> {
// Verify we're working with a Result and get the def
let result_def = {
let frame = self.frames().last().unwrap();
match frame.allocated.shape().def {
Def::Result(def) => def,
_ => {
return Err(self.err(ReflectErrorKind::WasNotA {
expected: "Result",
actual: frame.allocated.shape(),
}));
}
}
};
// Check if we need to handle re-initialization.
let needs_reinit = {
let frame = self.frames().last().unwrap();
frame.is_init
|| matches!(
frame.tracker,
Tracker::Result {
building_inner: false,
..
}
)
};
if needs_reinit {
self.prepare_for_reinitialization();
}
// Set tracker to indicate we're building the Ok value
// Get the type_plan before modifying tracker
let parent_type_plan = self.frames().last().unwrap().type_plan;
self.mode.stack_mut().last_mut().unwrap().tracker = Tracker::Result {
is_ok: true,
building_inner: true,
};
// Get the Ok type shape
let inner_shape = result_def.t;
// Allocate memory for the inner value
let inner_layout = inner_shape.layout.sized_layout().map_err(|_| {
self.err(ReflectErrorKind::Unsized {
shape: inner_shape,
operation: "begin_ok, allocating Result Ok value",
})
})?;
let inner_data = if inner_layout.size() == 0 {
// For ZST, use a non-null but unallocated pointer
PtrUninit::new(NonNull::<u8>::dangling().as_ptr())
} else {
// Allocate memory for the inner value
let ptr = unsafe { ::alloc::alloc::alloc(inner_layout) };
let Some(ptr) = NonNull::new(ptr) else {
::alloc::alloc::handle_alloc_error(inner_layout);
};
PtrUninit::new(ptr.as_ptr())
};
// Create a new frame for the inner value
// Get child type plan NodeId for Result Ok type
let (ok_node_id, _err_node_id) = self
.root_plan
.result_nodes_id(parent_type_plan)
.expect("TypePlan should have Result nodes");
let inner_frame = Frame::new(
inner_data,
AllocatedShape::new(inner_shape, inner_layout.size()),
FrameOwnership::Owned,
ok_node_id,
);
self.mode.stack_mut().push(inner_frame);
Ok(self)
}
/// Begin building the Err variant of a Result
pub fn begin_err(mut self) -> Result<Self, ReflectError> {
// Verify we're working with a Result and get the def
let result_def = {
let frame = self.frames().last().unwrap();
match frame.allocated.shape().def {
Def::Result(def) => def,
_ => {
return Err(self.err(ReflectErrorKind::WasNotA {
expected: "Result",
actual: frame.allocated.shape(),
}));
}
}
};
// Check if we need to handle re-initialization.
let needs_reinit = {
let frame = self.frames().last().unwrap();
frame.is_init
|| matches!(
frame.tracker,
Tracker::Result {
building_inner: false,
..
}
)
};
if needs_reinit {
self.prepare_for_reinitialization();
}
// Set tracker to indicate we're building the Err value
// Get the type_plan before modifying tracker
let parent_type_plan = self.frames().last().unwrap().type_plan;
self.mode.stack_mut().last_mut().unwrap().tracker = Tracker::Result {
is_ok: false,
building_inner: true,
};
// Get the Err type shape
let inner_shape = result_def.e;
// Allocate memory for the inner value
let inner_layout = inner_shape.layout.sized_layout().map_err(|_| {
self.err(ReflectErrorKind::Unsized {
shape: inner_shape,
operation: "begin_err, allocating Result Err value",
})
})?;
let inner_data = if inner_layout.size() == 0 {
// For ZST, use a non-null but unallocated pointer
PtrUninit::new(NonNull::<u8>::dangling().as_ptr())
} else {
// Allocate memory for the inner value
let ptr = unsafe { ::alloc::alloc::alloc(inner_layout) };
let Some(ptr) = NonNull::new(ptr) else {
::alloc::alloc::handle_alloc_error(inner_layout);
};
PtrUninit::new(ptr.as_ptr())
};
// Create a new frame for the inner value
// Get child type plan NodeId for Result Err type
let (_ok_node_id, err_node_id) = self
.root_plan
.result_nodes_id(parent_type_plan)
.expect("TypePlan should have Result nodes");
let inner_frame = Frame::new(
inner_data,
AllocatedShape::new(inner_shape, inner_layout.size()),
FrameOwnership::Owned,
err_node_id,
);
self.mode.stack_mut().push(inner_frame);
Ok(self)
}
}