Byte

Struct Byte 

Source
pub struct Byte(/* private fields */);
Expand description

Represent the n-bytes data. Use associated functions: from_unit, from_bytes, from_str, to create the instance.

Implementations§

Source§

impl Byte

Source

pub fn from_unit(value: f64, unit: ByteUnit) -> Result<Byte, ByteError>

Create a new Byte object from a specified value and a unit. Accuracy should be taken care of.

§Examples
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_unit(1500f64, ByteUnit::KB).unwrap();

assert_eq!(1500000, result.get_bytes());
Source

pub const fn from_bytes(bytes: u128) -> Byte

Create a new Byte object from bytes.

§Examples
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_bytes(1500000);

assert_eq!(1500000, result.get_bytes());
Examples found in repository?
examples/end_to_end.rs (line 26)
9async fn main() {
10    // Get keypair
11    let keypair_file: String = std::env::args()
12        .skip(1)
13        .next()
14        .expect("no keypair file provided");
15    let keypair: Keypair = read_keypair_file(keypair_file).expect("failed to read keypair file");
16    println!("loaded keypair");
17
18    // Initialize client
19    let client = ShadowDriveClient::new(keypair, SOLANA_MAINNET_RPC);
20    println!("initialized client");
21
22    // Create account
23    let response = client
24        .create_storage_account(
25            "test",
26            Byte::from_bytes(2_u128.pow(20)),
27            shadow_drive_sdk::StorageAccountVersion::V2,
28        )
29        .await
30        .expect("failed to create storage account");
31    let account = Pubkey::from_str(&response.shdw_bucket.unwrap()).unwrap();
32    println!("created storage account");
33
34    // Upload files
35    let files: Vec<ShadowFile> = vec![
36        ShadowFile::file("alpha.txt".to_string(), "./examples/files/alpha.txt"),
37        ShadowFile::file(
38            "not_alpha.txt".to_string(),
39            "./examples/files/not_alpha.txt",
40        ),
41    ];
42    let response = client
43        .store_files(&account, files.clone())
44        .await
45        .expect("failed to upload files");
46    println!("uploaded files");
47    for url in &response.finalized_locations {
48        println!("    {url}")
49    }
50    // Try editing
51    for file in files {
52        let response = client
53            .edit_file(&account, file)
54            .await
55            .expect("failed to upload files");
56        assert!(!response.finalized_location.is_empty(), "failed edit");
57        println!("edited file: {}", response.finalized_location);
58    }
59
60    // Delete files
61    for url in response.finalized_locations {
62        client
63            .delete_file(&account, url)
64            .await
65            .expect("failed to delete files");
66    }
67    println!("deleted files");
68
69    // Delete account
70    client
71        .delete_storage_account(&account)
72        .await
73        .expect("failed to delete storage account");
74    println!("deleted account");
75}
Source

pub fn from_str<S>(s: S) -> Result<Byte, ByteError>
where S: AsRef<str>,

Create a new Byte object from string. Accuracy should be taken care of.

§Examples
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("123KiB").unwrap();

assert_eq!(Byte::from_unit(123f64, ByteUnit::KiB).unwrap(), result);
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("50.84 MB").unwrap();

assert_eq!(Byte::from_unit(50.84f64, ByteUnit::MB).unwrap(), result);
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 B").unwrap(); // 8 bytes

assert_eq!(8, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8").unwrap(); // 8 bytes

assert_eq!(8, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 b").unwrap(); // 8 bytes

assert_eq!(8, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 kb").unwrap(); // 8 kilobytes

assert_eq!(8000, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 kib").unwrap(); // 8 kibibytes

assert_eq!(8192, result.get_bytes());
use byte_unit::{Byte, ByteUnit};

let result = Byte::from_str("8 k").unwrap(); // 8 kilobytes

assert_eq!(8000, result.get_bytes());
Source§

impl Byte

Source

pub const fn get_bytes(&self) -> u128

Get bytes represented by a Byte object.

§Examples
use byte_unit::Byte;

let byte = Byte::from_str("123KiB").unwrap();

let result = byte.get_bytes();

assert_eq!(125952, result);
use byte_unit::Byte;

let byte = Byte::from_str("50.84 MB").unwrap();

let result = byte.get_bytes();

assert_eq!(50840000, result);
Source

pub fn get_adjusted_unit(&self, unit: ByteUnit) -> AdjustedByte

Adjust the unit and value for Byte object. Accuracy should be taken care of.

§Examples
use byte_unit::{Byte, ByteUnit};

let byte = Byte::from_str("123KiB").unwrap();

let adjusted_byte = byte.get_adjusted_unit(ByteUnit::KB);

assert_eq!("125.95 KB", adjusted_byte.to_string());
use byte_unit::{Byte, ByteUnit};

let byte = Byte::from_str("50.84 MB").unwrap();

let adjusted_byte = byte.get_adjusted_unit(ByteUnit::MiB);

assert_eq!("48.48 MiB", adjusted_byte.to_string());
Source

pub fn get_appropriate_unit(&self, binary_multiples: bool) -> AdjustedByte

Find the appropriate unit and value for Byte object. Accuracy should be taken care of.

§Examples
use byte_unit::Byte;

let byte = Byte::from_str("123KiB").unwrap();

let adjusted_byte = byte.get_appropriate_unit(false);

assert_eq!("125.95 KB", adjusted_byte.to_string());
use byte_unit::Byte;

let byte = Byte::from_str("50.84 MB").unwrap();

let adjusted_byte = byte.get_appropriate_unit(true);

assert_eq!("48.48 MiB", adjusted_byte.to_string());

Trait Implementations§

Source§

impl Clone for Byte

Source§

fn clone(&self) -> Byte

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Byte

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Byte

Source§

fn default() -> Byte

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Byte

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Byte, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Byte

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<AdjustedByte> for Byte

Source§

fn from(other: AdjustedByte) -> Byte

Converts to this type from the input type.
Source§

impl From<u128> for Byte

Source§

fn from(u: u128) -> Byte

Converts to this type from the input type.
Source§

impl From<u16> for Byte

Source§

fn from(u: u16) -> Byte

Converts to this type from the input type.
Source§

impl From<u32> for Byte

Source§

fn from(u: u32) -> Byte

Converts to this type from the input type.
Source§

impl From<u64> for Byte

Source§

fn from(u: u64) -> Byte

Converts to this type from the input type.
Source§

impl From<u8> for Byte

Source§

fn from(u: u8) -> Byte

Converts to this type from the input type.
Source§

impl From<usize> for Byte

Source§

fn from(u: usize) -> Byte

Converts to this type from the input type.
Source§

impl FromStr for Byte

Source§

type Err = ByteError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Byte, <Byte as FromStr>::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Byte

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Byte

Source§

fn cmp(&self, other: &Byte) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Byte

Source§

fn eq(&self, other: &Byte) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Byte

Source§

fn partial_cmp(&self, other: &Byte) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Byte

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&str> for Byte

Source§

type Error = ByteError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &str) -> Result<Byte, <Byte as TryFrom<&str>>::Error>

Performs the conversion.
Source§

impl Copy for Byte

Source§

impl Eq for Byte

Source§

impl StructuralPartialEq for Byte

Auto Trait Implementations§

§

impl Freeze for Byte

§

impl RefUnwindSafe for Byte

§

impl Send for Byte

§

impl Sync for Byte

§

impl Unpin for Byte

§

impl UnwindSafe for Byte

Blanket Implementations§

Source§

impl<T> AbiEnumVisitor for T
where T: Serialize + ?Sized,

Source§

default fn visit_for_abi( &self, _digester: &mut AbiDigester, ) -> Result<AbiDigester, DigestError>

Source§

impl<T> AbiEnumVisitor for T
where T: Serialize + AbiExample + ?Sized,

Source§

default fn visit_for_abi( &self, digester: &mut AbiDigester, ) -> Result<AbiDigester, DigestError>

Source§

impl<T> AbiExample for T

Source§

default fn example() -> T

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,