mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-16 03:12:41 +00:00
Bump ssz_types to v0.12.2 (#8032)
https://github.com/sigp/lighthouse/issues/8012 Replace all instances of `VariableList::from` and `FixedVector::from` to their `try_from` variants. While I tried to use proper error handling in most cases, there were certain situations where adding an `expect` for situations where `try_from` can trivially never fail avoided adding a lot of extra complexity. Co-Authored-By: Mac L <mjladson@pm.me> Co-Authored-By: Michael Sproul <michaelsproul@users.noreply.github.com> Co-Authored-By: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
@@ -29,15 +29,21 @@ pub const MAX_ERROR_LEN: u64 = 256;
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ErrorType(pub VariableList<u8, MaxErrorLen>);
|
||||
|
||||
impl From<String> for ErrorType {
|
||||
fn from(s: String) -> Self {
|
||||
Self(VariableList::from(s.as_bytes().to_vec()))
|
||||
impl From<&str> for ErrorType {
|
||||
// This will truncate the error if `string.as_bytes()` exceeds `MaxErrorLen`.
|
||||
fn from(s: &str) -> Self {
|
||||
let mut bytes = s.as_bytes().to_vec();
|
||||
bytes.truncate(MAX_ERROR_LEN as usize);
|
||||
Self(
|
||||
VariableList::try_from(bytes)
|
||||
.expect("length should not exceed MaxErrorLen after truncation"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for ErrorType {
|
||||
fn from(s: &str) -> Self {
|
||||
Self(VariableList::from(s.as_bytes().to_vec()))
|
||||
impl From<String> for ErrorType {
|
||||
fn from(s: String) -> Self {
|
||||
Self::from(s.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user