Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustfmt macros #22

Merged
merged 2 commits into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion constructor/src/fixed_hash/core/builtin/std_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl HashConstructor {
let part = quote!(
impl ::std::default::Default for #name {
#[inline]
fn default() -> Self {
fn default() -> Self {
Self::zero()
}
}
Expand Down
12 changes: 6 additions & 6 deletions constructor/src/fixed_hash/core/builtin/std_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ impl HashConstructor {
write!(f, #write_tpl_padded, data[4])?;
write!(f, #write_tpl_padded, data[5])?;
write!(f, #omit)?;
write!(f, #write_tpl_padded, data[#unit_amount-6])?;
write!(f, #write_tpl_padded, data[#unit_amount-5])?;
write!(f, #write_tpl_padded, data[#unit_amount-4])?;
write!(f, #write_tpl_padded, data[#unit_amount-3])?;
write!(f, #write_tpl_padded, data[#unit_amount-2])?;
write!(f, #write_tpl_padded, data[#unit_amount-1])?;
write!(f, #write_tpl_padded, data[#unit_amount - 6])?;
write!(f, #write_tpl_padded, data[#unit_amount - 5])?;
write!(f, #write_tpl_padded, data[#unit_amount - 4])?;
write!(f, #write_tpl_padded, data[#unit_amount - 3])?;
write!(f, #write_tpl_padded, data[#unit_amount - 2])?;
write!(f, #write_tpl_padded, data[#unit_amount - 1])?;
)
} else {
quote!(#(write!(f, #loop_write_tpl_padded, data[#loop_unit_amount])?;)*)
Expand Down
23 changes: 10 additions & 13 deletions constructor/src/fixed_hash/core/builtin/std_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl HashConstructor {
let part = quote!(
impl<'a, Rhs> ::std::ops::#trait_name<Rhs> for &'a #name
where
Rhs: ::std::convert::Into<#name>
Rhs: ::std::convert::Into<#name>,
{
type Output = #name;
#[inline]
Expand All @@ -48,7 +48,7 @@ impl HashConstructor {
}
impl<Rhs> ::std::ops::#trait_name<Rhs> for #name
where
Rhs: ::std::convert::Into<#name>
Rhs: ::std::convert::Into<#name>,
{
type Output = #name;
#[inline]
Expand All @@ -58,31 +58,28 @@ impl HashConstructor {
}
impl<Rhs> ::std::ops::#trait_assign_name<Rhs> for #name
where
Rhs: ::std::convert::Into<#name>
Rhs: ::std::convert::Into<#name>,
{
#[inline]
fn #func_assign_name(&mut self, other: Rhs) {
*self = self.#real_func(&other.into());
}
}
impl<'a, 'b> ::std::ops::#trait_name<&'b #name> for &'a #name
{
impl<'a, 'b> ::std::ops::#trait_name<&'b #name> for &'a #name {
type Output = #name;
#[inline]
fn #func_name(self, other: &#name) -> Self::Output {
self.#real_func(other)
}
}
impl<'a> ::std::ops::#trait_name<&'a #name> for #name
{
impl<'a> ::std::ops::#trait_name<&'a #name> for #name {
type Output = #name;
#[inline]
fn #func_name(self, other: &#name) -> Self::Output {
self.#real_func(other)
}
}
impl<'a> ::std::ops::#trait_assign_name<&'a #name> for #name
{
impl<'a> ::std::ops::#trait_assign_name<&'a #name> for #name {
#[inline]
fn #func_assign_name(&mut self, other: &#name) {
*self = self.#real_func(other);
Expand Down Expand Up @@ -124,7 +121,7 @@ impl HashConstructor {
let uint_name = utils::ident_to_ts(uint_name);
let real_func = utils::ident_to_ts(format!("_ush{}", direction).as_ref());
let part = quote!(
impl<'a,'b> ::std::ops::#trait_name<&'a #uint_name> for &'b #name {
impl<'a, 'b> ::std::ops::#trait_name<&'a #uint_name> for &'b #name {
type Output = #name;
#[inline]
fn #func_name(self, other: &#uint_name) -> Self::Output {
Expand Down Expand Up @@ -173,7 +170,7 @@ impl HashConstructor {
let int_name = utils::ident_to_ts(int_name);
let real_func = utils::ident_to_ts(format!("_ish{}", direction).as_ref());
let part = quote!(
impl<'a,'b> ::std::ops::#trait_name<&'a #int_name> for &'b #name {
impl<'a, 'b> ::std::ops::#trait_name<&'a #int_name> for &'b #name {
type Output = #name;
#[inline]
fn #func_name(self, other: &#int_name) -> Self::Output {
Expand Down Expand Up @@ -225,7 +222,7 @@ impl HashConstructor {
let part = quote!(
impl<Idx> ::std::ops::Index<Idx> for #name
where
Idx: ::std::slice::SliceIndex<[u8], Output = [u8]>
Idx: ::std::slice::SliceIndex<[u8], Output = [u8]>,
{
type Output = Idx::Output;
#[inline]
Expand All @@ -235,7 +232,7 @@ impl HashConstructor {
}
impl<Idx> ::std::ops::IndexMut<Idx> for #name
where
Idx: ::std::slice::SliceIndex<[u8], Output = [u8]>
Idx: ::std::slice::SliceIndex<[u8], Output = [u8]>,
{
#[inline]
fn index_mut(&mut self, index: Idx) -> &mut Idx::Output {
Expand Down
2 changes: 1 addition & 1 deletion constructor/src/fixed_hash/core/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl HashConstructor {
let part = quote!(
/// Fixed hash type.
#[derive(Clone)]
pub struct #name (pub #inner_type);
pub struct #name(pub #inner_type);
);
self.attach_hash(part);
}
Expand Down
33 changes: 26 additions & 7 deletions constructor/src/fixed_hash/core/extension/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,46 @@ impl HashConstructor {
let part = quote!(
#[cfg(feature = "support_serde")]
impl serde::Serialize for #name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let bytes = self.as_bytes();
let mut dst = [0u8; #bytes_size * 2 + 2];
dst[0] = b'0';
dst[1] = b'x';
faster_hex::hex_to(bytes, &mut dst[2..])
.map_err(|e| serde::ser::Error::custom(&format!("{}", e)))?;
serializer.serialize_str(unsafe {::std::str::from_utf8_unchecked(&dst)})
serializer.serialize_str(unsafe { ::std::str::from_utf8_unchecked(&dst) })
}
}

#[cfg(feature = "support_serde")]
impl<'de> serde::Deserialize<'de> for #name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor;

impl<'b> serde::de::Visitor<'b> for Visitor {
type Value = #name;

fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(formatter, "a 0x-prefixed hex string with {} digits", #bytes_size * 2)
fn expecting(
&self,
formatter: &mut ::std::fmt::Formatter,
) -> ::std::fmt::Result {
write!(
formatter,
"a 0x-prefixed hex string with {} digits",
#bytes_size * 2
)
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: serde::de::Error {
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v.len() != #bytes_size * 2 + 2 {
return Err(E::invalid_length(v.len() - 2, &self));
}
Expand All @@ -67,7 +83,10 @@ impl HashConstructor {
})
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E> where E: serde::de::Error {
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str(&v)
}
}
Expand Down
5 changes: 3 additions & 2 deletions constructor/src/fixed_hash/core/internal/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ impl HashConstructor {
let that_bytes_size = &uc.ts.unit_amount;
quote!(
let mut ret = #that_name::zero();
ret.mut_inner().copy_from_slice(&self.inner()[..#that_bytes_size]);
ret.mut_inner()
.copy_from_slice(&self.inner()[..#that_bytes_size]);
(ret, true)
)
};
Expand All @@ -61,7 +62,7 @@ impl HashConstructor {
/// Create a new fixed hash with a provided input.
#[inline]
const fn new(data: #inner_type) -> Self {
#name (data)
#name(data)
}
/// Get a reference of the inner data of the fixed hash.
#[inline]
Expand Down
50 changes: 41 additions & 9 deletions constructor/src/fixed_uint/core/builtin/as_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,53 @@ impl UintConstructor {
#[inline]
pub fn checked_add(&self, rhs: &Self) -> Option<Self> {
let (ret, of) = self._add(rhs);
if of { None } else { Some(ret) }
if of {
None
} else {
Some(ret)
}
}
/// Checked integer subtraction. Computes `self - rhs`,
/// returning `None` if overflow occurred.
#[inline]
pub fn checked_sub(&self, rhs: &Self) -> Option<Self> {
let (ret, of) = self._sub(rhs);
if of { None } else { Some(ret) }
if of {
None
} else {
Some(ret)
}
}
/// Checked integer multiplication. Computes `self * rhs`,
/// returning `None` if overflow occurred.
#[inline]
pub fn checked_mul(&self, rhs: &Self) -> Option<Self> {
let (ret, of) = self._mul(rhs);
if of { None } else { Some(ret) }
if of {
None
} else {
Some(ret)
}
}
/// Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`.
#[inline]
pub fn checked_div(&self, rhs: &Self) -> Option<Self> {
let (ret, of) = self._div(rhs);
if of { None } else { Some(ret) }
if of {
None
} else {
Some(ret)
}
}
/// Checked integer remainder. Computes `self % rhs`, returning `None` if `rhs == 0`.
#[inline]
pub fn checked_rem(&self, rhs: &Self) -> Option<Self> {
let (ret, of) = self._rem(rhs);
if of { None } else { Some(ret) }
if of {
None
} else {
Some(ret)
}
}
/// Checked shift left. Computes `self << rhs`,
/// returning `None` if `rhs` is larger than or equal to the number of bits in `self`.
Expand Down Expand Up @@ -180,21 +200,33 @@ impl UintConstructor {
#[inline]
pub fn saturating_add(&self, rhs: &Self) -> Self {
let (ret, of) = self._add(rhs);
if of { Self::max_value() } else { ret }
if of {
Self::max_value()
} else {
ret
}
}
/// Checked integer subtraction. Computes `self - rhs`,
/// returning `None` if overflow occurred.
#[inline]
pub fn saturating_sub(&self, rhs: &Self) -> Self {
let (ret, of) = self._sub(rhs);
if of { Self::zero() } else { ret }
if of {
Self::zero()
} else {
ret
}
}
/// Checked integer multiplication. Computes `self * rhs`,
/// returning `None` if overflow occurred.
#[inline]
pub fn saturating_mul(&self, rhs: &Self) -> Self {
let (ret, of) = self._mul(rhs);
if of { Self::max_value() } else { ret }
if of {
Self::max_value()
} else {
ret
}
}
);
self.defun(part);
Expand Down Expand Up @@ -301,7 +333,7 @@ impl UintConstructor {
if of {
unreachable!();
}
(val , true)
(val, true)
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion constructor/src/fixed_uint/core/builtin/std_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl UintConstructor {
let part = quote!(
impl ::std::default::Default for #name {
#[inline]
fn default() -> Self {
fn default() -> Self {
Self::zero()
}
}
Expand Down
4 changes: 2 additions & 2 deletions constructor/src/fixed_uint/core/builtin/std_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ impl UintConstructor {
write!(f, #write_tpl, data[idx])?;
idx -= 1;
while idx > 0 {
write!(f, #write_tpl_padded, data[idx], width=#width)?;
write!(f, #write_tpl_padded, data[idx], width = #width)?;
idx -= 1;
}
write!(f, #write_tpl_padded, data[0], width=#width)
write!(f, #write_tpl_padded, data[0], width = #width)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions constructor/src/fixed_uint/core/builtin/std_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl UintConstructor {
#[inline]
fn sum<I>(iter: I) -> Self
where
I: ::std::iter::Iterator<Item = &'a #name>
I: ::std::iter::Iterator<Item = &'a #name>,
{
iter.fold(Self::zero(), ::std::ops::Add::add)
}
Expand All @@ -35,7 +35,7 @@ impl UintConstructor {
#[inline]
fn sum<I>(iter: I) -> Self
where
I: ::std::iter::Iterator<Item = #name>
I: ::std::iter::Iterator<Item = #name>,
{
iter.fold(Self::zero(), ::std::ops::Add::add)
}
Expand All @@ -51,7 +51,7 @@ impl UintConstructor {
#[inline]
fn product<I>(iter: I) -> Self
where
I: ::std::iter::Iterator<Item = &'a #name>
I: ::std::iter::Iterator<Item = &'a #name>,
{
iter.fold(Self::one(), ::std::ops::Mul::mul)
}
Expand All @@ -60,7 +60,7 @@ impl UintConstructor {
#[inline]
fn product<I>(iter: I) -> Self
where
I: ::std::iter::Iterator<Item = #name>
I: ::std::iter::Iterator<Item = #name>,
{
iter.fold(Self::one(), ::std::ops::Mul::mul)
}
Expand Down
Loading