From 0911289db829803f19002735dd75c3f20cc28f88 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Thu, 12 Sep 2019 14:47:19 -0700 Subject: [PATCH] Fixup prost-derive dependency on std-only features of failure crate --- prost-derive/Cargo.toml | 1 - prost-derive/src/lib.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/prost-derive/Cargo.toml b/prost-derive/Cargo.toml index c89da0b4e..905962bbd 100644 --- a/prost-derive/Cargo.toml +++ b/prost-derive/Cargo.toml @@ -29,7 +29,6 @@ use_std = [ "failure/std" ] default = [] [dependencies] -failure = { version = "0.1", default-features = false } itertools = "0.8" proc-macro2 = "0.4.4" quote = "0.6.3" diff --git a/prost-derive/src/lib.rs b/prost-derive/src/lib.rs index 646534a46..7460c6752 100644 --- a/prost-derive/src/lib.rs +++ b/prost-derive/src/lib.rs @@ -5,10 +5,8 @@ extern crate proc_macro; extern crate alloc; -use failure::bail; use quote::quote; -use failure::Error; use itertools::Itertools; use proc_macro::TokenStream; use proc_macro2::Span; @@ -20,7 +18,23 @@ use syn::{ mod field; use crate::field::Field; -fn try_message(input: TokenStream) -> Result { +// Note: Historically we used failure::bail!, but this requires std feature in failure +// which makes it incompatible with any no_std targets that use failure crate, since +// cargo will perform feature unification +// +// This drops std::error::Error and replaces it with String, and makes bail! produce a string. +// It also drops our dependency on failure crate entirely. +use alloc::string::String; +macro_rules! bail { + ($e:expr) => { + return Err(($e).into()); + }; + ($fmt:expr, $($arg:tt)*) => { + return Err(format!($fmt, $($arg)*)); + }; +} + +fn try_message(input: TokenStream) -> Result { let input: DeriveInput = syn::parse(input)?; let ident = input.ident; @@ -68,11 +82,11 @@ fn try_message(input: TokenStream) -> Result { } Ok(None) => None, Err(err) => Some(Err( - err.context(format!("invalid message field {}.{}", ident, field_ident)) + format!("invalid message field {}.{}\n{}", ident, field_ident, err)) )), } }) - .collect::, failure::Context>>()?; + .collect::, String>>()?; // We want Debug to be in declaration order let unsorted_fields = fields.clone(); @@ -239,7 +253,7 @@ pub fn message(input: TokenStream) -> TokenStream { try_message(input).unwrap() } -fn try_enumeration(input: TokenStream) -> Result { +fn try_enumeration(input: TokenStream) -> Result { let input: DeriveInput = syn::parse(input)?; let ident = input.ident; @@ -340,7 +354,7 @@ pub fn enumeration(input: TokenStream) -> TokenStream { try_enumeration(input).unwrap() } -fn try_oneof(input: TokenStream) -> Result { +fn try_oneof(input: TokenStream) -> Result { let input: DeriveInput = syn::parse(input)?; let ident = input.ident; @@ -382,7 +396,7 @@ fn try_oneof(input: TokenStream) -> Result { let mut tags = fields .iter() - .flat_map(|&(ref variant_ident, ref field)| -> Result { + .flat_map(|&(ref variant_ident, ref field)| -> Result { if field.tags().len() > 1 { bail!( "invalid oneof variant {}::{}: oneof variants may only have a single tag",