From c0901ca8f0c30d69cc9a850780411edd44a5817b Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sat, 17 Aug 2024 13:09:35 +0200 Subject: [PATCH] chore: partially fix no-std use of the library --- .github/workflows/build.yml | 23 ++++++++++++++++++++--- Cargo.toml | 2 +- src/isa/exec.rs | 36 ++++++++++++++---------------------- src/library/lib.rs | 7 ++++++- src/library/segs.rs | 6 ++---- 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f8f3320..36d2a0c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,20 +27,37 @@ jobs: steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - - run: cargo check --workspace --no-default-features + - run: cargo check --workspace --no-default-features --features alloc features: runs-on: ubuntu-latest strategy: fail-fast: false matrix: - feature: [ default, stl, std, alloc, ascii-armor, secp256k1, curve25519 ] + feature: + - ascii-armor + - secp256k1 + - curve25519 + - stl steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - name: Feature ${{matrix.feature}} - run: cargo check --workspace --no-default-features --features=${{matrix.feature}} + run: cargo check --workspace --no-default-features --features=std,${{matrix.feature}} - name: Feature ${{matrix.feature}} run: cargo check --workspace --features=${{matrix.feature}} + features-nostd: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + feature: + - secp256k1 + - curve25519 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - name: Feature ${{matrix.feature}} + run: cargo check --workspace --no-default-features --features=alloc,${{matrix.feature}} platforms: runs-on: ${{ matrix.os }} strategy: diff --git a/Cargo.toml b/Cargo.toml index 2b584e9..8d25dd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ homepage = "https://aluvm.org" keywords = ["virtual-machine", "emulator", "functional", "risc", "edge-computing"] categories = ["no-std", "embedded", "compilers", "cryptography", "emulators"] rust-version = "1.75.0" # Due to amplify -edition = "2018" +edition = "2021" license = "Apache-2.0" readme = "README.md" exclude = [".github"] diff --git a/src/isa/exec.rs b/src/isa/exec.rs index f19c66c..e9fe937 100644 --- a/src/isa/exec.rs +++ b/src/isa/exec.rs @@ -27,9 +27,7 @@ use alloc::boxed::Box; use alloc::collections::BTreeSet; #[cfg(all(feature = "alloc", not(feature = "std")))] -use alloc::string::String; -#[cfg(all(feature = "alloc", not(feature = "std")))] -use alloc::vec::Vec; +use alloc::string::{String, ToString}; use core::cmp::Ordering; use core::ops::{BitAnd, BitOr, BitXor, Neg, Rem, Shl, Shr}; @@ -148,7 +146,7 @@ where Extension: InstructionSet Instr::Curve25519(instr) => instr.src_regs(), Instr::ExtensionCodes(instr) => instr.src_regs(), Instr::ReservedInstruction(instr) => instr.src_regs(), - Instr::Nop => bset![], + Instr::Nop => BTreeSet::new(), } } @@ -168,7 +166,7 @@ where Extension: InstructionSet Instr::Curve25519(instr) => instr.dst_regs(), Instr::ExtensionCodes(instr) => instr.dst_regs(), Instr::ReservedInstruction(instr) => instr.dst_regs(), - Instr::Nop => bset![], + Instr::Nop => BTreeSet::new(), } } @@ -220,9 +218,9 @@ impl InstructionSet for ControlFlowOp { #[inline] fn isa_ids() -> IsaSeg { IsaSeg::default() } - fn src_regs(&self) -> BTreeSet { bset![] } + fn src_regs(&self) -> BTreeSet { BTreeSet::new() } - fn dst_regs(&self) -> BTreeSet { bset![] } + fn dst_regs(&self) -> BTreeSet { BTreeSet::new() } #[inline] fn complexity(&self) -> u64 { 2 } @@ -270,11 +268,11 @@ impl InstructionSet for PutOp { #[inline] fn isa_ids() -> IsaSeg { IsaSeg::default() } - fn src_regs(&self) -> BTreeSet { bset![] } + fn src_regs(&self) -> BTreeSet { BTreeSet::new() } fn dst_regs(&self) -> BTreeSet { match self { - PutOp::ClrA(_, _) | PutOp::ClrF(_, _) | PutOp::ClrR(_, _) => bset![], + PutOp::ClrA(_, _) | PutOp::ClrF(_, _) | PutOp::ClrR(_, _) => BTreeSet::new(), PutOp::PutA(reg, reg32, _) => bset![Reg::A(*reg, *reg32)], PutOp::PutF(reg, reg32, _) => bset![Reg::F(*reg, *reg32)], PutOp::PutR(reg, reg32, _) => bset![Reg::R(*reg, *reg32)], @@ -555,10 +553,8 @@ impl InstructionSet for CmpOp { CmpOp::IfZR(reg, idx) | CmpOp::IfNR(reg, idx) => { bset![Reg::R(*reg, *idx)] } - CmpOp::St(_, _, _) => { - bset![] - } - CmpOp::StInv => bset![], + CmpOp::St(_, _, _) => BTreeSet::new(), + CmpOp::StInv => BTreeSet::new(), } } @@ -567,7 +563,7 @@ impl InstructionSet for CmpOp { CmpOp::St(_, reg, idx) => { bset![Reg::A(*reg, (*idx).into())] } - _ => bset![], + _ => BTreeSet::new(), } } @@ -1084,9 +1080,7 @@ impl InstructionSet for BytesOp { fn src_regs(&self) -> BTreeSet { match self { - BytesOp::Put(_reg, _, _) => { - bset![] - } + BytesOp::Put(_reg, _, _) => BTreeSet::new(), BytesOp::Swp(reg1, reg2) | BytesOp::Find(reg1, reg2) => { bset![Reg::S(*reg1), Reg::S(*reg2)] } @@ -1154,9 +1148,7 @@ impl InstructionSet for BytesOp { BytesOp::Cnt(_src, _byte, cnt) => { bset![Reg::new(RegA::A16, *cnt)] } - BytesOp::Eq(_reg1, _reg2) => { - bset![] - } + BytesOp::Eq(_reg1, _reg2) => BTreeSet::new(), BytesOp::Con(_reg1, _reg2, _no, offset, len) => { bset![Reg::A(RegA::A16, *offset), Reg::A(RegA::A16, *len)] } @@ -1673,9 +1665,9 @@ impl InstructionSet for ReservedOp { #[inline] fn isa_ids() -> IsaSeg { IsaSeg::default() } - fn src_regs(&self) -> BTreeSet { bset![] } + fn src_regs(&self) -> BTreeSet { BTreeSet::new() } - fn dst_regs(&self) -> BTreeSet { bset![] } + fn dst_regs(&self) -> BTreeSet { BTreeSet::new() } fn complexity(&self) -> u64 { u64::MAX } diff --git a/src/library/lib.rs b/src/library/lib.rs index c728818..640b99c 100644 --- a/src/library/lib.rs +++ b/src/library/lib.rs @@ -36,13 +36,16 @@ use amplify::confinement::SmallBlob; use amplify::{confinement, ByteArray, Bytes32}; use baid64::{Baid64ParseError, DisplayBaid64, FromBaid64Str}; use sha2::{Digest, Sha256}; +#[cfg(feature = "std")] use strict_encoding::{StrictDeserialize, StrictSerialize}; #[cfg(feature = "ascii-armor")] pub use self::_armor::LibArmorError; use super::{Cursor, Read, WriteError}; use crate::data::ByteStr; -use crate::isa::{Bytecode, BytecodeError, ExecStep, Instr, InstructionSet}; +#[cfg(feature = "std")] +use crate::isa::{Bytecode, Instr}; +use crate::isa::{BytecodeError, ExecStep, InstructionSet}; use crate::library::segs::IsaSeg; use crate::library::{CodeEofError, LibSeg, SegmentError}; use crate::reg::CoreRegs; @@ -135,7 +138,9 @@ pub struct Lib { pub libs: LibSeg, } +#[cfg(feature = "std")] impl StrictSerialize for Lib {} +#[cfg(feature = "std")] impl StrictDeserialize for Lib {} impl Display for Lib { diff --git a/src/library/segs.rs b/src/library/segs.rs index 76dc087..0d2a0d5 100644 --- a/src/library/segs.rs +++ b/src/library/segs.rs @@ -27,15 +27,13 @@ #[cfg(all(feature = "alloc", not(feature = "std")))] use alloc::borrow::ToOwned; -use alloc::collections::BTreeSet; +use alloc::collections::{btree_set, BTreeSet}; #[cfg(all(feature = "alloc", not(feature = "std")))] -use alloc::string::String; +use alloc::string::{String, ToString}; #[cfg(all(feature = "alloc", not(feature = "std")))] use alloc::vec::Vec; use core::fmt::{self, Debug, Display, Formatter}; use core::str::FromStr; -use std::collections::btree_set; -use std::convert::TryFrom; use amplify::confinement; use amplify::confinement::Confined;