Commit 9525f2f5 authored by Andrew Jones's avatar Andrew Jones Committed by Hero Bird

[*] parity-codec -> parity-scale-codec (#164)

* [*] parity-codec -> parity-scale-codec

* [core, model] Decode impls Option -> Result

* [core] typed_cell load expect successful Decode

* [core] Option to Result, remove redundant combinator

* [model] Option to Result

* [*] abbreviate parity-scale-codec

* [core] fix dummy Call Decode impl
parent 53520c15
......@@ -8,7 +8,7 @@ edition = "2018"
ink_core = { git = "https://github.com/paritytech/ink", package = "ink_core", default-features = false }
ink_model = { git = "https://github.com/paritytech/ink", package = "ink_model", default-features = false }
ink_lang = { git = "https://github.com/paritytech/ink", package = "ink_lang", default-features = false }
parity-codec = { version = "4.1", default-features = false, features = ["derive"] }
scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive"] }
[lib]
name = "{{name}}"
......@@ -20,7 +20,7 @@ std = [
"ink_core/std",
"ink_model/std",
"ink_lang/std",
"parity-codec/std",
"scale/std",
]
test-env = [
"std",
......
......@@ -20,7 +20,7 @@ include = ["/Cargo.toml", "src/**/*.rs", "/README.md", "/LICENSE"]
[dependencies]
ink_alloc = { path = "../alloc/", default-features = false }
ink_utils = { path = "../utils/", default-features = false }
parity-codec = { version = "4.1", default-features = false, features = ["derive", "full"] }
scale = { package = "parity-scale-codec", version = "1.0", default-features = false, features = ["derive", "full"] }
[features]
default = ["std"]
......@@ -28,5 +28,5 @@ test-env = ["std"]
std = [
"ink_alloc/std",
"ink_utils/std",
"parity-codec/std",
"scale/std",
]
......@@ -26,7 +26,7 @@ use crate::{
memory::vec::Vec,
storage::Key,
};
use parity_codec::Encode;
use scale::Encode;
/// Stores the given value under the specified key in the contract storage.
///
......
......@@ -25,7 +25,7 @@ use crate::{
storage::Key,
};
use core::marker::PhantomData;
use parity_codec::Decode;
use scale::Decode;
/// Load the contents of the scratch buffer
fn read_scratch_buffer() -> Vec<u8> {
......@@ -95,9 +95,6 @@ macro_rules! impl_getters_for_srml_env {
fn $name() -> $ret_type {
unsafe { sys::$ext_name() };
Decode::decode(&mut &read_scratch_buffer()[..])
.ok_or(concat!(
stringify!($name), " received an incorrectly sized buffer from SRML"
))
.expect(concat!(
stringify!($name), " expects to receive a correctly sized buffer"
))
......
......@@ -24,7 +24,7 @@ use crate::{
impl_empty_flush_for,
storage::Flush,
};
use parity_codec::{
use scale::{
Decode,
Encode,
};
......@@ -44,15 +44,15 @@ pub enum DefaultSrmlTypes {}
/// environment, in order to keep the code size small.
#[cfg_attr(feature = "test-env", derive(Debug, Clone, PartialEq, Eq))]
pub enum Call {}
impl parity_codec::Encode for Call {}
impl scale::Encode for Call {}
/// This implementation is only to satisfy the Decode constraint in the
/// test environment. Since Call cannot be constructed then just return
/// None, but this should never be called.
#[cfg(feature = "test-env")]
impl parity_codec::Decode for Call {
fn decode<I: parity_codec::Input>(_value: &mut I) -> Option<Self> {
None
impl scale::Decode for Call {
fn decode<I: scale::Input>(_value: &mut I) -> Result<Self, scale::Error> {
Err("Call cannot be instantiated".into())
}
}
......
......@@ -27,7 +27,7 @@ use core::cell::{
Cell,
RefCell,
};
use parity_codec::{
use scale::{
Decode,
Encode,
};
......
......@@ -18,7 +18,7 @@ use crate::{
memory::vec::Vec,
storage::Key,
};
use parity_codec::Codec;
use scale::Codec;
#[cfg(not(feature = "test-env"))]
/// The environmental types usable by contracts defined with ink!.
......@@ -34,7 +34,7 @@ pub trait EnvTypes {
/// The type of block number.
type BlockNumber: Codec + Clone + PartialEq + Eq;
/// The type of a call into the runtime
type Call: parity_codec::Encode;
type Call: scale::Encode;
}
#[cfg(feature = "test-env")]
......
......@@ -21,7 +21,7 @@ use crate::storage::{
Key,
};
use parity_codec::{
use scale::{
Decode,
Encode,
};
......
......@@ -27,7 +27,7 @@ use crate::{
},
};
use parity_codec::{
use scale::{
Decode,
Encode,
};
......
......@@ -286,14 +286,14 @@ impl<T> Cache<T> {
}
}
impl<T> parity_codec::Encode for SyncCell<T> {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
impl<T> scale::Encode for SyncCell<T> {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.cell.encode_to(dest)
}
}
impl<T> parity_codec::Decode for SyncCell<T> {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
impl<T> scale::Decode for SyncCell<T> {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
TypedCell::decode(input).map(|typed_cell| {
Self {
cell: typed_cell,
......@@ -305,7 +305,7 @@ impl<T> parity_codec::Decode for SyncCell<T> {
impl<T> Flush for SyncCell<T>
where
T: parity_codec::Encode + Flush,
T: scale::Encode + Flush,
{
fn flush(&mut self) {
if self.cache.is_dirty() {
......@@ -343,7 +343,7 @@ impl<T> SyncCell<T> {
impl<T> SyncCell<T>
where
T: parity_codec::Decode,
T: scale::Decode,
{
/// Returns an immutable reference to the value of the cell.
pub fn get(&self) -> Option<&T> {
......@@ -357,7 +357,7 @@ where
impl<T> SyncCell<T>
where
T: parity_codec::Encode,
T: scale::Encode,
{
/// Sets the value of the cell.
pub fn set(&mut self, val: T) {
......@@ -368,7 +368,7 @@ where
impl<T> SyncCell<T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
/// Returns a mutable reference to the value of the cell.
pub fn get_mut(&mut self) -> Option<&mut T> {
......
......@@ -41,14 +41,14 @@ pub struct TypedCell<T> {
non_clone: NonCloneMarker<T>,
}
impl<T> parity_codec::Encode for TypedCell<T> {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
impl<T> scale::Encode for TypedCell<T> {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.cell.encode_to(dest)
}
}
impl<T> parity_codec::Decode for TypedCell<T> {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
impl<T> scale::Decode for TypedCell<T> {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
RawCell::decode(input).map(|raw_cell| {
Self {
cell: raw_cell,
......@@ -79,19 +79,22 @@ impl<T> TypedCell<T> {
impl<T> TypedCell<T>
where
T: parity_codec::Decode,
T: scale::Decode,
{
/// Loads the value stored in the cell if any.
pub fn load(&self) -> Option<T> {
self.cell
.load()
.and_then(|bytes| T::decode(&mut &bytes[..]))
self.cell.load().map(|bytes| {
T::decode(&mut &bytes[..]).expect(
"[ink_core::TypedCell::load] Error: \
failed upon decoding",
)
})
}
}
impl<T> TypedCell<T>
where
T: parity_codec::Encode,
T: scale::Encode,
{
/// Stores the value into the cell.
pub fn store(&mut self, val: &T) {
......
......@@ -78,14 +78,14 @@ impl RawChunkCell<'_> {
}
}
impl parity_codec::Encode for RawChunk {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
impl scale::Encode for RawChunk {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.key.encode_to(dest)
}
}
impl parity_codec::Decode for RawChunk {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
impl scale::Decode for RawChunk {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
Key::decode(input).map(|key| unsafe { Self::new_unchecked(key) })
}
}
......
......@@ -47,7 +47,7 @@ pub struct SyncChunk<T> {
impl<T> Flush for SyncChunk<T>
where
T: parity_codec::Encode + Flush,
T: scale::Encode + Flush,
{
fn flush(&mut self) {
for (n, dirty_val) in self.cache.iter_dirty() {
......@@ -63,14 +63,14 @@ where
}
}
impl<T> parity_codec::Encode for SyncChunk<T> {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
impl<T> scale::Encode for SyncChunk<T> {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.chunk.encode_to(dest)
}
}
impl<T> parity_codec::Decode for SyncChunk<T> {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
impl<T> scale::Decode for SyncChunk<T> {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
TypedChunk::decode(input).map(|typed_chunk| {
Self {
chunk: typed_chunk,
......@@ -111,7 +111,7 @@ impl<T> SyncChunk<T> {
impl<T> SyncChunk<T>
where
T: parity_codec::Decode,
T: scale::Decode,
{
/// Returns the value of the `n`-th cell if any.
#[must_use]
......@@ -152,7 +152,7 @@ where
impl<T> SyncChunk<T>
where
T: parity_codec::Encode,
T: scale::Encode,
{
/// Sets the value of the `n`-th cell.
pub fn set(&mut self, n: u32, val: T) {
......@@ -162,7 +162,7 @@ where
impl<T> SyncChunk<T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
/// Replaces the value of the `n`-th cell and returns its old value if any.
///
......
......@@ -87,7 +87,7 @@ impl<'a, T> TypedChunkCell<'a, T> {
impl<'a, T> TypedChunkCell<'a, T>
where
T: parity_codec::Encode,
T: scale::Encode,
{
/// Stores the value into the cell.
pub fn store(&mut self, val: &T) {
......@@ -95,14 +95,14 @@ where
}
}
impl<T> parity_codec::Encode for TypedChunk<T> {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
impl<T> scale::Encode for TypedChunk<T> {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.chunk.encode_to(dest)
}
}
impl<T> parity_codec::Decode for TypedChunk<T> {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
impl<T> scale::Decode for TypedChunk<T> {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
RawChunk::decode(input).map(|raw_chunk| {
Self {
chunk: raw_chunk,
......@@ -136,7 +136,7 @@ impl<T> TypedChunk<T> {
impl<T> TypedChunk<T>
where
T: parity_codec::Decode,
T: scale::Decode,
{
/// Loads the value stored in the `n`-th cell if any.
///
......@@ -157,7 +157,7 @@ where
impl<T> TypedChunk<T>
where
T: parity_codec::Encode,
T: scale::Encode,
{
/// Stores the value into the `n`-th cell.
pub fn store(&mut self, n: u32, val: &T) {
......
......@@ -16,7 +16,7 @@
use super::BitPack;
use crate::storage::Flush;
use parity_codec::{
use scale::{
Decode,
Encode,
};
......
......@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with ink!. If not, see <http://www.gnu.org/licenses/>.
use parity_codec::{
use scale::{
Decode,
Encode,
};
......
......@@ -35,18 +35,18 @@ pub struct BitVec {
blocks: SyncChunk<BitBlock>,
}
impl parity_codec::Encode for BitVec {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
impl scale::Encode for BitVec {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.len.encode_to(dest);
self.blocks.encode_to(dest);
}
}
impl parity_codec::Decode for BitVec {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
impl scale::Decode for BitVec {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
let len = storage::Value::decode(input)?;
let blocks = SyncChunk::decode(input)?;
Some(Self { len, blocks })
Ok(Self { len, blocks })
}
}
......
......@@ -69,7 +69,7 @@ pub struct HashMap<K, V> {
/// This can either store the entries key and value
/// or represent an entry that was removed after it
/// has been occupied with key and value.
#[derive(Debug, Clone, PartialEq, Eq, parity_codec::Encode, parity_codec::Decode)]
#[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
pub enum Entry<K, V> {
/// An occupied slot with a key and a value.
Occupied(OccupiedEntry<K, V>),
......@@ -79,8 +79,8 @@ pub enum Entry<K, V> {
impl<K, V> Flush for Entry<K, V>
where
K: parity_codec::Encode + Flush,
V: parity_codec::Encode + Flush,
K: scale::Encode + Flush,
V: scale::Encode + Flush,
{
fn flush(&mut self) {
match self {
......@@ -91,7 +91,7 @@ where
}
/// An occupied entry of a storage map.
#[derive(Debug, Clone, PartialEq, Eq, parity_codec::Encode, parity_codec::Decode)]
#[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
pub struct OccupiedEntry<K, V> {
/// The entry's key.
key: K,
......@@ -101,8 +101,8 @@ pub struct OccupiedEntry<K, V> {
impl<K, V> Flush for OccupiedEntry<K, V>
where
K: parity_codec::Encode + Flush,
V: parity_codec::Encode + Flush,
K: scale::Encode + Flush,
V: scale::Encode + Flush,
{
fn flush(&mut self) {
self.key.flush();
......@@ -112,8 +112,8 @@ where
impl<K, V> Flush for HashMap<K, V>
where
K: parity_codec::Encode + Flush,
V: parity_codec::Encode + Flush,
K: scale::Encode + Flush,
V: scale::Encode + Flush,
{
fn flush(&mut self) {
self.len.flush();
......@@ -121,18 +121,18 @@ where
}
}
impl<K, V> parity_codec::Encode for HashMap<K, V> {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
impl<K, V> scale::Encode for HashMap<K, V> {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.len.encode_to(dest);
self.entries.encode_to(dest);
}
}
impl<K, V> parity_codec::Decode for HashMap<K, V> {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
impl<K, V> scale::Decode for HashMap<K, V> {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
let len = storage::Value::decode(input)?;
let entries = SyncChunk::decode(input)?;
Some(Self { len, entries })
Ok(Self { len, entries })
}
}
......@@ -189,8 +189,8 @@ where
impl<K, V> HashMap<K, V>
where
K: parity_codec::Codec + Hash + Eq,
V: parity_codec::Codec,
K: scale::Codec + Hash + Eq,
V: scale::Codec,
{
/// Inserts a key-value pair into the map.
///
......@@ -247,8 +247,8 @@ impl ProbeSlot {
impl<K, V> HashMap<K, V>
where
K: parity_codec::Codec,
V: parity_codec::Codec,
K: scale::Codec,
V: scale::Codec,
{
/// The maximum amount of probing hops through the hash map.
///
......@@ -470,8 +470,8 @@ where
impl<'a, K, Q: ?Sized, V> core::ops::Index<&'a Q> for HashMap<K, V>
where
K: Eq + Hash + Borrow<Q> + parity_codec::Codec,
V: parity_codec::Codec,
K: Eq + Hash + Borrow<Q> + scale::Codec,
V: scale::Codec,
Q: Eq + Hash,
{
type Output = V;
......@@ -486,8 +486,8 @@ where
impl<'a, K, Q: ?Sized, V> core::ops::IndexMut<&'a Q> for HashMap<K, V>
where
K: Eq + Hash + Borrow<Q> + parity_codec::Codec,
V: parity_codec::Codec,
K: Eq + Hash + Borrow<Q> + scale::Codec,
V: scale::Codec,
Q: Eq + Hash,
{
fn index_mut(&mut self, index: &Q) -> &mut Self::Output {
......
......@@ -26,7 +26,7 @@ use crate::storage::{
Key,
};
use parity_codec::{
use scale::{
Decode,
Encode,
};
......@@ -67,7 +67,7 @@ pub struct Stash<T> {
/// for performance reasons so that they all reside in the same
/// storage entiry. This allows implementations to perform less reads
/// and writes to the underlying contract storage.
#[derive(Debug, parity_codec::Encode, parity_codec::Decode)]
#[derive(Debug, scale::Encode, scale::Decode)]
struct StashHeader {
/// The latest vacant index.
next_vacant: u32,
......@@ -116,7 +116,7 @@ where
impl<'a, T> Iterator for Values<'a, T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
type Item = &'a T;
......@@ -129,11 +129,11 @@ where
}
}
impl<'a, T> ExactSizeIterator for Values<'a, T> where T: parity_codec::Codec {}
impl<'a, T> ExactSizeIterator for Values<'a, T> where T: scale::Codec {}
impl<'a, T> DoubleEndedIterator for Values<'a, T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|(_index, value)| value)
......@@ -170,7 +170,7 @@ impl<'a, T> Iter<'a, T> {
impl<'a, T> Iterator for Iter<'a, T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
type Item = (u32, &'a T);
......@@ -196,11 +196,11 @@ where
}
}
impl<'a, T> ExactSizeIterator for Iter<'a, T> where T: parity_codec::Codec {}
impl<'a, T> ExactSizeIterator for Iter<'a, T> where T: scale::Codec {}
impl<'a, T> DoubleEndedIterator for Iter<'a, T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
fn next_back(&mut self) -> Option<Self::Item> {
debug_assert!(self.begin <= self.end);
......@@ -243,17 +243,17 @@ where
}
impl<T> Encode for Stash<T> {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.header.encode_to(dest);
self.entries.encode_to(dest);
}
}
impl<T> Decode for Stash<T> {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
let header = storage::Value::decode(input)?;
let entries = SyncChunk::decode(input)?;
Some(Self { header, entries })
Ok(Self { header, entries })
}
}
......@@ -342,7 +342,7 @@ impl<T> Stash<T> {
impl<T> Stash<T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
/// Returns the element stored at index `n` if any.
pub fn get(&self, n: u32) -> Option<&T> {
......
......@@ -76,7 +76,7 @@ impl<'a, T> Iter<'a, T> {
impl<T> Flush for Vec<T>
where
T: parity_codec::Encode + Flush,
T: scale::Encode + Flush,
{
fn flush(&mut self) {
self.len.flush();
......@@ -86,7 +86,7 @@ where
impl<'a, T> Iterator for Iter<'a, T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
type Item = &'a T;
......@@ -106,11 +106,11 @@ where
}
}
impl<'a, T> ExactSizeIterator for Iter<'a, T> where T: parity_codec::Codec {}
impl<'a, T> ExactSizeIterator for Iter<'a, T> where T: scale::Codec {}
impl<'a, T> DoubleEndedIterator for Iter<'a, T>
where
T: parity_codec::Codec,
T: scale::Codec,
{
fn next_back(&mut self) -> Option<Self::Item> {
debug_assert!(self.begin <= self.end);
......@@ -123,18 +123,18 @@ where
}
}
impl<T> parity_codec::Encode for Vec<T> {
fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
impl<T> scale::Encode for Vec<T> {
fn encode_to<W: scale::Output>(&self, dest: &mut W) {
self.len.encode_to(dest);
self.cells.encode_to(dest);