pub struct SwarmAddress(pub B256);Expand description
A 256-bit address for a chunk in the Swarm network
Tuple Fields§
§0: B256Implementations§
Source§impl SwarmAddress
impl SwarmAddress
Sourcepub const fn with_first_byte(byte: u8) -> Self
pub const fn with_first_byte(byte: u8) -> Self
Creates an address with only the first byte set, rest zeros.
The first byte controls proximity order (leading bits determine PO).
Sourcepub fn from_slice(slice: &[u8]) -> Result<Self>
pub fn from_slice(slice: &[u8]) -> Result<Self>
Creates a new address from a slice, checking the length
Sourcepub fn distance(&self, y: &Self) -> U256
pub fn distance(&self, y: &Self) -> U256
Calculate the distance between Self and address y in big-endian
Sourcepub fn distance_cmp(&self, x: &Self, y: &Self) -> Ordering
pub fn distance_cmp(&self, x: &Self, y: &Self) -> Ordering
Compares addresses x and y by their distance from self.
Returns:
Ordering::Lessifxis farther fromselfthany(i.e.,yis closer)Ordering::Greaterifxis closer toselfthanyOrdering::Equalifxandyare equidistant fromself
§Usage with min_by
This comparator is designed for use with Iterator::min_by to find
the address closest to self:
let target = SwarmAddress::zero();
let addresses = vec![
SwarmAddress::from(B256::repeat_byte(0x01)),
SwarmAddress::from(B256::repeat_byte(0x02)),
];
let closest = addresses.iter().min_by(|a, b| target.distance_cmp(a, b));Note: The ordering may seem inverted from intuition. Greater means x
is closer (smaller distance), because min_by selects the element for
which the comparator returns Less - and we want to select the one
that is NOT closer (i.e., has a larger distance), leaving the closest.
Sourcepub fn closer(&self, x: &Self, y: &Self) -> bool
pub fn closer(&self, x: &Self, y: &Self) -> bool
Determine if self is closer to x than to y.
Returns true if distance(self, x) < distance(self, y).
Sourcepub fn is_within_proximity(
&self,
other: &Self,
min_proximity: ProximityOrder,
) -> bool
pub fn is_within_proximity( &self, other: &Self, min_proximity: ProximityOrder, ) -> bool
Check if this address is within the given proximity to another address
Sourcepub fn proximity(&self, other: &Self) -> ProximityOrder
pub fn proximity(&self, other: &Self) -> ProximityOrder
Calculate the proximity order between self and another address.
Returns the number of leading bits that match between the two addresses,
capped at MAX_PO (31). Use this for standard Kademlia routing operations.
For operations requiring finer granularity (like reserve sampling),
use extended_proximity() instead.
Sourcepub fn extended_proximity(&self, other: &Self) -> u8
pub fn extended_proximity(&self, other: &Self) -> u8
Calculate the extended proximity order between self and another address.
Returns the number of leading bits that match between the two addresses,
capped at EXTENDED_PO (36). Use this for Kademlia bin balancing where
the algorithm checks po + BitSuffixLength + 1 (up to 36 for bin 31).
Returns a raw u8 because the extended range exceeds ProximityOrder’s
invariant (0..=MAX_PO). For standard routing, use
proximity() instead.
Sourcepub fn xor(&self, other: &Self) -> Self
pub fn xor(&self, other: &Self) -> Self
XOR distance - bitwise XOR of the two 32-byte addresses as a new
SwarmAddress. Useful when callers want the raw distance bytes
(e.g. for content-routing bias) rather than the proximity-order metric.
Methods from Deref<Target = B256>§
pub const ZERO: FixedBytes<N>
Sourcepub fn as_slice(&self) -> &[u8] ⓘ
pub fn as_slice(&self) -> &[u8] ⓘ
Returns a slice containing the entire array. Equivalent to &s[..].
Sourcepub fn covers(&self, other: &FixedBytes<N>) -> bool
pub fn covers(&self, other: &FixedBytes<N>) -> bool
Returns true if all bits set in self are also set in b.
Sourcepub fn const_eq(&self, other: &FixedBytes<N>) -> bool
pub fn const_eq(&self, other: &FixedBytes<N>) -> bool
Compile-time equality. NOT constant-time equality.
Sourcepub fn const_is_zero(&self) -> bool
pub fn const_is_zero(&self) -> bool
Returns true if no bits are set.
Methods from Deref<Target = [u8; N]>§
Sourcepub fn as_ascii(&self) -> Option<&[AsciiChar; N]>
🔬This is a nightly-only experimental API. (ascii_char)
pub fn as_ascii(&self) -> Option<&[AsciiChar; N]>
ascii_char)Converts this array of bytes into an array of ASCII characters,
or returns None if any of the characters is non-ASCII.
§Examples
#![feature(ascii_char)]
const HEX_DIGITS: [std::ascii::Char; 16] =
*b"0123456789abcdef".as_ascii().unwrap();
assert_eq!(HEX_DIGITS[1].as_str(), "1");
assert_eq!(HEX_DIGITS[10].as_str(), "a");Sourcepub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]
🔬This is a nightly-only experimental API. (ascii_char)
pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]
ascii_char)Converts this array of bytes into an array of ASCII characters, without checking whether they’re valid.
§Safety
Every byte in the array must be in 0..=127, or else this is UB.
1.57.0 · Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice containing the entire array. Equivalent to &s[..].
1.57.0 · Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice containing the entire array. Equivalent to
&mut s[..].
1.77.0 · Sourcepub fn each_ref(&self) -> [&T; N]
pub fn each_ref(&self) -> [&T; N]
Borrows each element and returns an array of references with the same
size as self.
§Example
let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);This method is particularly useful if combined with other methods, like
map. This way, you can avoid moving the original
array if its elements are not Copy.
let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);
// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);1.77.0 · Sourcepub fn each_mut(&mut self) -> [&mut T; N]
pub fn each_mut(&mut self) -> [&mut T; N]
Borrows each element mutably and returns an array of mutable references
with the same size as self.
§Example
let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);Sourcepub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
🔬This is a nightly-only experimental API. (split_array)
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
split_array)Divides one array reference into two at an index.
The first will contain all indices from [0, M) (excluding
the index M itself) and the second will contain all
indices from [M, N) (excluding the index N itself).
§Panics
Panics if M > N.
§Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.split_array_ref::<0>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<2>();
assert_eq!(left, &[1, 2]);
assert_eq!(right, &[3, 4, 5, 6]);
}
{
let (left, right) = v.split_array_ref::<6>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}Sourcepub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
🔬This is a nightly-only experimental API. (split_array)
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
split_array)Divides one mutable array reference into two at an index.
The first will contain all indices from [0, M) (excluding
the index M itself) and the second will contain all
indices from [M, N) (excluding the index N itself).
§Panics
Panics if M > N.
§Examples
#![feature(split_array)]
let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);Sourcepub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
🔬This is a nightly-only experimental API. (split_array)
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
split_array)Divides one array reference into two at an index from the end.
The first will contain all indices from [0, N - M) (excluding
the index N - M itself) and the second will contain all
indices from [N - M, N) (excluding the index N itself).
§Panics
Panics if M > N.
§Examples
#![feature(split_array)]
let v = [1, 2, 3, 4, 5, 6];
{
let (left, right) = v.rsplit_array_ref::<0>();
assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
assert_eq!(right, &[]);
}
{
let (left, right) = v.rsplit_array_ref::<2>();
assert_eq!(left, &[1, 2, 3, 4]);
assert_eq!(right, &[5, 6]);
}
{
let (left, right) = v.rsplit_array_ref::<6>();
assert_eq!(left, &[]);
assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}Sourcepub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
🔬This is a nightly-only experimental API. (split_array)
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
split_array)Divides one mutable array reference into two at an index from the end.
The first will contain all indices from [0, N - M) (excluding
the index N - M itself) and the second will contain all
indices from [N - M, N) (excluding the index N itself).
§Panics
Panics if M > N.
§Examples
#![feature(split_array)]
let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);Trait Implementations§
Source§impl<'a> Arbitrary<'a> for SwarmAddress
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for SwarmAddress
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl AsRef<[u8]> for SwarmAddress
impl AsRef<[u8]> for SwarmAddress
Source§impl Clone for SwarmAddress
impl Clone for SwarmAddress
Source§fn clone(&self) -> SwarmAddress
fn clone(&self) -> SwarmAddress
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for SwarmAddress
Source§impl Debug for SwarmAddress
impl Debug for SwarmAddress
Source§impl Default for SwarmAddress
impl Default for SwarmAddress
Source§impl Deref for SwarmAddress
impl Deref for SwarmAddress
Source§impl<'de> Deserialize<'de> for SwarmAddress
impl<'de> Deserialize<'de> for SwarmAddress
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for SwarmAddress
impl Display for SwarmAddress
impl Eq for SwarmAddress
Source§impl From<FixedBytes<32>> for SwarmAddress
impl From<FixedBytes<32>> for SwarmAddress
Source§impl From<SwarmAddress> for B256
impl From<SwarmAddress> for B256
Source§fn from(addr: SwarmAddress) -> Self
fn from(addr: SwarmAddress) -> Self
Source§impl From<SwarmAddress> for [u8; 32]
impl From<SwarmAddress> for [u8; 32]
Source§fn from(addr: SwarmAddress) -> Self
fn from(addr: SwarmAddress) -> Self
Source§impl From<SwarmAddress> for EntryRef
impl From<SwarmAddress> for EntryRef
Source§fn from(addr: ChunkAddress) -> Self
fn from(addr: ChunkAddress) -> Self
Source§impl Hash for SwarmAddress
impl Hash for SwarmAddress
Source§impl Ord for SwarmAddress
impl Ord for SwarmAddress
Source§fn cmp(&self, other: &SwarmAddress) -> Ordering
fn cmp(&self, other: &SwarmAddress) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for SwarmAddress
impl PartialEq for SwarmAddress
Source§fn eq(&self, other: &SwarmAddress) -> bool
fn eq(&self, other: &SwarmAddress) -> bool
self and other values to be equal, and is used by ==.Source§impl PartialOrd for SwarmAddress
impl PartialOrd for SwarmAddress
Source§impl Serialize for SwarmAddress
impl Serialize for SwarmAddress
impl StructuralPartialEq for SwarmAddress
Auto Trait Implementations§
impl Freeze for SwarmAddress
impl RefUnwindSafe for SwarmAddress
impl Send for SwarmAddress
impl Sync for SwarmAddress
impl Unpin for SwarmAddress
impl UnsafeUnpin for SwarmAddress
impl UnwindSafe for SwarmAddress
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<'de, T> BorrowedRpcObject<'de> for T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<'de, T> RpcBorrow<'de> for T
impl<T> RpcObject for T
impl<T> RpcRecv for T
impl<T> RpcSend for T
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
use ToHexExt instead
self into the result.
Lower case letters are used (e.g. f9b4ca).Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
use ToHexExt instead
self into the result.
Upper case letters are used (e.g. F9B4CA).Source§impl<T> ToHexExt for T
impl<T> ToHexExt for T
Source§fn encode_hex(&self) -> String
fn encode_hex(&self) -> String
self into the result.
Lower case letters are used (e.g. f9b4ca).Source§fn encode_hex_upper(&self) -> String
fn encode_hex_upper(&self) -> String
self into the result.
Upper case letters are used (e.g. F9B4CA).Source§fn encode_hex_with_prefix(&self) -> String
fn encode_hex_with_prefix(&self) -> String
self into the result with prefix 0x.
Lower case letters are used (e.g. 0xf9b4ca).Source§fn encode_hex_upper_with_prefix(&self) -> String
fn encode_hex_upper_with_prefix(&self) -> String
self into the result with prefix 0X.
Upper case letters are used (e.g. 0xF9B4CA).