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

Fix RString strict encoding representation #38

Merged
merged 3 commits into from
May 6, 2024
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
6 changes: 5 additions & 1 deletion rust/src/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,14 @@ impl<C: RestrictedCharSet, C1: RestrictedCharSet, const MIN_LEN: usize, const MA
StrictEncode for RString<C, C1, MIN_LEN, MAX_LEN>
{
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
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
.register_list(&C::strict_dumb(), sizing)
.register_rstring(&C::strict_dumb(), &C1::strict_dumb(), sizing)
.write_string::<MAX_LEN>(self.as_bytes())
}
}
Expand Down
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
9 changes: 8 additions & 1 deletion 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 @@ -74,7 +75,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<AsciiString, MIN, MAX>,
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
11 changes: 10 additions & 1 deletion rust/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading