diff options
| author | Saleem Jaffer <ssaleem1992@gmail.com> | 2019-04-25 18:22:34 +0530 |
|---|---|---|
| committer | Saleem Jaffer <ssaleem1992@gmail.com> | 2019-05-04 15:17:26 +0530 |
| commit | 8e3d9f1039df40a959d8d63e71dec5ecfecd56c7 (patch) | |
| tree | 1b520ddcd5b6dc0d88e894903b2b951a016e1a8f | |
| parent | 35bd58b4e85791e9c94f1eea7387f59c5feb6b60 (diff) | |
add to_result to ty::MaybeResult
| -rw-r--r-- | src/librustc/ty/layout.rs | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index b0631598554..7f879cd77fd 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -5,6 +5,7 @@ use syntax::ast::{self, Ident, IntTy, UintTy}; use syntax::attr; use syntax_pos::DUMMY_SP; +// use std::convert::From; use std::cmp; use std::fmt; use std::i128; @@ -1544,32 +1545,38 @@ impl<'gcx, 'tcx, T: HasTyCtxt<'gcx>> HasTyCtxt<'gcx> for LayoutCx<'tcx, T> { } pub trait MaybeResult<T> { + type Item; + fn from_ok(x: T) -> Self; fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self; - fn ok(self) -> Option<T>; + fn to_result(self) -> Result<T, Self::Item>; } impl<T> MaybeResult<T> for T { + type Item = !; + fn from_ok(x: T) -> Self { x } fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self { f(self) } - fn ok(self) -> Option<T> { - Some(self) + fn to_result(self) -> Result<T, !> { + Ok(self) } } impl<T, E> MaybeResult<T> for Result<T, E> { + type Item = E; + fn from_ok(x: T) -> Self { Ok(x) } fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self { self.map(f) } - fn ok(self) -> Option<T> { - self.ok() + fn to_result(self) -> Result<T, E> { + self } } @@ -1843,7 +1850,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> ) -> Option<PointeeInfo> { match this.ty.sty { ty::RawPtr(mt) if offset.bytes() == 0 => { - cx.layout_of(mt.ty).ok() + cx.layout_of(mt.ty).to_result().ok() .map(|layout| PointeeInfo { size: layout.size, align: layout.align.abi, @@ -1882,7 +1889,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } }; - cx.layout_of(ty).ok() + cx.layout_of(ty).to_result().ok() .map(|layout| PointeeInfo { size: layout.size, align: layout.align.abi, @@ -1930,7 +1937,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> let field_start = variant.fields.offset(i); if field_start <= offset { let field = variant.field(cx, i); - result = field.ok() + result = field.to_result().ok() .and_then(|field| { if ptr_end <= field_start + field.size { // We found the right field, look inside it. |
