From 4d5abbe232b786359659dc1c3a717e1557fa4751 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 16 Aug 2024 14:11:55 +0300 Subject: feat(tazjin/german-string): add Drop impl for transient strings All of these strings are currently transient (the storage class is not yet represented anywhere), and the ones that are heap allocated need to be deallocated when the transient string dies. Change-Id: Iba0ca926df5db7594f304c5d5318db9d69c6f42c Reviewed-on: https://cl.tvl.fyi/c/depot/+/12235 Autosubmit: tazjin Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/german-string/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/users/tazjin/german-string/src/lib.rs b/users/tazjin/german-string/src/lib.rs index c35cb3ad4464..813cd5b1e515 100644 --- a/users/tazjin/german-string/src/lib.rs +++ b/users/tazjin/german-string/src/lib.rs @@ -65,4 +65,21 @@ impl GermanString { GermanString(GSRepr { large }) } } + + fn len(&self) -> usize { + // SAFETY: The length field is located in the same location for both + // variants, reading it from either is safe. + unsafe { self.0.small.len as usize } + } +} + +impl Drop for GermanString { + fn drop(&mut self) { + if self.len() > 12 { + let layout = Layout::array::(self.len()).unwrap(); + unsafe { + std::alloc::dealloc(self.0.large.data, layout); + } + } + } } -- cgit 1.4.1