Skip to content

Commit

Permalink
Make bee-ternary no_std (#844)
Browse files Browse the repository at this point in the history
* Make bee-ternary no_std

* Bump version

* Remove comments
  • Loading branch information
thibault-martinez authored Nov 19, 2021
1 parent cb131f1 commit 5e3c86e
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion bee-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ keywords = [ "iota", "tangle", "bee", "framework", "crypto" ]
homepage = "https://www.iota.org"

[dependencies]
bee-ternary = { version = "0.5.1", path = "../bee-ternary", default-features = false }
bee-ternary = { version = "0.5.2", path = "../bee-ternary", default-features = false }

byteorder = {version = "1.4.3", default-features = false }
lazy_static = {version = "1.4.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion bee-message/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage = "https://www.iota.org"
[dependencies]
bee-common = { version = "0.5.0", path = "../bee-common/bee-common", default-features = false }
bee-pow = { version = "0.1.0", path = "../bee-pow", default-features = false }
bee-ternary = { version = "0.5.1", path = "../bee-ternary", default-features = false, features = [ "serde1" ] }
bee-ternary = { version = "0.5.2", path = "../bee-ternary", default-features = false, features = [ "serde1" ] }

bech32 = { version = "0.8.1", default-features = false }
bytemuck = { version = "1.7.2", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion bee-pow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ homepage = "https://www.iota.org"

[dependencies]
bee-crypto = { version = "0.2.1-alpha", path = "../bee-crypto", default-features = false }
bee-ternary = { version = "0.5.1", path = "../bee-ternary", default-features = false }
bee-ternary = { version = "0.5.2", path = "../bee-ternary", default-features = false }

iota-crypto = { version = "0.7.0", default-features = false, features = [ "blake2b", "digest" ] }
thiserror = { version = "1.0.30", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion bee-signing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage = "https://www.iota.org"
[dependencies]
bee-common-derive = { version = "0.1.1-alpha", path = "../bee-common/bee-common-derive", default-features = false }
bee-crypto = { version = "0.2.1-alpha", path = "../bee-crypto", default-features = false }
bee-ternary = { version = "0.5.1", path = "../bee-ternary", default-features = false }
bee-ternary = { version = "0.5.2", path = "../bee-ternary", default-features = false }

rand = { version = "0.8.4", default-features = false, features = [ "std", "std_rng" ] }
sha3 = { version = "0.9.1", default-features = false }
Expand Down
6 changes: 6 additions & 0 deletions bee-ternary/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 0.5.2 - 2021-11-19

### Changed

- The crate has been made `no_std`;

## 0.5.1 - 2021-11-16

### Added
Expand Down
3 changes: 2 additions & 1 deletion bee-ternary/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bee-ternary"
version = "0.5.1"
version = "0.5.2"
authors = [ "IOTA Stiftung" ]
edition = "2021"
description = "Ergonomic ternary manipulation utilities"
Expand All @@ -19,3 +19,4 @@ autocfg = { version = "1.0.0", default-features = false }

[features]
serde1 = [ "serde" ]
std = [ ]
4 changes: 4 additions & 0 deletions bee-ternary/src/b1t6.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

extern crate alloc;

use crate::{Btrit, RawEncoding, RawEncodingBuf, TritBuf, Trits, Tryte};

use alloc::vec::Vec;

const TRYTES_PER_BYTE: usize = 2;
const TRITS_PER_TRYTE: usize = 3;
const TRITS_PER_BYTE: usize = TRYTES_PER_BYTE * TRITS_PER_TRYTE;
Expand Down
6 changes: 3 additions & 3 deletions bee-ternary/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{Btrit, RawEncoding, RawEncodingBuf, Trit, TritBuf, Trits, Utrit};

use num_traits::{AsPrimitive, CheckedAdd, CheckedSub, FromPrimitive, Num, Signed};

use std::cmp::PartialOrd;
use core::cmp::PartialOrd;

/// An error that may be produced during numeric conversion.
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -130,7 +130,7 @@ where

let radix = I::from_i8(3).unwrap();

std::iter::from_fn(move || {
core::iter::from_fn(move || {
if x.is_zero() {
None
} else {
Expand All @@ -153,7 +153,7 @@ where
{
let radix = I::from_u8(3).unwrap();

std::iter::from_fn(move || {
core::iter::from_fn(move || {
if x.is_zero() {
None
} else {
Expand Down
25 changes: 15 additions & 10 deletions bee-ternary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
//! buffer will always start on an index of the *original* buffer that is a multiple of 3.
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

use std::slice;
extern crate alloc;

use core::slice;

/// Utility functions for encoding and decoding B1T6 encoding.
pub mod b1t6;
Expand Down Expand Up @@ -90,11 +93,12 @@ pub use crate::{
tryte::{Tryte, TryteBuf},
};

use std::{
use alloc::borrow::ToOwned;
use core::{
any,
borrow::{Borrow, BorrowMut},
cmp::Ordering,
error, fmt, hash,
fmt, hash,
ops::{
Deref, DerefMut, Index, IndexMut, Neg, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
},
Expand All @@ -115,7 +119,8 @@ impl fmt::Display for Error {
}
}

impl error::Error for Error {}
#[cfg(feature = "std")]
impl std::error::Error for Error {}

/// A type that represents a buffer of trits of unknown length.
///
Expand All @@ -135,7 +140,7 @@ where
unsafe { &*(T::empty() as *const _ as *const Self) }
}

/// Interpret an (`std::i8`) slice as a trit slice with the given encoding without first
/// Interpret an (`core::i8`) slice as a trit slice with the given encoding without first
/// checking that the slice is valid in the given encoding. The `num_trits` parameter is used
/// to specify the exact length, in trits, that the slice should be taken to have. Providing a
/// slice that is not valid for this encoding is undefined behaviour.
Expand All @@ -159,7 +164,7 @@ where
&*(T::from_raw_unchecked(raw, num_trits) as *const _ as *const _)
}

/// Interpret a mutable (`std::i8`) slice as a mutable trit slice with the given encoding
/// Interpret a mutable (`core::i8`) slice as a mutable trit slice with the given encoding
/// without first checking that the slice is valid in the given encoding. The `num_trits`
/// parameter is used to specify the exact length, in trits, that the slice should be taken to
/// have. Providing a slice that is not valid for this encoding is undefined behaviour.
Expand All @@ -183,7 +188,7 @@ where
&mut *(T::from_raw_unchecked_mut(raw, num_trits) as *mut _ as *mut _)
}

/// Interpret an (`std::i8`) slice as a trit slice with the given encoding, checking to ensure
/// Interpret an (`core::i8`) slice as a trit slice with the given encoding, checking to ensure
/// that the slice is valid in the given encoding. The `num_trits` parameter is used to specify
/// the exact length, in trits, that the slice should be taken to have.
///
Expand All @@ -199,7 +204,7 @@ where
}
}

/// Interpret a mutable (`std::i8`) slice as a mutable trit slice with the given encoding,
/// Interpret a mutable (`core::i8`) slice as a mutable trit slice with the given encoding,
/// checking to ensure that the slice is valid in the given encoding. The `num_trits` parameter
/// is used to specify the exact length, in trits, that the slice should be taken to have.
///
Expand All @@ -225,7 +230,7 @@ where
self.0.len()
}

/// Interpret this slice as an (`std::i8`) slice.
/// Interpret this slice as an (`core::i8`) slice.
///
/// # Panics
///
Expand All @@ -234,7 +239,7 @@ where
self.0.as_i8_slice()
}

/// Interpret this slice as a mutable (`std::i8`) slice.
/// Interpret this slice as a mutable (`core::i8`) slice.
///
/// # Panics
///
Expand Down
6 changes: 1 addition & 5 deletions bee-ternary/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{Trit, TritBuf};

use std::ops::Range;
use core::ops::Range;

/// A trait to be implemented by alternative trit encoding scheme slices.
#[allow(clippy::len_without_is_empty)]
Expand Down Expand Up @@ -107,10 +107,6 @@ pub trait RawEncodingBuf {
T: RawEncodingBuf,
T::Slice: RawEncoding<Trit = <Self::Slice as RawEncoding>::Trit>,
{
// if TypeId::of::<Self>() == TypeId::of::<T>() {
// unsafe { std::mem::transmute(this) }
// } else {
this.iter().collect()
// }
}
}
2 changes: 1 addition & 1 deletion bee-ternary/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{
Deserialize, Deserializer, Serialize, Serializer,
};

use std::{fmt, marker::PhantomData};
use core::{fmt, marker::PhantomData};

// Serialisation

Expand Down
9 changes: 6 additions & 3 deletions bee-ternary/src/t1b1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

extern crate alloc;

use crate::{trit::ShiftTernary, Btrit, RawEncoding, RawEncodingBuf, Trit};

use std::{hash, marker::PhantomData, ops::Range};
use alloc::vec::Vec;
use core::{hash, marker::PhantomData, ops::Range};

const TRITS_PER_BYTE: usize = 1;

Expand All @@ -16,7 +19,7 @@ pub struct T1B1<T: Trit = Btrit> {

impl<T: Trit> T1B1<T> {
unsafe fn make(ptr: *const T, offset: usize, len: usize) -> *const Self {
std::mem::transmute((ptr.add(offset), len))
core::mem::transmute((ptr.add(offset), len))
}

unsafe fn ptr(&self, index: usize) -> *const T {
Expand Down Expand Up @@ -120,7 +123,7 @@ where
}

// Take ownership of the inner vector and cast it to a `Vec<T::Target>`
let raw_trits = std::mem::ManuallyDrop::new(trit_buf.0);
let raw_trits = core::mem::ManuallyDrop::new(trit_buf.0);

let p = raw_trits.as_ptr();
let len = raw_trits.len();
Expand Down
11 changes: 7 additions & 4 deletions bee-ternary/src/t2b1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

extern crate alloc;

use crate::{Btrit, RawEncoding, RawEncodingBuf, ShiftTernary, Utrit};

use std::ops::Range;
use alloc::vec::Vec;
use core::ops::Range;

const TRITS_PER_BYTE: usize = 2;
// Number required to push a byte between balanced and unbalanced representations
Expand All @@ -16,7 +19,7 @@ pub struct T2B1([()]);
impl T2B1 {
unsafe fn make(ptr: *const i8, offset: usize, len: usize) -> *const Self {
let len = (len << 2) | (offset % TRITS_PER_BYTE);
std::mem::transmute((ptr.add(offset / TRITS_PER_BYTE), len))
core::mem::transmute((ptr.add(offset / TRITS_PER_BYTE), len))
}

unsafe fn ptr(&self, index: usize) -> *const i8 {
Expand Down Expand Up @@ -67,7 +70,7 @@ impl RawEncoding for T2B1 {
fn as_i8_slice(&self) -> &[i8] {
assert!(self.len_offset().1 == 0);
unsafe {
std::slice::from_raw_parts(
core::slice::from_raw_parts(
self as *const _ as *const _,
(self.len() + TRITS_PER_BYTE - 1) / TRITS_PER_BYTE,
)
Expand All @@ -76,7 +79,7 @@ impl RawEncoding for T2B1 {

unsafe fn as_i8_slice_mut(&mut self) -> &mut [i8] {
assert!(self.len_offset().1 == 0);
std::slice::from_raw_parts_mut(
core::slice::from_raw_parts_mut(
self as *mut _ as *mut _,
(self.len() + TRITS_PER_BYTE - 1) / TRITS_PER_BYTE,
)
Expand Down
11 changes: 7 additions & 4 deletions bee-ternary/src/t3b1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

extern crate alloc;

use crate::{Btrit, RawEncoding, RawEncodingBuf, ShiftTernary, Utrit};

use std::ops::Range;
use alloc::vec::Vec;
use core::ops::Range;

const TRITS_PER_BYTE: usize = 3;
// Number required to push a byte between balanced and unbalanced representations
Expand All @@ -16,7 +19,7 @@ pub struct T3B1([()]);
impl T3B1 {
pub(crate) unsafe fn make(ptr: *const i8, offset: usize, len: usize) -> *const Self {
let len = (len << 2) | (offset % TRITS_PER_BYTE);
std::mem::transmute((ptr.add(offset / TRITS_PER_BYTE), len))
core::mem::transmute((ptr.add(offset / TRITS_PER_BYTE), len))
}

unsafe fn ptr(&self, index: usize) -> *const i8 {
Expand Down Expand Up @@ -69,7 +72,7 @@ impl RawEncoding for T3B1 {
fn as_i8_slice(&self) -> &[i8] {
assert!(self.len_offset().1 == 0);
unsafe {
std::slice::from_raw_parts(
core::slice::from_raw_parts(
self as *const _ as *const _,
(self.len() + TRITS_PER_BYTE - 1) / TRITS_PER_BYTE,
)
Expand All @@ -78,7 +81,7 @@ impl RawEncoding for T3B1 {

unsafe fn as_i8_slice_mut(&mut self) -> &mut [i8] {
assert!(self.len_offset().1 == 0);
std::slice::from_raw_parts_mut(
core::slice::from_raw_parts_mut(
self as *mut _ as *mut _,
(self.len() + TRITS_PER_BYTE - 1) / TRITS_PER_BYTE,
)
Expand Down
11 changes: 7 additions & 4 deletions bee-ternary/src/t4b1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

extern crate alloc;

use crate::{Btrit, RawEncoding, RawEncodingBuf, ShiftTernary, Utrit};

use std::ops::Range;
use alloc::vec::Vec;
use core::ops::Range;

const TRITS_PER_BYTE: usize = 4;
// Number required to push a byte between balanced and unbalanced representationsconst TPB: usize = 4;
Expand All @@ -16,7 +19,7 @@ pub struct T4B1([()]);
impl T4B1 {
unsafe fn make(ptr: *const i8, offset: usize, len: usize) -> *const Self {
let len = (len << 2) | (offset % TRITS_PER_BYTE);
std::mem::transmute((ptr.add(offset / TRITS_PER_BYTE), len))
core::mem::transmute((ptr.add(offset / TRITS_PER_BYTE), len))
}

unsafe fn ptr(&self, index: usize) -> *const i8 {
Expand Down Expand Up @@ -67,7 +70,7 @@ impl RawEncoding for T4B1 {
fn as_i8_slice(&self) -> &[i8] {
assert!(self.len_offset().1 == 0);
unsafe {
std::slice::from_raw_parts(
core::slice::from_raw_parts(
self as *const _ as *const _,
(self.len() + TRITS_PER_BYTE - 1) / TRITS_PER_BYTE,
)
Expand All @@ -76,7 +79,7 @@ impl RawEncoding for T4B1 {

unsafe fn as_i8_slice_mut(&mut self) -> &mut [i8] {
assert!(self.len_offset().1 == 0);
std::slice::from_raw_parts_mut(
core::slice::from_raw_parts_mut(
self as *mut _ as *mut _,
(self.len() + TRITS_PER_BYTE - 1) / TRITS_PER_BYTE,
)
Expand Down
Loading

0 comments on commit 5e3c86e

Please sign in to comment.