diff options
author | Malo Jaffré <jaffre.malo@gmail.com> | 2017-08-24 12:11:10 +0200 |
---|---|---|
committer | Malo Jaffré <jaffre.malo@gmail.com> | 2017-08-24 12:11:10 +0200 |
commit | 93d96428ce7ec51c198866703cd3efc25800be11 (patch) | |
tree | 052f44543d812a98a59e5d30c839f6e2e20f3ede /src/codegen/helpers.rs | |
parent | 978b5316f79aae7a66fd7390d13276a005f9fc9a (diff) |
Simplify helpers::blob
Thanks @fitzgen for the detailed instructions.
Fixes #928.
Diffstat (limited to 'src/codegen/helpers.rs')
-rw-r--r-- | src/codegen/helpers.rs | 50 |
1 files changed, 19 insertions, 31 deletions
diff --git a/src/codegen/helpers.rs b/src/codegen/helpers.rs index 2bc3ad44..ed165aa9 100644 --- a/src/codegen/helpers.rs +++ b/src/codegen/helpers.rs @@ -60,40 +60,28 @@ pub mod attributes { /// Generates a proper type for a field or type with a given `Layout`, that is, /// a type with the correct size and alignment restrictions. -pub struct BlobTyBuilder { - layout: Layout, -} - -impl BlobTyBuilder { - pub fn new(layout: Layout) -> Self { - BlobTyBuilder { - layout: layout, +pub fn blob(layout: Layout) -> P<ast::Ty> { + let opaque = layout.opaque(); + + // FIXME(emilio, #412): We fall back to byte alignment, but there are + // some things that legitimately are more than 8-byte aligned. + // + // Eventually we should be able to `unwrap` here, but... + let ty_name = match opaque.known_rust_type_for_array() { + Some(ty) => ty, + None => { + warn!("Found unknown alignment on code generation!"); + "u8" } - } + }; - pub fn build(self) -> P<ast::Ty> { - let opaque = self.layout.opaque(); + let data_len = opaque.array_size().unwrap_or(layout.size); - // FIXME(emilio, #412): We fall back to byte alignment, but there are - // some things that legitimately are more than 8-byte aligned. - // - // Eventually we should be able to `unwrap` here, but... - let ty_name = match opaque.known_rust_type_for_array() { - Some(ty) => ty, - None => { - warn!("Found unknown alignment on code generation!"); - "u8" - } - }; - - let data_len = opaque.array_size().unwrap_or(self.layout.size); - - let inner_ty = aster::AstBuilder::new().ty().path().id(ty_name).build(); - if data_len == 1 { - inner_ty - } else { - aster::ty::TyBuilder::new().array(data_len).build(inner_ty) - } + let inner_ty = aster::AstBuilder::new().ty().path().id(ty_name).build(); + if data_len == 1 { + inner_ty + } else { + aster::ty::TyBuilder::new().array(data_len).build(inner_ty) } } |