From 7c46c97c10655612a1c71b984faa62ffc543b226 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 25 Apr 2024 10:17:17 +0200 Subject: [PATCH 1/3] fix encoding RString's --- rust/src/embedded.rs | 2 +- rust/src/traits.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/rust/src/embedded.rs b/rust/src/embedded.rs index e084d75..fccddfa 100644 --- a/rust/src/embedded.rs +++ b/rust/src/embedded.rs @@ -475,7 +475,7 @@ impl(self.as_bytes()) } } diff --git a/rust/src/traits.rs b/rust/src/traits.rs index c09f1dc..7994aee 100644 --- a/rust/src/traits.rs +++ b/rust/src/traits.rs @@ -97,11 +97,20 @@ pub trait TypedWrite: Sized { #[doc(hidden)] unsafe fn register_unicode(self, sizing: Sizing) -> Self { self } #[doc(hidden)] - #[deprecated(since = "2.3.1", note = "use register_list with AsciiSym type")] + #[deprecated(since = "2.3.1", note = "use register_rstring")] unsafe fn register_ascii(self, sizing: Sizing) -> Self { panic!("TypedWrite::register_ascii must not be called; pls see compilation warnings") } #[doc(hidden)] + unsafe fn register_rstring( + self, + c: &impl StrictEncode, + c1: &impl StrictEncode, + sizing: Sizing, + ) -> Self { + self + } + #[doc(hidden)] unsafe fn register_list(self, ty: &impl StrictEncode, sizing: Sizing) -> Self { self } #[doc(hidden)] unsafe fn register_set(self, ty: &impl StrictEncode, sizing: Sizing) -> Self { self } From 7fbf31b2bc0f117cba5ae676e46e4ef3656616f5 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 25 Apr 2024 14:34:37 +0200 Subject: [PATCH 2/3] prohibit zero-length restricted strings --- rust/src/embedded.rs | 4 ++++ rust/src/stl.rs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/rust/src/embedded.rs b/rust/src/embedded.rs index fccddfa..e350382 100644 --- a/rust/src/embedded.rs +++ b/rust/src/embedded.rs @@ -472,6 +472,10 @@ impl { fn strict_encode(&self, writer: W) -> io::Result { + debug_assert_ne!( + MIN_LEN, 0, + "Restricted string type can't have minimum length equal to zero" + ); let sizing = Sizing::new(MIN_LEN as u64, MAX_LEN as u64); unsafe { writer diff --git a/rust/src/stl.rs b/rust/src/stl.rs index 70d164d..ff4df41 100644 --- a/rust/src/stl.rs +++ b/rust/src/stl.rs @@ -74,7 +74,7 @@ pub trait RestrictedCharSet: pub struct RString< C1: RestrictedCharSet, C: RestrictedCharSet = C1, - const MIN: usize = 0, + const MIN: usize = 1, const MAX: usize = 255, > { s: Confined, From 901e26ee8b01b3bcff4d7ac76acff2fcc633634b Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Fri, 26 Apr 2024 13:45:23 +0200 Subject: [PATCH 3/3] impl Borrow for RString --- rust/src/ident.rs | 9 ++++++--- rust/src/stl.rs | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/rust/src/ident.rs b/rust/src/ident.rs index c392699..e919695 100644 --- a/rust/src/ident.rs +++ b/rust/src/ident.rs @@ -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; @@ -46,14 +45,18 @@ macro_rules! impl_ident_type { fn try_from(s: String) -> Result { 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::()) .field(&self.as_str()) .finish() } } + impl ::core::borrow::Borrow for $ty { + fn borrow(&self) -> &str { self.as_str() } + } + impl $ty { /// Returns string reference. pub fn as_str(&self) -> &str { self.0.as_str() } diff --git a/rust/src/stl.rs b/rust/src/stl.rs index ff4df41..5603858 100644 --- a/rust/src/stl.rs +++ b/rust/src/stl.rs @@ -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; @@ -102,6 +103,12 @@ impl &str { self.s.as_str() } } +impl Borrow + for RString +{ + fn borrow(&self) -> &str { self.s.as_str() } +} + impl FromStr for RString {