1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
// This file is part of Substrate.

// Copyright (C) 2017-2023 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Traits, types and structs to support putting a bounded vector into storage, as a raw value, map
//! or a double map.

use super::WeakBoundedVec;
use crate::{Get, TryCollect};
use alloc::vec::Vec;
use codec::{decode_vec_with_len, Compact, Decode, Encode, EncodeLike, MaxEncodedLen};
use core::{
	marker::PhantomData,
	ops::{Deref, Index, IndexMut, RangeBounds},
	slice::SliceIndex,
};
#[cfg(feature = "serde")]
use serde::{
	de::{Error, SeqAccess, Visitor},
	Deserialize, Deserializer, Serialize,
};

/// A bounded vector.
///
/// It has implementations for efficient append and length decoding, as with a normal `Vec<_>`, once
/// put into storage as a raw value, map or double-map.
///
/// As the name suggests, the length of the queue is always bounded. All internal operations ensure
/// this bound is respected.
#[cfg_attr(feature = "serde", derive(Serialize), serde(transparent))]
#[derive(Encode, scale_info::TypeInfo)]
#[scale_info(skip_type_params(S))]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
pub struct BoundedVec<T, S>(pub(super) Vec<T>, #[cfg_attr(feature = "serde", serde(skip_serializing))] PhantomData<S>);

/// Create an object through truncation.
pub trait TruncateFrom<T> {
	/// Create an object through truncation.
	fn truncate_from(unbound: T) -> Self;
}

#[cfg(feature = "serde")]
impl<'de, T, S: Get<u32>> Deserialize<'de> for BoundedVec<T, S>
where
	T: Deserialize<'de>,
{
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: Deserializer<'de>,
	{
		struct VecVisitor<T, S: Get<u32>>(PhantomData<(T, S)>);

		impl<'de, T, S: Get<u32>> Visitor<'de> for VecVisitor<T, S>
		where
			T: Deserialize<'de>,
		{
			type Value = Vec<T>;

			fn expecting(&self, formatter: &mut alloc::fmt::Formatter) -> alloc::fmt::Result {
				formatter.write_str("a sequence")
			}

			fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
			where
				A: SeqAccess<'de>,
			{
				let size = seq.size_hint().unwrap_or(0);
				let max = match usize::try_from(S::get()) {
					Ok(n) => n,
					Err(_) => return Err(A::Error::custom("can't convert to usize")),
				};
				if size > max {
					Err(A::Error::custom("out of bounds"))
				} else {
					let mut values = Vec::with_capacity(size);

					while let Some(value) = seq.next_element()? {
						if values.len() >= max {
							return Err(A::Error::custom("out of bounds"))
						}
						values.push(value);
					}

					Ok(values)
				}
			}
		}

		let visitor: VecVisitor<T, S> = VecVisitor(PhantomData);
		deserializer
			.deserialize_seq(visitor)
			.map(|v| BoundedVec::<T, S>::try_from(v).map_err(|_| Error::custom("out of bounds")))?
	}
}

/// A bounded slice.
///
/// Similar to a `BoundedVec`, but not owned and cannot be decoded.
#[derive(Encode, scale_info::TypeInfo)]
pub struct BoundedSlice<'a, T, S>(pub(super) &'a [T], PhantomData<S>);

// `BoundedSlice`s encode to something which will always decode into a `BoundedVec`,
// `WeakBoundedVec`, or a `Vec`.
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<BoundedVec<T, S>> for BoundedSlice<'a, T, S> {}
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<WeakBoundedVec<T, S>> for BoundedSlice<'a, T, S> {}
impl<'a, T: Encode + Decode, S: Get<u32>> EncodeLike<Vec<T>> for BoundedSlice<'a, T, S> {}

impl<'a, T, BoundSelf, BoundRhs> PartialEq<BoundedSlice<'a, T, BoundRhs>> for BoundedSlice<'a, T, BoundSelf>
where
	T: PartialEq,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn eq(&self, other: &BoundedSlice<'a, T, BoundRhs>) -> bool {
		self.0 == other.0
	}
}

impl<'a, T, BoundSelf, BoundRhs> PartialEq<BoundedVec<T, BoundRhs>> for BoundedSlice<'a, T, BoundSelf>
where
	T: PartialEq,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn eq(&self, other: &BoundedVec<T, BoundRhs>) -> bool {
		self.0 == other.0
	}
}

impl<'a, T, BoundSelf, BoundRhs> PartialEq<WeakBoundedVec<T, BoundRhs>> for BoundedSlice<'a, T, BoundSelf>
where
	T: PartialEq,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn eq(&self, other: &WeakBoundedVec<T, BoundRhs>) -> bool {
		self.0 == other.0
	}
}

impl<'a, T, S: Get<u32>> Eq for BoundedSlice<'a, T, S> where T: Eq {}

impl<'a, T, BoundSelf, BoundRhs> PartialOrd<BoundedSlice<'a, T, BoundRhs>> for BoundedSlice<'a, T, BoundSelf>
where
	T: PartialOrd,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn partial_cmp(&self, other: &BoundedSlice<'a, T, BoundRhs>) -> Option<core::cmp::Ordering> {
		self.0.partial_cmp(other.0)
	}
}

impl<'a, T, BoundSelf, BoundRhs> PartialOrd<BoundedVec<T, BoundRhs>> for BoundedSlice<'a, T, BoundSelf>
where
	T: PartialOrd,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn partial_cmp(&self, other: &BoundedVec<T, BoundRhs>) -> Option<core::cmp::Ordering> {
		self.0.partial_cmp(&*other.0)
	}
}

impl<'a, T, BoundSelf, BoundRhs> PartialOrd<WeakBoundedVec<T, BoundRhs>> for BoundedSlice<'a, T, BoundSelf>
where
	T: PartialOrd,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn partial_cmp(&self, other: &WeakBoundedVec<T, BoundRhs>) -> Option<core::cmp::Ordering> {
		self.0.partial_cmp(&*other.0)
	}
}

impl<'a, T: Ord, Bound: Get<u32>> Ord for BoundedSlice<'a, T, Bound> {
	fn cmp(&self, other: &Self) -> core::cmp::Ordering {
		self.0.cmp(&other.0)
	}
}

impl<'a, T, S: Get<u32>> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> {
	type Error = &'a [T];
	fn try_from(t: &'a [T]) -> Result<Self, Self::Error> {
		if t.len() <= S::get() as usize {
			Ok(BoundedSlice(t, PhantomData))
		} else {
			Err(t)
		}
	}
}

impl<'a, T, S> From<BoundedSlice<'a, T, S>> for &'a [T] {
	fn from(t: BoundedSlice<'a, T, S>) -> Self {
		t.0
	}
}

impl<'a, T, S: Get<u32>> TruncateFrom<&'a [T]> for BoundedSlice<'a, T, S> {
	fn truncate_from(unbound: &'a [T]) -> Self {
		BoundedSlice::<T, S>::truncate_from(unbound)
	}
}

impl<'a, T, S> Clone for BoundedSlice<'a, T, S> {
	fn clone(&self) -> Self {
		BoundedSlice(self.0, PhantomData)
	}
}

impl<'a, T, S> core::fmt::Debug for BoundedSlice<'a, T, S>
where
	&'a [T]: core::fmt::Debug,
	S: Get<u32>,
{
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		f.debug_tuple("BoundedSlice").field(&self.0).field(&S::get()).finish()
	}
}

// Since a reference `&T` is always `Copy`, so is `BoundedSlice<'a, T, S>`.
impl<'a, T, S> Copy for BoundedSlice<'a, T, S> {}

// will allow for all immutable operations of `[T]` on `BoundedSlice<T>`.
impl<'a, T, S> Deref for BoundedSlice<'a, T, S> {
	type Target = [T];

	fn deref(&self) -> &Self::Target {
		self.0
	}
}

// Custom implementation of `Hash` since deriving it would require all generic bounds to also
// implement it.
#[cfg(feature = "std")]
impl<'a, T: std::hash::Hash, S> std::hash::Hash for BoundedSlice<'a, T, S> {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		self.0.hash(state);
	}
}

impl<'a, T, S> core::iter::IntoIterator for BoundedSlice<'a, T, S> {
	type Item = &'a T;
	type IntoIter = core::slice::Iter<'a, T>;
	fn into_iter(self) -> Self::IntoIter {
		self.0.iter()
	}
}

impl<'a, T, S: Get<u32>> BoundedSlice<'a, T, S> {
	/// Create an instance from the first elements of the given slice (or all of it if it is smaller
	/// than the length bound).
	pub fn truncate_from(s: &'a [T]) -> Self {
		Self(&s[0..(s.len().min(S::get() as usize))], PhantomData)
	}
}

impl<T: Decode, S: Get<u32>> Decode for BoundedVec<T, S> {
	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
		// Same as the underlying implementation for `Decode` on `Vec`, except we fail early if the
		// len is too big.
		let len: u32 = <Compact<u32>>::decode(input)?.into();
		if len > S::get() {
			return Err("BoundedVec exceeds its limit".into())
		}
		let inner = decode_vec_with_len(input, len as usize)?;
		Ok(Self(inner, PhantomData))
	}

	fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
		Vec::<T>::skip(input)
	}
}

// `BoundedVec`s encode to something which will always decode as a `Vec`.
impl<T: Encode + Decode, S: Get<u32>> EncodeLike<Vec<T>> for BoundedVec<T, S> {}

impl<T, S> BoundedVec<T, S> {
	/// Create `Self` with no items.
	pub fn new() -> Self {
		Self(Vec::new(), Default::default())
	}

	/// Create `Self` from `t` without any checks.
	fn unchecked_from(t: Vec<T>) -> Self {
		Self(t, Default::default())
	}

	/// Exactly the same semantics as `Vec::clear`.
	pub fn clear(&mut self) {
		self.0.clear()
	}

	/// Consume self, and return the inner `Vec`. Henceforth, the `Vec<_>` can be altered in an
	/// arbitrary way. At some point, if the reverse conversion is required, `TryFrom<Vec<_>>` can
	/// be used.
	///
	/// This is useful for cases if you need access to an internal API of the inner `Vec<_>` which
	/// is not provided by the wrapper `BoundedVec`.
	pub fn into_inner(self) -> Vec<T> {
		self.0
	}

	/// Exactly the same semantics as [`slice::sort_by`].
	///
	/// This is safe since sorting cannot change the number of elements in the vector.
	pub fn sort_by<F>(&mut self, compare: F)
	where
		F: FnMut(&T, &T) -> core::cmp::Ordering,
	{
		self.0.sort_by(compare)
	}

	/// Exactly the same semantics as [`slice::sort_by_key`].
	///
	/// This is safe since sorting cannot change the number of elements in the vector.
	pub fn sort_by_key<K, F>(&mut self, f: F)
	where
		F: FnMut(&T) -> K,
		K: core::cmp::Ord,
	{
		self.0.sort_by_key(f)
	}

	/// Exactly the same semantics as [`slice::sort`].
	///
	/// This is safe since sorting cannot change the number of elements in the vector.
	pub fn sort(&mut self)
	where
		T: core::cmp::Ord,
	{
		self.0.sort()
	}

	/// Exactly the same semantics as `Vec::remove`.
	///
	/// # Panics
	///
	/// Panics if `index` is out of bounds.
	pub fn remove(&mut self, index: usize) -> T {
		self.0.remove(index)
	}

	/// Exactly the same semantics as `slice::swap_remove`.
	///
	/// # Panics
	///
	/// Panics if `index` is out of bounds.
	pub fn swap_remove(&mut self, index: usize) -> T {
		self.0.swap_remove(index)
	}

	/// Exactly the same semantics as `Vec::retain`.
	pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F) {
		self.0.retain(f)
	}

	/// Exactly the same semantics as `slice::get_mut`.
	pub fn get_mut<I: SliceIndex<[T]>>(&mut self, index: I) -> Option<&mut <I as SliceIndex<[T]>>::Output> {
		self.0.get_mut(index)
	}

	/// Exactly the same semantics as `Vec::truncate`.
	///
	/// This is safe because `truncate` can never increase the length of the internal vector.
	pub fn truncate(&mut self, s: usize) {
		self.0.truncate(s);
	}

	/// Exactly the same semantics as `Vec::pop`.
	///
	/// This is safe since popping can only shrink the inner vector.
	pub fn pop(&mut self) -> Option<T> {
		self.0.pop()
	}

	/// Exactly the same semantics as [`slice::iter_mut`].
	pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, T> {
		self.0.iter_mut()
	}

	/// Exactly the same semantics as [`slice::last_mut`].
	pub fn last_mut(&mut self) -> Option<&mut T> {
		self.0.last_mut()
	}

	/// Exact same semantics as [`Vec::drain`].
	pub fn drain<R>(&mut self, range: R) -> alloc::vec::Drain<'_, T>
	where
		R: RangeBounds<usize>,
	{
		self.0.drain(range)
	}
}

impl<T, S: Get<u32>> From<BoundedVec<T, S>> for Vec<T> {
	fn from(x: BoundedVec<T, S>) -> Vec<T> {
		x.0
	}
}

impl<T, S: Get<u32>> BoundedVec<T, S> {
	/// Pre-allocate `capacity` items in self.
	///
	/// If `capacity` is greater than [`Self::bound`], then the minimum of the two is used.
	pub fn with_bounded_capacity(capacity: usize) -> Self {
		let capacity = capacity.min(Self::bound());
		Self(Vec::with_capacity(capacity), Default::default())
	}

	/// Allocate self with the maximum possible capacity.
	pub fn with_max_capacity() -> Self {
		Self::with_bounded_capacity(Self::bound())
	}

	/// Consume and truncate the vector `v` in order to create a new instance of `Self` from it.
	pub fn truncate_from(mut v: Vec<T>) -> Self {
		v.truncate(Self::bound());
		Self::unchecked_from(v)
	}

	/// Get the bound of the type in `usize`.
	pub fn bound() -> usize {
		S::get() as usize
	}

	/// Returns true of this collection is full.
	pub fn is_full(&self) -> bool {
		self.len() >= Self::bound()
	}

	/// Forces the insertion of `element` into `self` retaining all items with index at least
	/// `index`.
	///
	/// If `index == 0` and `self.len() == Self::bound()`, then this is a no-op.
	///
	/// If `Self::bound() < index` or `self.len() < index`, then this is also a no-op.
	///
	/// Returns `Ok(maybe_removed)` if the item was inserted, where `maybe_removed` is
	/// `Some(removed)` if an item was removed to make room for the new one. Returns `Err(element)`
	/// if `element` cannot be inserted.
	pub fn force_insert_keep_right(&mut self, index: usize, mut element: T) -> Result<Option<T>, T> {
		// Check against panics.
		if Self::bound() < index || self.len() < index {
			Err(element)
		} else if self.len() < Self::bound() {
			// Cannot panic since self.len() >= index;
			self.0.insert(index, element);
			Ok(None)
		} else {
			if index == 0 {
				return Err(element)
			}
			core::mem::swap(&mut self[0], &mut element);
			// `[0..index] cannot panic since self.len() >= index.
			// `rotate_left(1)` cannot panic because there is at least 1 element.
			self[0..index].rotate_left(1);
			Ok(Some(element))
		}
	}

	/// Forces the insertion of `element` into `self` retaining all items with index at most
	/// `index`.
	///
	/// If `index == Self::bound()` and `self.len() == Self::bound()`, then this is a no-op.
	///
	/// If `Self::bound() < index` or `self.len() < index`, then this is also a no-op.
	///
	/// Returns `Ok(maybe_removed)` if the item was inserted, where `maybe_removed` is
	/// `Some(removed)` if an item was removed to make room for the new one. Returns `Err(element)`
	/// if `element` cannot be inserted.
	pub fn force_insert_keep_left(&mut self, index: usize, element: T) -> Result<Option<T>, T> {
		// Check against panics.
		if Self::bound() < index || self.len() < index || Self::bound() == 0 {
			return Err(element)
		}
		// Noop condition.
		if Self::bound() == index && self.len() <= Self::bound() {
			return Err(element)
		}
		let maybe_removed = if self.is_full() {
			// defensive-only: since we are at capacity, this is a noop.
			self.0.truncate(Self::bound());
			// if we truncate anything, it will be the last one.
			self.0.pop()
		} else {
			None
		};

		// Cannot panic since `self.len() >= index`;
		self.0.insert(index, element);
		Ok(maybe_removed)
	}

	/// Move the position of an item from one location to another in the slice.
	///
	/// Except for the item being moved, the order of the slice remains the same.
	///
	/// - `index` is the location of the item to be moved.
	/// - `insert_position` is the index of the item in the slice which should *immediately follow*
	///   the item which is being moved.
	///
	/// Returns `true` of the operation was successful, otherwise `false` if a noop.
	pub fn slide(&mut self, index: usize, insert_position: usize) -> bool {
		// Check against panics.
		if self.len() <= index || self.len() < insert_position || index == usize::MAX {
			return false
		}
		// Noop conditions.
		if index == insert_position || index + 1 == insert_position {
			return false
		}
		if insert_position < index && index < self.len() {
			// --- --- --- === === === === @@@ --- --- ---
			//            ^-- N            ^O^
			// ...
			//               /-----<<<-----\
			// --- --- --- === === === === @@@ --- --- ---
			//               >>> >>> >>> >>>
			// ...
			// --- --- --- @@@ === === === === --- --- ---
			//             ^N^
			self[insert_position..index + 1].rotate_right(1);
			return true
		} else if insert_position > 0 && index + 1 < insert_position {
			// Note that the apparent asymmetry of these two branches is due to the
			// fact that the "new" position is the position to be inserted *before*.
			// --- --- --- @@@ === === === === --- --- ---
			//             ^O^                ^-- N
			// ...
			//               /----->>>-----\
			// --- --- --- @@@ === === === === --- --- ---
			//               <<< <<< <<< <<<
			// ...
			// --- --- --- === === === === @@@ --- --- ---
			//                             ^N^
			self[index..insert_position].rotate_left(1);
			return true
		}

		debug_assert!(false, "all noop conditions should have been covered above");
		false
	}

	/// Forces the insertion of `s` into `self` truncating first if necessary.
	///
	/// Infallible, but if the bound is zero, then it's a no-op.
	pub fn force_push(&mut self, element: T) {
		if Self::bound() > 0 {
			self.0.truncate(Self::bound() as usize - 1);
			self.0.push(element);
		}
	}

	/// Same as `Vec::resize`, but if `size` is more than [`Self::bound`], then [`Self::bound`] is
	/// used.
	pub fn bounded_resize(&mut self, size: usize, value: T)
	where
		T: Clone,
	{
		let size = size.min(Self::bound());
		self.0.resize(size, value);
	}

	/// Exactly the same semantics as [`Vec::extend`], but returns an error and does nothing if the
	/// length of the outcome is larger than the bound.
	pub fn try_extend(&mut self, with: impl IntoIterator<Item = T> + ExactSizeIterator) -> Result<(), ()> {
		if with.len().saturating_add(self.len()) <= Self::bound() {
			self.0.extend(with);
			Ok(())
		} else {
			Err(())
		}
	}

	/// Exactly the same semantics as [`Vec::append`], but returns an error and does nothing if the
	/// length of the outcome is larger than the bound.
	pub fn try_append(&mut self, other: &mut Vec<T>) -> Result<(), ()> {
		if other.len().saturating_add(self.len()) <= Self::bound() {
			self.0.append(other);
			Ok(())
		} else {
			Err(())
		}
	}

	/// Consumes self and mutates self via the given `mutate` function.
	///
	/// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is
	/// returned.
	///
	/// This is essentially a *consuming* shorthand [`Self::into_inner`] -> `...` ->
	/// [`Self::try_from`].
	pub fn try_mutate(mut self, mut mutate: impl FnMut(&mut Vec<T>)) -> Option<Self> {
		mutate(&mut self.0);
		(self.0.len() <= Self::bound()).then(move || self)
	}

	/// Exactly the same semantics as [`Vec::insert`], but returns an `Err` (and is a noop) if the
	/// new length of the vector exceeds `S`.
	///
	/// # Panics
	///
	/// Panics if `index > len`.
	pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), T> {
		if self.len() < Self::bound() {
			self.0.insert(index, element);
			Ok(())
		} else {
			Err(element)
		}
	}

	/// Exactly the same semantics as [`Vec::push`], but returns an `Err` (and is a noop) if the
	/// new length of the vector exceeds `S`.
	///
	/// # Panics
	///
	/// Panics if the new capacity exceeds isize::MAX bytes.
	pub fn try_push(&mut self, element: T) -> Result<(), T> {
		if self.len() < Self::bound() {
			self.0.push(element);
			Ok(())
		} else {
			Err(element)
		}
	}

	/// Exactly the same semantics as [`Vec::rotate_left`], but returns an `Err` (and is a noop) if `mid` is larger then the current length.
	pub fn try_rotate_left(&mut self, mid: usize) -> Result<(), ()> {
		if mid > self.len() {
			return Err(())
		}

		self.0.rotate_left(mid);
		Ok(())
	}

	/// Exactly the same semantics as [`Vec::rotate_right`], but returns an `Err` (and is a noop) if `mid` is larger then the current length.
	pub fn try_rotate_right(&mut self, mid: usize) -> Result<(), ()> {
		if mid > self.len() {
			return Err(())
		}

		self.0.rotate_right(mid);
		Ok(())
	}
}

impl<T, S> BoundedVec<T, S> {
	/// Return a [`BoundedSlice`] with the content and bound of [`Self`].
	pub fn as_bounded_slice(&self) -> BoundedSlice<T, S> {
		BoundedSlice(&self.0[..], PhantomData::default())
	}
}

impl<T, S> Default for BoundedVec<T, S> {
	fn default() -> Self {
		// the bound cannot be below 0, which is satisfied by an empty vector
		Self::unchecked_from(Vec::default())
	}
}

impl<T, S> core::fmt::Debug for BoundedVec<T, S>
where
	Vec<T>: core::fmt::Debug,
	S: Get<u32>,
{
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		f.debug_tuple("BoundedVec").field(&self.0).field(&Self::bound()).finish()
	}
}

impl<T, S> Clone for BoundedVec<T, S>
where
	T: Clone,
{
	fn clone(&self) -> Self {
		// bound is retained
		Self::unchecked_from(self.0.clone())
	}
}

impl<T, S: Get<u32>> TryFrom<Vec<T>> for BoundedVec<T, S> {
	type Error = Vec<T>;
	fn try_from(t: Vec<T>) -> Result<Self, Self::Error> {
		if t.len() <= Self::bound() {
			// explicit check just above
			Ok(Self::unchecked_from(t))
		} else {
			Err(t)
		}
	}
}

impl<T, S: Get<u32>> TruncateFrom<Vec<T>> for BoundedVec<T, S> {
	fn truncate_from(unbound: Vec<T>) -> Self {
		BoundedVec::<T, S>::truncate_from(unbound)
	}
}

// Custom implementation of `Hash` since deriving it would require all generic bounds to also
// implement it.
#[cfg(feature = "std")]
impl<T: std::hash::Hash, S> std::hash::Hash for BoundedVec<T, S> {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		self.0.hash(state);
	}
}

// It is okay to give a non-mutable reference of the inner vec to anyone.
impl<T, S> AsRef<Vec<T>> for BoundedVec<T, S> {
	fn as_ref(&self) -> &Vec<T> {
		&self.0
	}
}

impl<T, S> AsRef<[T]> for BoundedVec<T, S> {
	fn as_ref(&self) -> &[T] {
		&self.0
	}
}

impl<T, S> AsMut<[T]> for BoundedVec<T, S> {
	fn as_mut(&mut self) -> &mut [T] {
		&mut self.0
	}
}

// will allow for all immutable operations of `Vec<T>` on `BoundedVec<T>`.
impl<T, S> Deref for BoundedVec<T, S> {
	type Target = Vec<T>;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

// Allows for indexing similar to a normal `Vec`. Can panic if out of bound.
impl<T, S, I> Index<I> for BoundedVec<T, S>
where
	I: SliceIndex<[T]>,
{
	type Output = I::Output;

	#[inline]
	fn index(&self, index: I) -> &Self::Output {
		self.0.index(index)
	}
}

impl<T, S, I> IndexMut<I> for BoundedVec<T, S>
where
	I: SliceIndex<[T]>,
{
	#[inline]
	fn index_mut(&mut self, index: I) -> &mut Self::Output {
		self.0.index_mut(index)
	}
}

impl<T, S> core::iter::IntoIterator for BoundedVec<T, S> {
	type Item = T;
	type IntoIter = alloc::vec::IntoIter<T>;
	fn into_iter(self) -> Self::IntoIter {
		self.0.into_iter()
	}
}

impl<'a, T, S> core::iter::IntoIterator for &'a BoundedVec<T, S> {
	type Item = &'a T;
	type IntoIter = core::slice::Iter<'a, T>;
	fn into_iter(self) -> Self::IntoIter {
		self.0.iter()
	}
}

impl<'a, T, S> core::iter::IntoIterator for &'a mut BoundedVec<T, S> {
	type Item = &'a mut T;
	type IntoIter = core::slice::IterMut<'a, T>;
	fn into_iter(self) -> Self::IntoIter {
		self.0.iter_mut()
	}
}

impl<T, S> codec::DecodeLength for BoundedVec<T, S> {
	fn len(self_encoded: &[u8]) -> Result<usize, codec::Error> {
		// `BoundedVec<T, _>` stored just a `Vec<T>`, thus the length is at the beginning in
		// `Compact` form, and same implementation as `Vec<T>` can be used.
		<Vec<T> as codec::DecodeLength>::len(self_encoded)
	}
}

impl<T, BoundSelf, BoundRhs> PartialEq<BoundedVec<T, BoundRhs>> for BoundedVec<T, BoundSelf>
where
	T: PartialEq,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn eq(&self, rhs: &BoundedVec<T, BoundRhs>) -> bool {
		self.0 == rhs.0
	}
}

impl<T, BoundSelf, BoundRhs> PartialEq<WeakBoundedVec<T, BoundRhs>> for BoundedVec<T, BoundSelf>
where
	T: PartialEq,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn eq(&self, rhs: &WeakBoundedVec<T, BoundRhs>) -> bool {
		self.0 == rhs.0
	}
}

impl<'a, T, BoundSelf, BoundRhs> PartialEq<BoundedSlice<'a, T, BoundRhs>> for BoundedVec<T, BoundSelf>
where
	T: PartialEq,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn eq(&self, rhs: &BoundedSlice<'a, T, BoundRhs>) -> bool {
		self.0 == rhs.0
	}
}

impl<'a, T: PartialEq, S: Get<u32>> PartialEq<&'a [T]> for BoundedSlice<'a, T, S> {
	fn eq(&self, other: &&'a [T]) -> bool {
		&self.0 == other
	}
}

impl<T: PartialEq, S: Get<u32>> PartialEq<Vec<T>> for BoundedVec<T, S> {
	fn eq(&self, other: &Vec<T>) -> bool {
		&self.0 == other
	}
}

impl<T, S: Get<u32>> Eq for BoundedVec<T, S> where T: Eq {}

impl<T, BoundSelf, BoundRhs> PartialOrd<BoundedVec<T, BoundRhs>> for BoundedVec<T, BoundSelf>
where
	T: PartialOrd,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn partial_cmp(&self, other: &BoundedVec<T, BoundRhs>) -> Option<core::cmp::Ordering> {
		self.0.partial_cmp(&other.0)
	}
}

impl<T, BoundSelf, BoundRhs> PartialOrd<WeakBoundedVec<T, BoundRhs>> for BoundedVec<T, BoundSelf>
where
	T: PartialOrd,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn partial_cmp(&self, other: &WeakBoundedVec<T, BoundRhs>) -> Option<core::cmp::Ordering> {
		self.0.partial_cmp(&other.0)
	}
}

impl<'a, T, BoundSelf, BoundRhs> PartialOrd<BoundedSlice<'a, T, BoundRhs>> for BoundedVec<T, BoundSelf>
where
	T: PartialOrd,
	BoundSelf: Get<u32>,
	BoundRhs: Get<u32>,
{
	fn partial_cmp(&self, other: &BoundedSlice<'a, T, BoundRhs>) -> Option<core::cmp::Ordering> {
		(&*self.0).partial_cmp(other.0)
	}
}

impl<T: Ord, Bound: Get<u32>> Ord for BoundedVec<T, Bound> {
	fn cmp(&self, other: &Self) -> core::cmp::Ordering {
		self.0.cmp(&other.0)
	}
}

impl<T, S> MaxEncodedLen for BoundedVec<T, S>
where
	T: MaxEncodedLen,
	S: Get<u32>,
	BoundedVec<T, S>: Encode,
{
	fn max_encoded_len() -> usize {
		// BoundedVec<T, S> encodes like Vec<T> which encodes like [T], which is a compact u32
		// plus each item in the slice:
		// See: https://docs.substrate.io/reference/scale-codec/
		codec::Compact(S::get())
			.encoded_size()
			.saturating_add(Self::bound().saturating_mul(T::max_encoded_len()))
	}
}

impl<I, T, Bound> TryCollect<BoundedVec<T, Bound>> for I
where
	I: ExactSizeIterator + Iterator<Item = T>,
	Bound: Get<u32>,
{
	type Error = &'static str;

	fn try_collect(self) -> Result<BoundedVec<T, Bound>, Self::Error> {
		if self.len() > Bound::get() as usize {
			Err("iterator length too big")
		} else {
			Ok(BoundedVec::<T, Bound>::unchecked_from(self.collect::<Vec<T>>()))
		}
	}
}

#[cfg(all(test, feature = "std"))]
mod test {
	use super::*;
	use crate::{bounded_vec, ConstU32, ConstU8};
	use codec::CompactLen;

	#[test]
	fn encoding_same_as_unbounded_vec() {
		let b: BoundedVec<u32, ConstU32<6>> = bounded_vec![0, 1, 2, 3, 4, 5];
		let v: Vec<u32> = vec![0, 1, 2, 3, 4, 5];

		assert_eq!(b.encode(), v.encode());
	}

	#[test]
	fn slice_truncate_from_works() {
		let bounded = BoundedSlice::<u32, ConstU32<4>>::truncate_from(&[1, 2, 3, 4, 5]);
		assert_eq!(bounded.deref(), &[1, 2, 3, 4]);
		let bounded = BoundedSlice::<u32, ConstU32<4>>::truncate_from(&[1, 2, 3, 4]);
		assert_eq!(bounded.deref(), &[1, 2, 3, 4]);
		let bounded = BoundedSlice::<u32, ConstU32<4>>::truncate_from(&[1, 2, 3]);
		assert_eq!(bounded.deref(), &[1, 2, 3]);
	}

	#[test]
	fn slide_works() {
		let mut b: BoundedVec<u32, ConstU32<6>> = bounded_vec![0, 1, 2, 3, 4, 5];
		assert!(b.slide(1, 5));
		assert_eq!(*b, vec![0, 2, 3, 4, 1, 5]);
		assert!(b.slide(4, 0));
		assert_eq!(*b, vec![1, 0, 2, 3, 4, 5]);
		assert!(b.slide(0, 2));
		assert_eq!(*b, vec![0, 1, 2, 3, 4, 5]);
		assert!(b.slide(1, 6));
		assert_eq!(*b, vec![0, 2, 3, 4, 5, 1]);
		assert!(b.slide(0, 6));
		assert_eq!(*b, vec![2, 3, 4, 5, 1, 0]);
		assert!(b.slide(5, 0));
		assert_eq!(*b, vec![0, 2, 3, 4, 5, 1]);
		assert!(!b.slide(6, 0));
		assert!(!b.slide(7, 0));
		assert_eq!(*b, vec![0, 2, 3, 4, 5, 1]);

		let mut c: BoundedVec<u32, ConstU32<6>> = bounded_vec![0, 1, 2];
		assert!(!c.slide(1, 5));
		assert_eq!(*c, vec![0, 1, 2]);
		assert!(!c.slide(4, 0));
		assert_eq!(*c, vec![0, 1, 2]);
		assert!(!c.slide(3, 0));
		assert_eq!(*c, vec![0, 1, 2]);
		assert!(c.slide(2, 0));
		assert_eq!(*c, vec![2, 0, 1]);
	}

	#[test]
	fn slide_noops_work() {
		let mut b: BoundedVec<u32, ConstU32<6>> = bounded_vec![0, 1, 2, 3, 4, 5];
		assert!(!b.slide(3, 3));
		assert_eq!(*b, vec![0, 1, 2, 3, 4, 5]);
		assert!(!b.slide(3, 4));
		assert_eq!(*b, vec![0, 1, 2, 3, 4, 5]);
	}

	#[test]
	fn force_insert_keep_left_works() {
		let mut b: BoundedVec<u32, ConstU32<4>> = bounded_vec![];
		assert_eq!(b.force_insert_keep_left(1, 10), Err(10));
		assert!(b.is_empty());

		assert_eq!(b.force_insert_keep_left(0, 30), Ok(None));
		assert_eq!(b.force_insert_keep_left(0, 10), Ok(None));
		assert_eq!(b.force_insert_keep_left(1, 20), Ok(None));
		assert_eq!(b.force_insert_keep_left(3, 40), Ok(None));
		assert_eq!(*b, vec![10, 20, 30, 40]);
		// at capacity.
		assert_eq!(b.force_insert_keep_left(4, 41), Err(41));
		assert_eq!(*b, vec![10, 20, 30, 40]);
		assert_eq!(b.force_insert_keep_left(3, 31), Ok(Some(40)));
		assert_eq!(*b, vec![10, 20, 30, 31]);
		assert_eq!(b.force_insert_keep_left(1, 11), Ok(Some(31)));
		assert_eq!(*b, vec![10, 11, 20, 30]);
		assert_eq!(b.force_insert_keep_left(0, 1), Ok(Some(30)));
		assert_eq!(*b, vec![1, 10, 11, 20]);

		let mut z: BoundedVec<u32, ConstU32<0>> = bounded_vec![];
		assert!(z.is_empty());
		assert_eq!(z.force_insert_keep_left(0, 10), Err(10));
		assert!(z.is_empty());
	}

	#[test]
	fn force_insert_keep_right_works() {
		let mut b: BoundedVec<u32, ConstU32<4>> = bounded_vec![];
		assert_eq!(b.force_insert_keep_right(1, 10), Err(10));
		assert!(b.is_empty());

		assert_eq!(b.force_insert_keep_right(0, 30), Ok(None));
		assert_eq!(b.force_insert_keep_right(0, 10), Ok(None));
		assert_eq!(b.force_insert_keep_right(1, 20), Ok(None));
		assert_eq!(b.force_insert_keep_right(3, 40), Ok(None));
		assert_eq!(*b, vec![10, 20, 30, 40]);

		// at capacity.
		assert_eq!(b.force_insert_keep_right(0, 0), Err(0));
		assert_eq!(*b, vec![10, 20, 30, 40]);
		assert_eq!(b.force_insert_keep_right(1, 11), Ok(Some(10)));
		assert_eq!(*b, vec![11, 20, 30, 40]);
		assert_eq!(b.force_insert_keep_right(3, 31), Ok(Some(11)));
		assert_eq!(*b, vec![20, 30, 31, 40]);
		assert_eq!(b.force_insert_keep_right(4, 41), Ok(Some(20)));
		assert_eq!(*b, vec![30, 31, 40, 41]);

		assert_eq!(b.force_insert_keep_right(5, 69), Err(69));
		assert_eq!(*b, vec![30, 31, 40, 41]);

		let mut z: BoundedVec<u32, ConstU32<0>> = bounded_vec![];
		assert!(z.is_empty());
		assert_eq!(z.force_insert_keep_right(0, 10), Err(10));
		assert!(z.is_empty());
	}

	#[test]
	fn bound_returns_correct_value() {
		assert_eq!(BoundedVec::<u32, ConstU32<7>>::bound(), 7);
	}

	#[test]
	fn try_insert_works() {
		let mut bounded: BoundedVec<u32, ConstU32<4>> = bounded_vec![1, 2, 3];
		bounded.try_insert(1, 0).unwrap();
		assert_eq!(*bounded, vec![1, 0, 2, 3]);

		assert!(bounded.try_insert(0, 9).is_err());
		assert_eq!(*bounded, vec![1, 0, 2, 3]);
	}

	#[test]
	fn constructor_macro_works() {
		// With values. Use some brackets to make sure the macro doesn't expand.
		let bv: BoundedVec<(u32, u32), ConstU32<3>> = bounded_vec![(1, 2), (1, 2), (1, 2)];
		assert_eq!(bv, vec![(1, 2), (1, 2), (1, 2)]);

		// With repetition.
		let bv: BoundedVec<(u32, u32), ConstU32<3>> = bounded_vec![(1, 2); 3];
		assert_eq!(bv, vec![(1, 2), (1, 2), (1, 2)]);
	}

	#[test]
	#[should_panic(expected = "insertion index (is 9) should be <= len (is 3)")]
	fn try_inert_panics_if_oob() {
		let mut bounded: BoundedVec<u32, ConstU32<4>> = bounded_vec![1, 2, 3];
		bounded.try_insert(9, 0).unwrap();
	}

	#[test]
	fn try_push_works() {
		let mut bounded: BoundedVec<u32, ConstU32<4>> = bounded_vec![1, 2, 3];
		bounded.try_push(0).unwrap();
		assert_eq!(*bounded, vec![1, 2, 3, 0]);

		assert!(bounded.try_push(9).is_err());
	}

	#[test]
	fn deref_vec_coercion_works() {
		let bounded: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3];
		// these methods come from deref-ed vec.
		assert_eq!(bounded.len(), 3);
		assert!(bounded.iter().next().is_some());
		assert!(!bounded.is_empty());
	}

	#[test]
	fn deref_slice_coercion_works() {
		let bounded = BoundedSlice::<u32, ConstU32<7>>::try_from(&[1, 2, 3][..]).unwrap();
		// these methods come from deref-ed slice.
		assert_eq!(bounded.len(), 3);
		assert!(bounded.iter().next().is_some());
		assert!(!bounded.is_empty());
	}

	#[test]
	fn try_mutate_works() {
		let bounded: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3, 4, 5, 6];
		let bounded = bounded.try_mutate(|v| v.push(7)).unwrap();
		assert_eq!(bounded.len(), 7);
		assert!(bounded.try_mutate(|v| v.push(8)).is_none());
	}

	#[test]
	fn slice_indexing_works() {
		let bounded: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3, 4, 5, 6];
		assert_eq!(&bounded[0..=2], &[1, 2, 3]);
	}

	#[test]
	fn vec_eq_works() {
		let bounded: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3, 4, 5, 6];
		assert_eq!(bounded, vec![1, 2, 3, 4, 5, 6]);
	}

	#[test]
	fn too_big_vec_fail_to_decode() {
		let v: Vec<u32> = vec![1, 2, 3, 4, 5];
		assert_eq!(
			BoundedVec::<u32, ConstU32<4>>::decode(&mut &v.encode()[..]),
			Err("BoundedVec exceeds its limit".into()),
		);
	}

	#[test]
	fn dont_consume_more_data_than_bounded_len() {
		let v: Vec<u32> = vec![1, 2, 3, 4, 5];
		let data = v.encode();
		let data_input = &mut &data[..];

		BoundedVec::<u32, ConstU32<4>>::decode(data_input).unwrap_err();
		assert_eq!(data_input.len(), data.len() - Compact::<u32>::compact_len(&(data.len() as u32)));
	}

	#[test]
	fn eq_works() {
		// of same type
		let b1: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3];
		let b2: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3];
		assert_eq!(b1, b2);

		// of different type, but same value and bound.
		crate::parameter_types! {
			B1: u32 = 7;
			B2: u32 = 7;
		}
		let b1: BoundedVec<u32, B1> = bounded_vec![1, 2, 3];
		let b2: BoundedVec<u32, B2> = bounded_vec![1, 2, 3];
		assert_eq!(b1, b2);
	}

	#[test]
	fn ord_works() {
		use std::cmp::Ordering;
		let b1: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3];
		let b2: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 3, 2];

		// ordering for vec is lexicographic.
		assert_eq!(b1.cmp(&b2), Ordering::Less);
		assert_eq!(b1.cmp(&b2), b1.into_inner().cmp(&b2.into_inner()));
	}

	#[test]
	fn try_extend_works() {
		let mut b: BoundedVec<u32, ConstU32<5>> = bounded_vec![1, 2, 3];

		assert!(b.try_extend(vec![4].into_iter()).is_ok());
		assert_eq!(*b, vec![1, 2, 3, 4]);

		assert!(b.try_extend(vec![5].into_iter()).is_ok());
		assert_eq!(*b, vec![1, 2, 3, 4, 5]);

		assert!(b.try_extend(vec![6].into_iter()).is_err());
		assert_eq!(*b, vec![1, 2, 3, 4, 5]);

		let mut b: BoundedVec<u32, ConstU32<5>> = bounded_vec![1, 2, 3];
		assert!(b.try_extend(vec![4, 5].into_iter()).is_ok());
		assert_eq!(*b, vec![1, 2, 3, 4, 5]);

		let mut b: BoundedVec<u32, ConstU32<5>> = bounded_vec![1, 2, 3];
		assert!(b.try_extend(vec![4, 5, 6].into_iter()).is_err());
		assert_eq!(*b, vec![1, 2, 3]);
	}

	#[test]
	fn test_serializer() {
		let c: BoundedVec<u32, ConstU32<6>> = bounded_vec![0, 1, 2];
		assert_eq!(serde_json::json!(&c).to_string(), r#"[0,1,2]"#);
	}

	#[test]
	fn test_deserializer() {
		let c: BoundedVec<u32, ConstU32<6>> = serde_json::from_str(r#"[0,1,2]"#).unwrap();

		assert_eq!(c.len(), 3);
		assert_eq!(c[0], 0);
		assert_eq!(c[1], 1);
		assert_eq!(c[2], 2);
	}

	#[test]
	fn test_deserializer_bound() {
		let c: BoundedVec<u32, ConstU32<3>> = serde_json::from_str(r#"[0,1,2]"#).unwrap();

		assert_eq!(c.len(), 3);
		assert_eq!(c[0], 0);
		assert_eq!(c[1], 1);
		assert_eq!(c[2], 2);
	}

	#[test]
	fn test_deserializer_failed() {
		let c: Result<BoundedVec<u32, ConstU32<4>>, serde_json::error::Error> = serde_json::from_str(r#"[0,1,2,3,4]"#);

		match c {
			Err(msg) => assert_eq!(msg.to_string(), "out of bounds at line 1 column 11"),
			_ => unreachable!("deserializer must raise error"),
		}
	}

	#[test]
	fn bounded_vec_try_from_works() {
		assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0]).is_ok());
		assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0, 1]).is_ok());
		assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0, 1, 2]).is_err());
	}

	#[test]
	fn bounded_slice_try_from_works() {
		assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0][..]).is_ok());
		assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0, 1][..]).is_ok());
		assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0, 1, 2][..]).is_err());
	}

	#[test]
	fn can_be_collected() {
		let b1: BoundedVec<u32, ConstU32<5>> = bounded_vec![1, 2, 3, 4];
		let b2: BoundedVec<u32, ConstU32<5>> = b1.iter().map(|x| x + 1).try_collect().unwrap();
		assert_eq!(b2, vec![2, 3, 4, 5]);

		// can also be collected into a collection of length 4.
		let b2: BoundedVec<u32, ConstU32<4>> = b1.iter().map(|x| x + 1).try_collect().unwrap();
		assert_eq!(b2, vec![2, 3, 4, 5]);

		// can be mutated further into iterators that are `ExactSizedIterator`.
		let b2: BoundedVec<u32, ConstU32<4>> = b1.iter().map(|x| x + 1).rev().try_collect().unwrap();
		assert_eq!(b2, vec![5, 4, 3, 2]);

		let b2: BoundedVec<u32, ConstU32<4>> = b1.iter().map(|x| x + 1).rev().skip(2).try_collect().unwrap();
		assert_eq!(b2, vec![3, 2]);
		let b2: BoundedVec<u32, ConstU32<2>> = b1.iter().map(|x| x + 1).rev().skip(2).try_collect().unwrap();
		assert_eq!(b2, vec![3, 2]);

		let b2: BoundedVec<u32, ConstU32<4>> = b1.iter().map(|x| x + 1).rev().take(2).try_collect().unwrap();
		assert_eq!(b2, vec![5, 4]);
		let b2: BoundedVec<u32, ConstU32<2>> = b1.iter().map(|x| x + 1).rev().take(2).try_collect().unwrap();
		assert_eq!(b2, vec![5, 4]);

		// but these worn't work
		let b2: Result<BoundedVec<u32, ConstU32<3>>, _> = b1.iter().map(|x| x + 1).try_collect();
		assert!(b2.is_err());

		let b2: Result<BoundedVec<u32, ConstU32<1>>, _> = b1.iter().map(|x| x + 1).rev().take(2).try_collect();
		assert!(b2.is_err());
	}

	#[test]
	fn bounded_vec_debug_works() {
		let bound = BoundedVec::<u32, ConstU32<5>>::truncate_from(vec![1, 2, 3]);
		assert_eq!(format!("{:?}", bound), "BoundedVec([1, 2, 3], 5)");
	}

	#[test]
	fn bounded_slice_debug_works() {
		let bound = BoundedSlice::<u32, ConstU32<5>>::truncate_from(&[1, 2, 3]);
		assert_eq!(format!("{:?}", bound), "BoundedSlice([1, 2, 3], 5)");
	}

	#[test]
	fn bounded_vec_sort_by_key_works() {
		let mut v: BoundedVec<i32, ConstU32<5>> = bounded_vec![-5, 4, 1, -3, 2];
		// Sort by absolute value.
		v.sort_by_key(|k| k.abs());
		assert_eq!(v, vec![1, 2, -3, 4, -5]);
	}

	#[test]
	fn bounded_vec_truncate_from_works() {
		let unbound = vec![1, 2, 3, 4, 5];
		let bound = BoundedVec::<u32, ConstU32<3>>::truncate_from(unbound.clone());
		assert_eq!(bound, vec![1, 2, 3]);
	}

	#[test]
	fn bounded_slice_truncate_from_works() {
		let unbound = [1, 2, 3, 4, 5];
		let bound = BoundedSlice::<u32, ConstU32<3>>::truncate_from(&unbound);
		assert_eq!(bound, &[1, 2, 3][..]);
	}

	#[test]
	fn bounded_slice_partialeq_slice_works() {
		let unbound = [1, 2, 3];
		let bound = BoundedSlice::<u32, ConstU32<3>>::truncate_from(&unbound);

		assert_eq!(bound, &unbound[..]);
		assert!(bound == &unbound[..]);
	}

	#[test]
	fn bounded_vec_try_rotate_left_works() {
		let o = BoundedVec::<u32, ConstU32<3>>::truncate_from(vec![1, 2, 3]);
		let mut bound = o.clone();

		bound.try_rotate_left(0).unwrap();
		assert_eq!(bound, o);
		bound.try_rotate_left(3).unwrap();
		assert_eq!(bound, o);

		bound.try_rotate_left(4).unwrap_err();
		assert_eq!(bound, o);

		bound.try_rotate_left(1).unwrap();
		assert_eq!(bound, vec![2, 3, 1]);
		bound.try_rotate_left(2).unwrap();
		assert_eq!(bound, o);
	}

	#[test]
	fn bounded_vec_try_rotate_right_works() {
		let o = BoundedVec::<u32, ConstU32<3>>::truncate_from(vec![1, 2, 3]);
		let mut bound = o.clone();

		bound.try_rotate_right(0).unwrap();
		assert_eq!(bound, o);
		bound.try_rotate_right(3).unwrap();
		assert_eq!(bound, o);

		bound.try_rotate_right(4).unwrap_err();
		assert_eq!(bound, o);

		bound.try_rotate_right(1).unwrap();
		assert_eq!(bound, vec![3, 1, 2]);
		bound.try_rotate_right(2).unwrap();
		assert_eq!(bound, o);
	}

	// Just a test that structs containing `BoundedVec` and `BoundedSlice` can derive `Hash`. (This was broken when
	// they were deriving `Hash`).
	#[test]
	#[cfg(feature = "std")]
	fn container_can_derive_hash() {
		#[derive(Hash)]
		struct Foo<'a> {
			bar: u8,
			slice: BoundedSlice<'a, usize, ConstU8<8>>,
			map: BoundedVec<String, ConstU32<16>>,
		}
	}
}