mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-11 18:58:24 +03:00
Merge pull request #6878 from roc-lang/glue-borrow
Update rust glue to allow for borrowing payloads from tags
This commit is contained in:
commit
69d670d27e
@ -644,6 +644,30 @@ generateDestructorFunction = \buf, types, tagUnionType, name, optPayload ->
|
||||
else
|
||||
"unsafe { core::mem::ManuallyDrop::take(&mut self.payload.$(name)) }"
|
||||
|
||||
(borrow, borrowType) =
|
||||
if canDeriveCopy types shape then
|
||||
("unsafe { self.payload.$(name) }", payloadType)
|
||||
else
|
||||
(
|
||||
"""
|
||||
use core::borrow::Borrow;
|
||||
unsafe { self.payload.$(name).borrow() }
|
||||
""",
|
||||
"&$(payloadType)",
|
||||
)
|
||||
|
||||
(borrowMut, borrowMutType) =
|
||||
if canDeriveCopy types shape then
|
||||
("unsafe { &mut self.payload.$(name) }", "&mut $(payloadType)")
|
||||
else
|
||||
(
|
||||
"""
|
||||
use core::borrow::BorrowMut;
|
||||
unsafe { self.payload.$(name).borrow_mut() }
|
||||
""",
|
||||
"&mut $(payloadType)",
|
||||
)
|
||||
|
||||
"""
|
||||
$(buf)
|
||||
|
||||
@ -652,6 +676,16 @@ generateDestructorFunction = \buf, types, tagUnionType, name, optPayload ->
|
||||
$(take)
|
||||
}
|
||||
|
||||
pub fn borrow_$(name)(&self) -> $(borrowType) {
|
||||
debug_assert_eq!(self.discriminant, discriminant_$(tagUnionType)::$(name));
|
||||
$(borrow)
|
||||
}
|
||||
|
||||
pub fn borrow_mut_$(name)(&mut self) -> $(borrowMutType) {
|
||||
debug_assert_eq!(self.discriminant, discriminant_$(tagUnionType)::$(name));
|
||||
$(borrowMut)
|
||||
}
|
||||
|
||||
pub fn is_$(name)(&self) -> bool {
|
||||
matches!(self.discriminant, discriminant_$(tagUnionType)::$(name))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user