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
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(ReflectError::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
let frame = self.frames_mut().last_mut().unwrap();
frame.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(|_| ReflectError::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
let inner_frame = Frame::new(
inner_data,
AllocatedShape::new(inner_shape, inner_layout.size()),
FrameOwnership::Owned,
);
self.frames_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(ReflectError::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
let frame = self.frames_mut().last_mut().unwrap();
frame.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(|_| ReflectError::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
let inner_frame = Frame::new(
inner_data,
AllocatedShape::new(inner_shape, inner_layout.size()),
FrameOwnership::Owned,
);
self.frames_mut().push(inner_frame);
Ok(self)
}
}