Skip to content

Commit

Permalink
impl Borrow<str> for RString
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Apr 26, 2024
1 parent 7fbf31b commit 901e26e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions rust/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::{self, Debug, Formatter};
use std::str::FromStr;

use amplify::Wrapper;
Expand All @@ -46,14 +45,18 @@ macro_rules! impl_ident_type {
fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
}

impl Debug for $ty {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
impl ::core::fmt::Debug for $ty {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple(&$crate::type_name::<Self>())
.field(&self.as_str())
.finish()
}
}

impl ::core::borrow::Borrow<str> for $ty {
fn borrow(&self) -> &str { self.as_str() }
}

impl $ty {
/// Returns string reference.
pub fn as_str(&self) -> &str { self.0.as_str() }
Expand Down
7 changes: 7 additions & 0 deletions rust/src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#![allow(non_camel_case_types, unused_imports)]

use std::borrow::Borrow;
use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;
use std::ops::Deref;
Expand Down Expand Up @@ -102,6 +103,12 @@ impl<C1: RestrictedCharSet, C: RestrictedCharSet, const MIN: usize, const MAX: u
fn as_ref(&self) -> &str { self.s.as_str() }
}

impl<C1: RestrictedCharSet, C: RestrictedCharSet, const MIN: usize, const MAX: usize> Borrow<str>
for RString<C1, C, MIN, MAX>
{
fn borrow(&self) -> &str { self.s.as_str() }
}

impl<C1: RestrictedCharSet, C: RestrictedCharSet, const MIN: usize, const MAX: usize> FromStr
for RString<C1, C, MIN, MAX>
{
Expand Down

0 comments on commit 901e26e

Please sign in to comment.