Skip to content

Commit

Permalink
epee: unseal trait EpeeValue (#184)
Browse files Browse the repository at this point in the history
* unseal `trait EpeeValue`

* fix `container_as_blob.rs`

* clippy

* epee-encoding: remove `sealed`
  • Loading branch information
hinto-janai authored Jun 20, 2024
1 parent b76042a commit bef2a2c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 47 deletions.
21 changes: 1 addition & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion net/epee-encoding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ std = ["dep:thiserror", "bytes/std", "fixed-bytes/std"]
[dependencies]
fixed-bytes = { path = "../fixed-bytes", default-features = false }

sealed = "0.5.0"
paste = "1.0.14"
ref-cast = "1.0.22"
bytes = { workspace = true }
Expand Down
4 changes: 1 addition & 3 deletions net/epee-encoding/src/container_as_blob.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bytes::{Buf, BufMut, Bytes, BytesMut};
use ref_cast::RefCast;
use sealed::sealed;

use crate::{error::*, value::*, EpeeValue, InnerMarker, Marker};
use crate::{error::*, EpeeValue, InnerMarker, Marker};

#[derive(RefCast)]
#[repr(transparent)]
Expand All @@ -26,7 +25,6 @@ impl<'a, T: Containerable + EpeeValue> From<&'a Vec<T>> for &'a ContainerAsBlob<
}
}

#[sealed]
impl<T: Containerable + EpeeValue> EpeeValue for ContainerAsBlob<T> {
const MARKER: Marker = Marker::new(InnerMarker::String);

Expand Down
29 changes: 6 additions & 23 deletions net/epee-encoding/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
//! This module contains a [`EpeeValue`] trait and
//! impls for some possible base epee values.

use alloc::{string::String, vec::Vec};
/// This module contains a `sealed` [`EpeeValue`] trait and different impls for
/// the different possible base epee values.
use core::fmt::Debug;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use sealed::sealed;

use fixed_bytes::{ByteArray, ByteArrayVec};

use crate::{
io::*, varint::*, EpeeObject, Error, InnerMarker, Marker, Result, MAX_STRING_LEN_POSSIBLE,
};

/// A trait for epee values, this trait is sealed as all possible epee values are
/// defined in the lib, to make an [`EpeeValue`] outside the lib you will need to
/// use the trait [`EpeeObject`].
#[sealed(pub(crate))]
/// A trait for epee values.
///
/// All [`EpeeObject`] objects automatically implement [`EpeeValue`].
pub trait EpeeValue: Sized {
const MARKER: Marker;

Expand All @@ -37,7 +36,6 @@ pub trait EpeeValue: Sized {
fn write<B: BufMut>(self, w: &mut B) -> Result<()>;
}

#[sealed]
impl<T: EpeeObject> EpeeValue for T {
const MARKER: Marker = Marker::new(InnerMarker::Object);

Expand All @@ -56,7 +54,6 @@ impl<T: EpeeObject> EpeeValue for T {
}
}

#[sealed]
impl<T: EpeeObject> EpeeValue for Vec<T> {
const MARKER: Marker = T::MARKER.into_seq();

Expand Down Expand Up @@ -94,7 +91,6 @@ impl<T: EpeeObject> EpeeValue for Vec<T> {
}
}

#[sealed]
impl<T: EpeeObject + Debug, const N: usize> EpeeValue for [T; N] {
const MARKER: Marker = <T>::MARKER.into_seq();

Expand All @@ -119,7 +115,6 @@ impl<T: EpeeObject + Debug, const N: usize> EpeeValue for [T; N] {

macro_rules! epee_numb {
($numb:ty, $marker:ident, $read_fn:ident, $write_fn:ident) => {
#[sealed]
impl EpeeValue for $numb {
const MARKER: Marker = Marker::new(InnerMarker::$marker);

Expand Down Expand Up @@ -148,7 +143,6 @@ epee_numb!(u32, U32, get_u32_le, put_u32_le);
epee_numb!(u64, U64, get_u64_le, put_u64_le);
epee_numb!(f64, F64, get_f64_le, put_f64_le);

#[sealed]
impl EpeeValue for bool {
const MARKER: Marker = Marker::new(InnerMarker::Bool);

Expand All @@ -165,7 +159,6 @@ impl EpeeValue for bool {
}
}

#[sealed]
impl EpeeValue for Vec<u8> {
const MARKER: Marker = Marker::new(InnerMarker::String);

Expand Down Expand Up @@ -209,7 +202,6 @@ impl EpeeValue for Vec<u8> {
}
}

#[sealed::sealed]
impl EpeeValue for Bytes {
const MARKER: Marker = Marker::new(InnerMarker::String);

Expand Down Expand Up @@ -250,7 +242,6 @@ impl EpeeValue for Bytes {
}
}

#[sealed::sealed]
impl EpeeValue for BytesMut {
const MARKER: Marker = Marker::new(InnerMarker::String);

Expand Down Expand Up @@ -294,7 +285,6 @@ impl EpeeValue for BytesMut {
}
}

#[sealed::sealed]
impl<const N: usize> EpeeValue for ByteArrayVec<N> {
const MARKER: Marker = Marker::new(InnerMarker::String);

Expand Down Expand Up @@ -338,7 +328,6 @@ impl<const N: usize> EpeeValue for ByteArrayVec<N> {
}
}

#[sealed::sealed]
impl<const N: usize> EpeeValue for ByteArray<N> {
const MARKER: Marker = Marker::new(InnerMarker::String);

Expand Down Expand Up @@ -374,7 +363,6 @@ impl<const N: usize> EpeeValue for ByteArray<N> {
}
}

#[sealed]
impl EpeeValue for String {
const MARKER: Marker = Marker::new(InnerMarker::String);

Expand Down Expand Up @@ -403,7 +391,6 @@ impl EpeeValue for String {
}
}

#[sealed]
impl<const N: usize> EpeeValue for [u8; N] {
const MARKER: Marker = Marker::new(InnerMarker::String);

Expand All @@ -429,7 +416,6 @@ impl<const N: usize> EpeeValue for [u8; N] {
}
}

#[sealed]
impl<const N: usize> EpeeValue for Vec<[u8; N]> {
const MARKER: Marker = <[u8; N]>::MARKER.into_seq();

Expand Down Expand Up @@ -470,7 +456,6 @@ impl<const N: usize> EpeeValue for Vec<[u8; N]> {

macro_rules! epee_seq {
($val:ty) => {
#[sealed]
impl EpeeValue for Vec<$val> {
const MARKER: Marker = <$val>::MARKER.into_seq();

Expand Down Expand Up @@ -509,7 +494,6 @@ macro_rules! epee_seq {
}
}

#[sealed]
impl<const N: usize> EpeeValue for [$val; N] {
const MARKER: Marker = <$val>::MARKER.into_seq();

Expand Down Expand Up @@ -548,7 +532,6 @@ epee_seq!(String);
epee_seq!(Bytes);
epee_seq!(BytesMut);

#[sealed]
impl<T: EpeeValue> EpeeValue for Option<T> {
const MARKER: Marker = T::MARKER;

Expand Down

0 comments on commit bef2a2c

Please sign in to comment.