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
impl Byte
Sourcepub fn from_unit(value: f64, unit: ByteUnit) -> Result<Byte, ByteError>
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());Sourcepub const fn from_bytes(bytes: u128) -> Byte
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}Sourcepub fn from_str<S>(s: S) -> Result<Byte, ByteError>
pub fn from_str<S>(s: S) -> Result<Byte, ByteError>
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
impl Byte
Sourcepub const fn get_bytes(&self) -> u128
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);Sourcepub fn get_adjusted_unit(&self, unit: ByteUnit) -> AdjustedByte
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());Sourcepub fn get_appropriate_unit(&self, binary_multiples: bool) -> AdjustedByte
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<'de> Deserialize<'de> for Byte
impl<'de> Deserialize<'de> for Byte
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Byte, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
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 From<AdjustedByte> for Byte
impl From<AdjustedByte> for Byte
Source§fn from(other: AdjustedByte) -> Byte
fn from(other: AdjustedByte) -> Byte
Converts to this type from the input type.
Source§impl Ord for Byte
impl Ord for Byte
Source§impl PartialOrd for Byte
impl PartialOrd for Byte
Source§impl Serialize for Byte
impl Serialize for Byte
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
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
impl Copy for Byte
impl Eq for Byte
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
impl<T> AbiEnumVisitor for T
default fn visit_for_abi( &self, _digester: &mut AbiDigester, ) -> Result<AbiDigester, DigestError>
Source§impl<T> AbiEnumVisitor for T
impl<T> AbiEnumVisitor for T
default fn visit_for_abi( &self, digester: &mut AbiDigester, ) -> Result<AbiDigester, DigestError>
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
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
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>
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 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>
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