Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
strategy:
fail-fast: false
matrix:
toolchain: [ nightly, beta, stable, 1.77.0 ]
toolchain: [ nightly, beta, stable, 1.81.0 ]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
Expand Down
19 changes: 3 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ default-members = ["rust", "rust/derive", "rust/test_helpers"]
resolver = "2"

[workspace.package]
version = "2.8.1"
version = "3.0.0-alpha.1"
authors = ["Dr Maxim Orlovsky <[email protected]>"]
homepage = "https://strict-types.org"
repository = "https://github.com/strict-types/strict-encoding"
rust-version = "1.77.0"
rust-version = "1.81.0"
edition = "2021"
license = "Apache-2.0"

[workspace.dependencies]
amplify = { version = "4.8.0" }
amplify = { version = "4.8.0", default-features = false }
serde_crate = { package = "serde", version = "1", features = ["derive"] }
13 changes: 0 additions & 13 deletions MANIFEST.yml

This file was deleted.

8 changes: 5 additions & 3 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ readme = "README.md"
exclude = ["derive", "test_helpers"]

[dependencies]
amplify = { workspace = true, features = ["proc_attr"] }
strict_encoding_derive = { version = "2.8.0", path = "derive" }
amplify = { workspace = true, features = ["proc_attr", "hex"] }
strict_encoding_derive = { version = "3.0.0-alpha.1", path = "derive" }
serde_crate = { workspace = true, optional = true }

[dev-dependencies]
Expand All @@ -32,13 +32,15 @@ wasm-bindgen-test = "0.3"

[features]
default = [
"derive"
"derive",
]
all = [
"std",
"float",
"derive",
"serde"
]
std = ["amplify/std", "serde_crate/std"]
derive = []
float = [
"amplify/apfloat",
Expand Down
6 changes: 3 additions & 3 deletions rust/derive/src/derive_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl DeriveInner for DeriveEncode<'_> {
}

Ok(quote! {
fn strict_encode<W: #crate_name::TypedWrite>(&self, writer: W) -> ::std::io::Result<W> {
fn strict_encode<W: #crate_name::TypedWrite>(&self, writer: W) -> Result<W, #crate_name::WriteError> {
use #crate_name::{TypedWrite, WriteStruct, fname};
writer.write_struct::<Self>(|w| {
Ok(w
Expand All @@ -80,7 +80,7 @@ impl DeriveInner for DeriveEncode<'_> {
});

Ok(quote! {
fn strict_encode<W: #crate_name::TypedWrite>(&self, writer: W) -> ::std::io::Result<W> {
fn strict_encode<W: #crate_name::TypedWrite>(&self, writer: W) -> Result<W, #crate_name::WriteError> {
use #crate_name::{TypedWrite, WriteTuple};
writer.write_tuple::<Self>(|w| {
Ok(w
Expand Down Expand Up @@ -203,7 +203,7 @@ impl DeriveInner for DeriveEncode<'_> {
};

Ok(quote! {
fn strict_encode<W: #crate_name::TypedWrite>(&self, writer: W) -> ::std::io::Result<W> {
fn strict_encode<W: #crate_name::TypedWrite>(&self, writer: W) -> Result<W, #crate_name::WriteError> {
use #crate_name::TypedWrite;
#inner
}
Expand Down
57 changes: 29 additions & 28 deletions rust/src/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::hash::Hash;
use std::io;
use std::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8};
use alloc::collections::{BTreeMap, BTreeSet, VecDeque};
use core::hash::Hash;
use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8};

use amplify::ascii::AsciiString;
use amplify::confinement::Confined;
Expand All @@ -35,8 +34,8 @@ use crate::stl::AsciiSym;
use crate::{
DecodeError, DefineUnion, Primitive, RString, ReadRaw, ReadTuple, ReadUnion, RestrictedCharSet,
Sizing, StrictDecode, StrictDumb, StrictEncode, StrictProduct, StrictStruct, StrictSum,
StrictTuple, StrictType, StrictUnion, TypeName, TypedRead, TypedWrite, WriteRaw, WriteTuple,
WriteUnion, LIB_EMBEDDED,
StrictTuple, StrictType, StrictUnion, TypeName, TypedRead, TypedWrite, WriteError, WriteRaw,
WriteTuple, WriteUnion, LIB_EMBEDDED,
};

pub trait DecodeRawLe: Sized {
Expand All @@ -53,7 +52,7 @@ pub trait DecodeRawLe: Sized {
pub struct Byte(u8);

impl StrictEncode for Byte {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, WriteError> {
unsafe {
writer = writer.register_primitive(Primitive::BYTE);
writer.raw_writer().write_raw::<1>([self.0])?;
Expand All @@ -69,7 +68,7 @@ macro_rules! encode_num {
fn strict_name() -> Option<TypeName> { Some(tn!(stringify!($id))) }
}
impl $crate::StrictEncode for $ty {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, $crate::WriteError> {
unsafe {
writer = writer.register_primitive(Primitive::$id);
writer.raw_writer().write_raw_array(self.to_le_bytes())?;
Expand All @@ -78,13 +77,15 @@ macro_rules! encode_num {
}
}
impl $crate::DecodeRawLe for $ty {
fn decode_raw_le(reader: &mut (impl ReadRaw + ?Sized)) -> Result<Self, DecodeError> {
fn decode_raw_le(
reader: &mut (impl ReadRaw + ?Sized),
) -> Result<Self, $crate::DecodeError> {
let buf = reader.read_raw_array::<{ Self::BITS as usize / 8 }>()?;
Ok(Self::from_le_bytes(buf))
}
}
impl $crate::StrictDecode for $ty {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, $crate::DecodeError> {
Self::decode_raw_le(unsafe { reader.raw_reader() })
}
}
Expand All @@ -98,7 +99,7 @@ macro_rules! encode_nonzero {
fn strict_name() -> Option<TypeName> { Some(tn!(stringify!($id))) }
}
impl $crate::StrictEncode for $ty {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, $crate::WriteError> {
unsafe {
writer = writer.register_primitive(Primitive::$id);
writer.raw_writer().write_raw_array(self.get().to_le_bytes())?;
Expand All @@ -107,7 +108,7 @@ macro_rules! encode_nonzero {
}
}
impl $crate::StrictDecode for $ty {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, $crate::DecodeError> {
let buf =
unsafe { reader.raw_reader().read_raw_array::<{ Self::BITS as usize / 8 }>()? };
let v = <$p>::from_le_bytes(buf);
Expand All @@ -126,7 +127,7 @@ macro_rules! encode_float {
}
#[cfg(feature = "float")]
impl $crate::StrictEncode for $ty {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, $crate::WriteError> {
let mut be = [0u8; $len];
be.copy_from_slice(&self.to_bits().to_le_bytes()[..$len]);
unsafe {
Expand All @@ -138,7 +139,7 @@ macro_rules! encode_float {
}
#[cfg(feature = "float")]
impl $crate::StrictDecode for $ty {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, $crate::DecodeError> {
const BYTES: usize = <$ty>::BITS / 8;
let mut inner = [0u8; 32];
let buf = unsafe { reader.raw_reader().read_raw_array::<BYTES>()? };
Expand Down Expand Up @@ -211,7 +212,7 @@ where T: Default + StrictStruct
impl<T> StrictEncode for Box<T>
where T: StrictEncode
{
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
self.as_ref().strict_encode(writer)
}
}
Expand Down Expand Up @@ -242,7 +243,7 @@ where T: StrictType
}
impl<T> StrictUnion for Option<T> where T: StrictType {}
impl<T: StrictEncode + StrictDumb> StrictEncode for Option<T> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
writer.write_union::<Self>(|u| {
let u = u.define_unit(vname!("none")).define_newtype::<T>(vname!("some")).complete();

Expand All @@ -269,7 +270,7 @@ impl StrictType for () {
fn strict_name() -> Option<TypeName> { None }
}
impl StrictEncode for () {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
Ok(unsafe { writer.register_primitive(Primitive::UNIT) })
}
}
Expand All @@ -286,7 +287,7 @@ impl<A: StrictType + Default, B: StrictType + Default> StrictTuple for (A, B) {
const FIELD_COUNT: u8 = 2;
}
impl<A: StrictEncode + Default, B: StrictEncode + Default> StrictEncode for (A, B) {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
writer.write_tuple::<Self>(|w| Ok(w.write_field(&self.0)?.write_field(&self.1)?.complete()))
}
}
Expand Down Expand Up @@ -316,7 +317,7 @@ impl<A: StrictType + Default, B: StrictType + Default, C: StrictType + Default>
impl<A: StrictEncode + Default, B: StrictEncode + Default, C: StrictEncode + Default> StrictEncode
for (A, B, C)
{
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
writer.write_tuple::<Self>(|w| {
Ok(w.write_field(&self.0)?.write_field(&self.1)?.write_field(&self.2)?.complete())
})
Expand All @@ -340,7 +341,7 @@ impl<T: StrictType + Copy + StrictDumb, const LEN: usize> StrictType for [T; LEN
fn strict_name() -> Option<TypeName> { None }
}
impl<T: StrictEncode + Copy + StrictDumb, const LEN: usize> StrictEncode for [T; LEN] {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, WriteError> {
for item in self {
writer = item.strict_encode(writer)?;
}
Expand Down Expand Up @@ -372,7 +373,7 @@ impl<T: StrictType + StrictDumb + Copy, const LEN: usize, const REVERSE_STR: boo
impl<T: StrictEncode + StrictDumb + Copy, const LEN: usize, const REVERSE_STR: bool> StrictEncode
for Array<T, LEN, REVERSE_STR>
{
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
self.as_inner().strict_encode(writer)
}
}
Expand All @@ -391,7 +392,7 @@ impl<const MIN_LEN: usize, const MAX_LEN: usize> StrictType for Confined<String,
impl<const MIN_LEN: usize, const MAX_LEN: usize> StrictEncode
for Confined<String, MIN_LEN, MAX_LEN>
{
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
unsafe {
writer
.register_unicode(Sizing::new(MIN_LEN as u64, MAX_LEN as u64))
Expand All @@ -418,7 +419,7 @@ impl<const MIN_LEN: usize, const MAX_LEN: usize> StrictType
impl<const MIN_LEN: usize, const MAX_LEN: usize> StrictEncode
for Confined<AsciiString, MIN_LEN, MAX_LEN>
{
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
unsafe {
writer
.register_list(
Expand Down Expand Up @@ -460,7 +461,7 @@ impl<C1: RestrictedCharSet, C: RestrictedCharSet, const MIN_LEN: usize, const MA
impl<C1: RestrictedCharSet, C: RestrictedCharSet, const MIN_LEN: usize, const MAX_LEN: usize>
StrictEncode for RString<C1, C, MIN_LEN, MAX_LEN>
{
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> Result<W, WriteError> {
debug_assert_ne!(
MIN_LEN, 0,
"Restricted string type can't have minimum length equal to zero"
Expand Down Expand Up @@ -491,7 +492,7 @@ impl<T: StrictType, const MIN_LEN: usize, const MAX_LEN: usize> StrictType
impl<T: StrictEncode + StrictDumb, const MIN_LEN: usize, const MAX_LEN: usize> StrictEncode
for Confined<Vec<T>, MIN_LEN, MAX_LEN>
{
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, WriteError> {
let sizing = Sizing::new(MIN_LEN as u64, MAX_LEN as u64);
writer = unsafe {
writer = writer.write_collection::<Vec<T>, MIN_LEN, MAX_LEN>(self)?;
Expand Down Expand Up @@ -526,7 +527,7 @@ impl<T: StrictType, const MIN_LEN: usize, const MAX_LEN: usize> StrictType
impl<T: StrictEncode + StrictDumb, const MIN_LEN: usize, const MAX_LEN: usize> StrictEncode
for Confined<VecDeque<T>, MIN_LEN, MAX_LEN>
{
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, WriteError> {
let sizing = Sizing::new(MIN_LEN as u64, MAX_LEN as u64);
writer = unsafe {
writer = writer.write_collection::<VecDeque<T>, MIN_LEN, MAX_LEN>(self)?;
Expand Down Expand Up @@ -561,7 +562,7 @@ impl<T: StrictType + Ord, const MIN_LEN: usize, const MAX_LEN: usize> StrictType
impl<T: StrictEncode + Ord + StrictDumb, const MIN_LEN: usize, const MAX_LEN: usize> StrictEncode
for Confined<BTreeSet<T>, MIN_LEN, MAX_LEN>
{
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, WriteError> {
unsafe {
writer = writer.write_collection::<BTreeSet<T>, MIN_LEN, MAX_LEN>(self)?;
}
Expand Down Expand Up @@ -602,7 +603,7 @@ impl<
const MAX_LEN: usize,
> StrictEncode for Confined<BTreeMap<K, V>, MIN_LEN, MAX_LEN>
{
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> Result<W, WriteError> {
unsafe {
writer.raw_writer().write_raw_len::<MAX_LEN>(self.len())?;
}
Expand Down
Loading
Loading