#![cfg_attr(feature = "nightly",
feature(external_doc)
)]
#![cfg_attr(feature = "nightly",
doc = "# Examples"
)]
#![cfg_attr(feature = "nightly",
doc = "```rust"
)]
#![cfg_attr(feature = "nightly",
doc(include = "../examples/do_loop.rs")
)]
#![cfg_attr(feature = "nightly",
doc = "```"
)]
#![cfg_attr(feature = "nightly",
doc = ""
)]
#![cfg_attr(feature = "nightly",
doc = "```rust"
)]
#![cfg_attr(feature = "nightly",
doc(include = "../examples/catch.rs")
)]
#![cfg_attr(feature = "nightly",
doc = "```"
)]
#![cfg_attr(feature = "try-trait",
feature(try_trait)
)]
#![no_std]
#![doc(html_root_url = "https://docs.rs/candy/0.1.0")]
#[doc(hidden)]
pub extern crate core as _core;
#[macro_export]
macro_rules! default {() => (
$crate::_core::default::Default::default()
)}
#[macro_export]
macro_rules! not {($cond:expr) => (
!$cond
)}
#[macro_export]
macro_rules! throw {($err_value:expr) => ({
#[cfg(not(feature = "try-trait"))] {
return $crate::_core::result::Result::Err($err_value.into())
}
#[cfg(feature = "try-trait")] {
return $crate::_core::ops::Try::from_err($err_value.into())
}
})}
#[macro_export]
macro_rules! ret {($ok_value:expr) => ({
#[cfg(not(feature = "try-trait"))] {
return $crate::_core::result::Result::Ok($ok_value.into())
}
#[cfg(feature = "try-trait")] {
return $crate::_core::ops::Try::from_ok($ok_value.into())
}
})}
#[doc = "# Example"]
#[cfg_attr(feature = "nightly",
doc = "```rust"
)]
#[cfg_attr(feature = "nightly",
doc(include = "../examples/catch.rs")
)]
#[cfg_attr(feature = "nightly",
doc = "```"
)]
#[cfg_attr(not(feature = "nightly"),
doc = "See [crates.io](https://crates.io/crates/candy)"
)]
#[cfg_attr(not(feature = "nightly"),
doc = "for an example."
)]
#[macro_export]
macro_rules! catch {
(
{ $($body:tt)* } -> $ok_ty:ty =>! $err_ty:ty
) => (
(|| -> $crate::_core::result::Result<$ok_ty, $err_ty>
{
#[cfg(not(feature = "try-trait"))] {
$crate::_core::result::Result::Ok({$($body)*})
}
#[cfg(feature = "try-trait")] {
$crate::_core::ops::Try::from_ok({$($body)*})
}
})()
);
{
$($body:tt)*
} => (
catch!({$($body)*} -> _ =>! _)
);
}
#[doc = "# Example"]
#[cfg_attr(feature = "nightly",
doc = "```rust"
)]
#[cfg_attr(feature = "nightly",
doc(include = "../examples/fallible.rs")
)]
#[cfg_attr(feature = "nightly",
doc = "```"
)]
#[cfg_attr(not(feature = "nightly"),
doc = "See [crates.io](https://crates.io/crates/candy)"
)]
#[cfg_attr(not(feature = "nightly"),
doc = "for an example."
)]
#[macro_export]
macro_rules! fallible {
(
$(#[$meta:meta])*
$vis:vis
fn $fname:ident $({$($ty_params:tt)*})?
($($args:tt)*)
-> $ret_ty:ty =>! $err_ty:ty
where {
$($wc:tt)*
}
$fbody:tt
) => (
$(#[$meta])*
$vis
fn $fname $(<$($ty_params)*>)?
($($args)*)
-> $crate::_core::result::Result<$ret_ty, $err_ty>
where
$($wc)*
{
let _ret = $fbody;
#[allow(unreachable_code)]
#[cfg(not(feature = "try-trait"))] {
$crate::_core::result::Result::Ok(_ret)
}
#[allow(unreachable_code)]
#[cfg(feature = "try-trait")] {
$crate::_core::ops::Try::from_ok(_ret)
}
}
);
(
$(#[$meta:meta])*
$vis:vis
fn $fname:ident {$($ty_params:tt)*}
($($args:tt)*)
-> $ret_ty:ty =>! $err_ty:ty
:
$($fbody:tt)*
) => (fallible! {
$(#[$meta])*
$vis
fn $fname {$($ty_params)*}
($($args)*)
-> $ret_ty =>! $err_ty
where {}
{$($fbody)*}
});
(
$(#[$meta:meta])*
$vis:vis
fn $fname:ident
($($args:tt)*)
-> $ret_ty:ty =>! $err_ty:ty
:
$($fbody:tt)*
) => (fallible! {
$(#[$meta])*
$vis
fn $fname {}
($($args)*)
-> $ret_ty =>! $err_ty
:
$($fbody)*
});
(
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)? fn $fname:ident $({$($ty_params:tt)*})?
($($args:tt)*)
-> $ret_ty:ty =>! $err_ty:ty
where {
$($wc:tt)*
}
) => (
$(#[$meta])*
$(pub $(($($vis)*))?)?
fn $fname $(<$($ty_params)*>)?
($($args)*)
-> ::std::result::Result<$ret_ty, $err_ty>
where
$($wc)*
;
);
(
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)? fn $fname:ident {$($ty_params:tt)*}
($($args:tt)*)
-> $ret_ty:ty =>! $err_ty:ty
) => (fallible!{
$(#[$meta])*
$(pub $(($($vis)*))?)?
fn $fname {$($ty_params)*}
($($args)*)
-> $ret_ty =>! $err_ty
where {}
});
(
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)? fn $fname:ident
($($args:tt)*)
-> $ret_ty:ty =>! $err_ty:ty
) => (fallible! {
$(#[$meta])*
$(pub $(($($vis)*))?)?
fn $fname {}
($($args)*)
-> $ret_ty =>! $err_ty
});
(
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)?
fn $fname:ident
< $($other:tt)*
) => (fallible! { @unsugaring_ty_params
$(#[$meta])*
$(pub $(($($vis)*))?)?
fn $fname
[ < ] {} $($other)*
});
(@unsugaring_ty_params
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)?
fn $fname:ident
[ < ]
$type_parameters:tt
> $($other:tt)*
) => (fallible! {
$(pub $(($($vis)*))?)?
fn $fname
$type_parameters
$($other)*
});
(@unsugaring_ty_params
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)?
fn $fname:ident
[ $($depth:tt)* ]
{$($type_parameters:tt)*}
< $($other:tt)*
) => (fallible! { @unsugaring_ty_params
$(#[$meta])*
$(pub $(($($vis)*))?)? fn $fname
[ $($depth)* < ]
{ $($type_parameters)* < }
$($other)*
});
(@unsugaring_ty_params
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)?
fn $fname:ident
[ < $($depth:tt)* ]
{$($type_parameters:tt)*}
> $($other:tt)*
) => (fallible! { @unsugaring_ty_params
$(#[$meta])*
$(pub $(($($vis)*))?)?
fn $fname
[ $($depth)* ]
{ $($type_parameters)* > }
$($other)*
});
(@unsugaring_ty_params
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)?
fn $fname:ident
$depth:tt
$type_parameters:tt
>> $($other:tt)*
) => (fallible! { @unsugaring_ty_params
$(#[$meta])*
$(pub $(($($vis)*))?)?
fn $fname
$depth
$type_parameters
> >
$($other)*
});
(@unsugaring_ty_params
$(#[$meta:meta])*
$(pub $(($($vis:tt)*))?)?
fn $fname:ident
$depth:tt
{ $($type_parameters:tt)* }
$single_tt:tt $($other:tt)*
) => (fallible! { @unsugaring_ty_params
$(#[$meta])*
$(pub $(($($vis)*))?)?
fn $fname
$depth
{ $($type_parameters)* $single_tt }
$($other)*
});
}
#[doc = "Sugar for the `do { ... } while (...);` C construct in Rust."]
#[doc = ""]
#[doc = "# Example"]
#[cfg_attr(feature = "nightly",
doc = "```rust"
)]
#[cfg_attr(feature = "nightly",
doc(include = "../examples/do_loop.rs")
)]
#[cfg_attr(feature = "nightly",
doc = "```"
)]
#[cfg_attr(not(feature = "nightly"),
doc = "See [crates.io](https://crates.io/crates/candy)"
)]
#[cfg_attr(not(feature = "nightly"),
doc = "for an example."
)]
#[macro_export]
macro_rules! do_loop {(
{ $($body:tt)* } while $cond:expr
) => (
loop {
$($body)* ;
if !$cond { break };
}
)}
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug_assert {
($($arg:tt)*) => ({ assert!($($arg)*); })
}
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug_assert {
($($arg:tt)*) => ( {} )
}
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug_assert_eq {
($($arg:tt)*) => ({ assert_eq!($($arg)*); })
}
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug_assert_eq {
($($arg:tt)*) => ( {} )
}
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug_assert_ne {
($($arg:tt)*) => ({ assert_ne!($($arg)*); })
}
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug_assert_ne {
($($arg:tt)*) => ( {} )
}