Skip to content

Commit 8e3d9f1

Browse files
committed
add to_result to ty::MaybeResult
1 parent 35bd58b commit 8e3d9f1

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/librustc/ty/layout.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use syntax::ast::{self, Ident, IntTy, UintTy};
55
use syntax::attr;
66
use syntax_pos::DUMMY_SP;
77

8+
// use std::convert::From;
89
use std::cmp;
910
use std::fmt;
1011
use std::i128;
@@ -1544,32 +1545,38 @@ impl<'gcx, 'tcx, T: HasTyCtxt<'gcx>> HasTyCtxt<'gcx> for LayoutCx<'tcx, T> {
15441545
}
15451546

15461547
pub trait MaybeResult<T> {
1548+
type Item;
1549+
15471550
fn from_ok(x: T) -> Self;
15481551
fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self;
1549-
fn ok(self) -> Option<T>;
1552+
fn to_result(self) -> Result<T, Self::Item>;
15501553
}
15511554

15521555
impl<T> MaybeResult<T> for T {
1556+
type Item = !;
1557+
15531558
fn from_ok(x: T) -> Self {
15541559
x
15551560
}
15561561
fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self {
15571562
f(self)
15581563
}
1559-
fn ok(self) -> Option<T> {
1560-
Some(self)
1564+
fn to_result(self) -> Result<T, !> {
1565+
Ok(self)
15611566
}
15621567
}
15631568

15641569
impl<T, E> MaybeResult<T> for Result<T, E> {
1570+
type Item = E;
1571+
15651572
fn from_ok(x: T) -> Self {
15661573
Ok(x)
15671574
}
15681575
fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self {
15691576
self.map(f)
15701577
}
1571-
fn ok(self) -> Option<T> {
1572-
self.ok()
1578+
fn to_result(self) -> Result<T, E> {
1579+
self
15731580
}
15741581
}
15751582

@@ -1843,7 +1850,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
18431850
) -> Option<PointeeInfo> {
18441851
match this.ty.sty {
18451852
ty::RawPtr(mt) if offset.bytes() == 0 => {
1846-
cx.layout_of(mt.ty).ok()
1853+
cx.layout_of(mt.ty).to_result().ok()
18471854
.map(|layout| PointeeInfo {
18481855
size: layout.size,
18491856
align: layout.align.abi,
@@ -1882,7 +1889,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
18821889
}
18831890
};
18841891

1885-
cx.layout_of(ty).ok()
1892+
cx.layout_of(ty).to_result().ok()
18861893
.map(|layout| PointeeInfo {
18871894
size: layout.size,
18881895
align: layout.align.abi,
@@ -1930,7 +1937,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
19301937
let field_start = variant.fields.offset(i);
19311938
if field_start <= offset {
19321939
let field = variant.field(cx, i);
1933-
result = field.ok()
1940+
result = field.to_result().ok()
19341941
.and_then(|field| {
19351942
if ptr_end <= field_start + field.size {
19361943
// We found the right field, look inside it.

0 commit comments

Comments
 (0)