Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Merge commits '829b365', '5073786', and '40de061'
Browse files Browse the repository at this point in the history
Closes #23, #24, and #25.
  • Loading branch information
darconeous committed Apr 11, 2020
3 parents 829b365 + 5073786 + 40de061 commit 99a61ee
Show file tree
Hide file tree
Showing 10 changed files with 553 additions and 327 deletions.
208 changes: 106 additions & 102 deletions async-coap-uri/src/any_uri_ref.rs

Large diffs are not rendered by default.

158 changes: 107 additions & 51 deletions async-coap-uri/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ macro_rules! _uri_const {
///
#[macro_export]
macro_rules! uri_ref {
( unsafe $S:expr ) => {{
// We don't do any correctness checks when $S is preceded by `unsafe`.
$crate::_uri_const!($S, $crate::UriRef)
}};
( $S:expr ) => {{
$crate::assert_uri_ref_literal!($S);
$crate::_uri_const!($S, $crate::UriRef)
}};
( ) => {
$crate::uri_ref!("")
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! iuri_ref {
( unsafe $S:expr ) => {{
// We don't do any correctness checks when $S is preceded by `unsafe`.
$crate::_uri_const!($S, $crate::UriRef)
Expand Down Expand Up @@ -152,6 +168,22 @@ macro_rules! uri_ref {
/// ```
#[macro_export]
macro_rules! rel_ref {
( unsafe $S:expr ) => {{
// We don't do any correctness checks when $S is preceded by `unsafe`.
$crate::_uri_const!($S, $crate::RelRef)
}};
( $S:expr ) => {{
$crate::assert_rel_ref_literal!($S);
$crate::_uri_const!($S, $crate::RelRef)
}};
( ) => {
$crate::rel_ref!("")
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! irel_ref {
( unsafe $S:expr ) => {{
// We don't do any correctness checks when $S is preceded by `unsafe`.
$crate::_uri_const!($S, $crate::RelRef)
Expand Down Expand Up @@ -197,6 +229,30 @@ macro_rules! rel_ref {
///
#[macro_export]
macro_rules! uri {
( unsafe $S:expr ) => {{
// We don't do any correctness checks when $S is preceded by `unsafe`.
$crate::_uri_const!($S, $crate::Uri)
}};
( $S:expr ) => {{
$crate::assert_uri_literal!($S);
$crate::_uri_const!($S, $crate::Uri)
}};
( ) => {
$crate::uri!("")
};
}

#[doc(hidden)]
// This macro should be used by `async-coap-uri` and the uri macro is used for downstream crates.
//
// This prevents prevents the error:
// > macro-expanded `macro_export` macros from the current
// > crate cannot be referred to by absolute paths
//
// and allows downstream crates to use the uri macro without having to import
// the `assert_uri_literal` macro.
#[macro_export]
macro_rules! iuri {
( unsafe $S:expr ) => {{
// We don't do any correctness checks when $S is preceded by `unsafe`.
$crate::_uri_const!($S, $crate::Uri)
Expand All @@ -216,7 +272,7 @@ macro_rules! uri {
#[cfg(feature = "std")]
#[macro_export]
macro_rules! uri_ref_format {
($($arg:tt)*) => ($crate::UriRefBuf::from_string(format!($($arg)*)))
($($arg:tt)*) => ($crate::UriRefBuf::from_string(::std::format!($($arg)*)))
}

/// Creates a `Option<UriBuf>` from the given string format and arguments.
Expand All @@ -225,7 +281,7 @@ macro_rules! uri_ref_format {
#[cfg(feature = "std")]
#[macro_export]
macro_rules! uri_format {
($($arg:tt)*) => ($crate::UriBuf::from_string(format!($($arg)*)))
($($arg:tt)*) => ($crate::UriBuf::from_string(::std::format!($($arg)*)))
}

/// Creates a `Option<RelRefBuf>` from the given string format and arguments.
Expand All @@ -234,47 +290,48 @@ macro_rules! uri_format {
#[cfg(feature = "std")]
#[macro_export]
macro_rules! rel_ref_format {
($($arg:tt)*) => ($crate::RelRefBuf::from_string(format!($($arg)*)))
($($arg:tt)*) => ($crate::RelRefBuf::from_string(::std::format!($($arg)*)))
}

#[doc(hidden)]
#[macro_export]
macro_rules! _impl_uri_traits {
( $C:ty ) => {
impl<T: AsRef<str> + ?Sized> core::cmp::PartialEq<T> for $C {
impl<T: ::core::convert::AsRef<str> + ?::core::marker::Sized> ::core::cmp::PartialEq<T>
for $C
{
fn eq(&self, other: &T) -> bool {
core::cmp::PartialEq::eq(self.as_str(), other.as_ref())
::core::cmp::PartialEq::eq(self.as_str(), other.as_ref())
}
}

impl<T: AsRef<str> + ?Sized> core::cmp::PartialOrd<T> for $C {
fn partial_cmp(&self, other: &T) -> Option<::std::cmp::Ordering> {
core::cmp::PartialOrd::partial_cmp(self.as_str(), other.as_ref())
impl<T: ::core::convert::AsRef<str> + ?::core::marker::Sized> ::core::cmp::PartialOrd<T>
for $C
{
fn partial_cmp(&self, other: &T) -> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(self.as_str(), other.as_ref())
}
}

impl core::cmp::Ord for $C {
impl ::core::cmp::Ord for $C {
fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
core::cmp::Ord::cmp(self.as_str(), other.as_str())
::core::cmp::Ord::cmp(self.as_str(), other.as_str())
}
}

impl std::fmt::Debug for $C {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::result::Result<(), std::fmt::Error> {
impl ::core::fmt::Debug for $C {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str(concat!(stringify!($C), "<"))?;
std::fmt::Display::fmt(self.as_str(), f)?;
::core::fmt::Display::fmt(self.as_str(), f)?;
f.write_str(">")
}
}
impl AsRef<str> for $C {
impl ::core::convert::AsRef<str> for $C {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<$C> for $C {
impl ::core::convert::AsRef<$C> for $C {
fn as_ref(&self) -> &$C {
&self
}
Expand All @@ -288,15 +345,17 @@ macro_rules! _impl_uri_traits_base {
( $C:ty ) => {
_impl_uri_traits!($C);

impl core::convert::From<&$C> for std::string::String {
impl ::core::convert::From<&$C> for ::std::string::String {
fn from(x: &$C) -> Self {
String::from(&x.0)
::std::string::String::from(&x.0)
}
}

impl core::convert::From<&$C> for $crate::UriRefBuf {
impl ::core::convert::From<&$C> for $crate::UriRefBuf {
fn from(x: &$C) -> Self {
unsafe { $crate::UriRefBuf::from_string_unchecked(String::from(&x.0)) }
unsafe {
$crate::UriRefBuf::from_string_unchecked(::std::string::String::from(&x.0))
}
}
}
};
Expand All @@ -309,7 +368,7 @@ macro_rules! impl_uri_traits {
_impl_uri_traits_base!($C);

impl $crate::AnyUriRef for $C {
fn components(&self) -> UriRawComponents<'_> {
fn components(&self) -> $crate::UriRawComponents<'_> {
self.0.components()
}

Expand All @@ -325,10 +384,10 @@ macro_rules! impl_uri_traits {
self.0.to_uri_ref_buf()
}

unsafe fn write_to_unsafe<W: core::fmt::Write + ?Sized>(
unsafe fn write_to_unsafe<W: ::core::fmt::Write + ?::core::marker::Sized>(
&self,
write: &mut W,
) -> Result<(), core::fmt::Error> {
) -> ::core::fmt::Result {
self.0.write_to_unsafe(write)
}
}
Expand All @@ -341,63 +400,60 @@ macro_rules! _impl_uri_buf_traits_base {
( $C:ty , $B:ty ) => {
_impl_uri_traits!($C);

impl core::convert::From<$C> for std::string::String {
impl ::core::convert::From<$C> for ::std::string::String {
fn from(x: $C) -> Self {
String::from(x.0)
::std::string::String::from(x.0)
}
}

impl core::convert::From<&$C> for $C {
impl ::core::convert::From<&$C> for $C {
fn from(x: &$C) -> Self {
x.clone()
<$C as ::core::clone::Clone>::clone(x)
}
}

impl std::borrow::ToOwned for $B {
impl ::std::borrow::ToOwned for $B {
type Owned = $C;

fn to_owned(&self) -> Self::Owned {
unsafe { <$C>::from_string_unchecked(self.to_string()) }
unsafe {
<$C>::from_string_unchecked(<Self as ::std::string::ToString>::to_string(self))
}
}
}

impl core::borrow::Borrow<$B> for $C {
impl ::core::borrow::Borrow<$B> for $C {
fn borrow(&self) -> &$B {
unsafe { <$B>::from_str_unchecked(self.as_str()) }
}
}

impl $crate::AnyUriRef for $C {
fn components(&self) -> UriRawComponents<'_> {
use core::borrow::Borrow;
let b: &$B = self.borrow();
fn components(&self) -> $crate::UriRawComponents<'_> {
let b: &$B = <Self as ::core::borrow::Borrow<$B>>::borrow(self);
b.components()
}

fn is_empty(&self) -> bool {
use core::borrow::Borrow;
let b: &$B = self.borrow();
let b: &$B = <Self as ::core::borrow::Borrow<$B>>::borrow(self);
b.is_empty()
}

fn uri_type(&self) -> $crate::UriType {
use core::borrow::Borrow;
let b: &$B = self.borrow();
let b: &$B = <Self as ::core::borrow::Borrow<$B>>::borrow(self);
b.uri_type()
}

fn to_uri_ref_buf(&self) -> $crate::UriRefBuf {
use core::borrow::Borrow;
let b: &$B = self.borrow();
let b: &$B = <Self as ::core::borrow::Borrow<$B>>::borrow(self);
b.to_uri_ref_buf()
}

unsafe fn write_to_unsafe<W: core::fmt::Write + ?Sized>(
unsafe fn write_to_unsafe<W: ::core::fmt::Write + ?::core::marker::Sized>(
&self,
write: &mut W,
) -> Result<(), core::fmt::Error> {
use core::borrow::Borrow;
let b: &$B = self.borrow();
) -> ::core::fmt::Result {
let b: &$B = <Self as ::core::borrow::Borrow<$B>>::borrow(self);
b.write_to_unsafe(write)
}
}
Expand All @@ -410,21 +466,21 @@ macro_rules! impl_uri_buf_traits {
( $C:ty , $B:ty) => {
_impl_uri_buf_traits_base!($C, $B);

impl AsRef<std::string::String> for $C {
impl ::core::convert::AsRef<::std::string::String> for $C {
fn as_ref(&self) -> &std::string::String {
AsRef::<std::string::String>::as_ref(&self.0)
::core::convert::AsRef::<::std::string::String>::as_ref(&self.0)
}
}

impl AsRef<$crate::UriRefBuf> for $C {
impl ::core::convert::AsRef<$crate::UriRefBuf> for $C {
fn as_ref(&self) -> &$crate::UriRefBuf {
AsRef::<$crate::UriRefBuf>::as_ref(&self.0)
::core::convert::AsRef::<$crate::UriRefBuf>::as_ref(&self.0)
}
}

impl core::convert::From<$C> for $crate::UriRefBuf {
impl ::core::convert::From<$C> for $crate::UriRefBuf {
fn from(x: $C) -> Self {
x.0.into()
::core::convert::Into::<Self>::into(x.0)
}
}
};
Expand Down

0 comments on commit 99a61ee

Please sign in to comment.