pub trait DefensiveTruncateFrom<T> {
    fn defensive_truncate_from(unbound: T) -> Self;
Expand description

Construct an object by defensively truncating an input if the TryFrom conversion fails.

Required Methods

Use TryFrom first and defensively fall back to truncating otherwise.

use frame_support::{BoundedVec, traits::DefensiveTruncateFrom};
use sp_runtime::traits::ConstU32;

let unbound = vec![1, 2];
let bound = BoundedVec::<u8, ConstU32<2>>::defensive_truncate_from(unbound);

assert_eq!(bound, vec![1, 2]);