Skip to content

Commit

Permalink
Implement TypeInfo for Cow (#84)
Browse files Browse the repository at this point in the history
* Implement TypeInfo for Cow

* Fmt

* Fix ui tests
  • Loading branch information
ascjones committed Apr 13, 2021
1 parent a02b151 commit 0fb9b94
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
18 changes: 18 additions & 0 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
// limitations under the License.

use crate::prelude::{
borrow::{
Cow,
ToOwned,
},
boxed::Box,
collections::BTreeMap,
marker::PhantomData,
Expand Down Expand Up @@ -152,6 +156,20 @@ where
}
}

impl<T> TypeInfo for Cow<'static, T>
where
T: ToOwned + TypeInfo + ?Sized + 'static,
{
type Identity = Self;

fn type_info() -> Type {
Type::builder()
.path(Path::prelude("Cow"))
.type_params(tuple_meta_type!(T))
.composite(Fields::unnamed().field_of::<T>("T"))
}
}

impl<K, V> TypeInfo for BTreeMap<K, V>
where
K: TypeInfo + 'static,
Expand Down
2 changes: 2 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cfg_if! {
if #[cfg(feature = "std")] {
pub use std::{
any,
borrow,
boxed,
cmp,
collections,
Expand All @@ -39,6 +40,7 @@ cfg_if! {
};
} else {
pub use alloc::{
borrow,
boxed,
collections,
format,
Expand Down
20 changes: 13 additions & 7 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@

use crate::{
build::*,
prelude::{
borrow::Cow,
boxed::Box,
string::String,
vec,
},
*,
};
use core::marker::PhantomData;
use scale::Compact;

#[cfg(not(feature = "std"))]
use alloc::{
boxed::Box,
string::String,
vec,
};

fn assert_type<T, E>(expected: E)
where
T: TypeInfo + ?Sized,
Expand Down Expand Up @@ -76,6 +75,13 @@ fn prelude_items() {
)
);
assert_type!(PhantomData<i32>, TypeDefPhantom::new(meta_type::<i32>()));
assert_type!(
Cow<u128>,
Type::builder()
.path(Path::prelude("Cow"))
.type_params(tuple_meta_type!(u128))
.composite(Fields::unnamed().field_of::<u128>("T"))
);
}

#[test]
Expand Down
18 changes: 9 additions & 9 deletions test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ error: Invalid attribute on field, only `#[codec(skip)]`, `#[codec(compact)]` an
13 | Thing(#[codec(index = 3)] u32),
| ^^^^^^^^^

error: expected literal
--> $DIR/fail_with_invalid_codec_attrs.rs:18:21
|
18 | #[codec(index = a)]
| ^

error: proc-macro derive panicked
--> $DIR/fail_with_invalid_codec_attrs.rs:16:18
|
Expand All @@ -19,10 +25,10 @@ error: proc-macro derive panicked
= help: message: scale-info: Bad index in `#[codec(index = …)]`, see `parity-scale-codec` error: Error("expected literal")

error: expected literal
--> $DIR/fail_with_invalid_codec_attrs.rs:18:21
--> $DIR/fail_with_invalid_codec_attrs.rs:24:25
|
18 | #[codec(index = a)]
| ^
24 | #[codec(encode_as = u8, compact)]
| ^^

error: proc-macro derive panicked
--> $DIR/fail_with_invalid_codec_attrs.rs:22:18
Expand All @@ -31,9 +37,3 @@ error: proc-macro derive panicked
| ^^^^^^^^
|
= help: message: scale-info: Bad index in `#[codec(index = …)]`, see `parity-scale-codec` error: Error("expected literal")

error: expected literal
--> $DIR/fail_with_invalid_codec_attrs.rs:24:25
|
24 | #[codec(encode_as = u8, compact)]
| ^^

0 comments on commit 0fb9b94

Please sign in to comment.