From 3024c1434a667425d30e4b0785857381323712aa Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Fri, 8 Dec 2017 17:32:04 -0800 Subject: Use Try syntax for Option in place of macros or match --- src/liballoc/allocator.rs | 46 +++++++++------------------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) (limited to 'src/liballoc/allocator.rs') diff --git a/src/liballoc/allocator.rs b/src/liballoc/allocator.rs index 3a2022ad429..c2a8f5f8ff9 100644 --- a/src/liballoc/allocator.rs +++ b/src/liballoc/allocator.rs @@ -217,14 +217,8 @@ impl Layout { /// On arithmetic overflow, returns `None`. #[inline] pub fn repeat(&self, n: usize) -> Option<(Self, usize)> { - let padded_size = match self.size.checked_add(self.padding_needed_for(self.align)) { - None => return None, - Some(padded_size) => padded_size, - }; - let alloc_size = match padded_size.checked_mul(n) { - None => return None, - Some(alloc_size) => alloc_size, - }; + let padded_size = self.size.checked_add(self.padding_needed_for(self.align))?; + let alloc_size = padded_size.checked_mul(n)?; // We can assume that `self.align` is a power-of-two that does // not exceed 231. Furthermore, `alloc_size` has already been @@ -246,26 +240,14 @@ impl Layout { /// On arithmetic overflow, returns `None`. pub fn extend(&self, next: Self) -> Option<(Self, usize)> { let new_align = cmp::max(self.align, next.align); - let realigned = match Layout::from_size_align(self.size, new_align) { - None => return None, - Some(l) => l, - }; + let realigned = Layout::from_size_align(self.size, new_align)?; let pad = realigned.padding_needed_for(next.align); - let offset = match self.size.checked_add(pad) { - None => return None, - Some(offset) => offset, - }; - let new_size = match offset.checked_add(next.size) { - None => return None, - Some(new_size) => new_size, - }; + let offset = self.size.checked_add(pad)?; + let new_size = offset.checked_add(next.size)?; - let layout = match Layout::from_size_align(new_size, new_align) { - None => return None, - Some(l) => l, - }; + let layout = Layout::from_size_align(new_size, new_align)?; Some((layout, offset)) } @@ -282,11 +264,7 @@ impl Layout { /// /// On arithmetic overflow, returns `None`. pub fn repeat_packed(&self, n: usize) -> Option { - let size = match self.size().checked_mul(n) { - None => return None, - Some(scaled) => scaled, - }; - + let size = self.size().checked_mul(n)?; Layout::from_size_align(size, self.align) } @@ -306,14 +284,8 @@ impl Layout { /// /// On arithmetic overflow, returns `None`. pub fn extend_packed(&self, next: Self) -> Option<(Self, usize)> { - let new_size = match self.size().checked_add(next.size()) { - None => return None, - Some(new_size) => new_size, - }; - let layout = match Layout::from_size_align(new_size, self.align) { - None => return None, - Some(l) => l, - }; + let new_size = self.size().checked_add(next.size())?; + let layout = Layout::from_size_align(new_size, self.align)?; Some((layout, self.size())) } -- cgit 1.4.1-3-g733a5