Struct AccountIdRef
pub struct AccountIdRef(/* private fields */);Expand description
Account identifier. This is the human readable UTF-8 string which is used internally to index accounts on the network and their respective state.
This is the “referenced” version of the account ID. It is to AccountId what str is to String,
and works quite similarly to Path. Like with str and Path, you
can’t have a value of type AccountIdRef, but you can have a reference like &AccountIdRef or
&mut AccountIdRef.
This type supports zero-copy deserialization offered by serde, but cannot
do the same for borsh since the latter does not support zero-copy.
§Examples
use near_account_id::{AccountId, AccountIdRef};
use std::convert::{TryFrom, TryInto};
// Construction
let alice = AccountIdRef::new("alice.near").unwrap();
assert!(AccountIdRef::new("invalid.").is_err());Implementations§
§impl AccountIdRef
impl AccountIdRef
pub fn new<S>(id: &S) -> Result<&AccountIdRef, ParseAccountError>
pub fn new<S>(id: &S) -> Result<&AccountIdRef, ParseAccountError>
Construct a &AccountIdRef from a string reference.
This constructor validates the provided ID, and will produce an error when validation fails.
pub const fn new_or_panic(id: &str) -> &AccountIdRef
pub const fn new_or_panic(id: &str) -> &AccountIdRef
Construct a &AccountIdRef from with validation at compile time.
This constructor will panic if validation fails.
use near_account_id::AccountIdRef;
const ALICE: &AccountIdRef = AccountIdRef::new_or_panic("alice.near");pub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns a string slice of the entire Account ID.
§Examples
use near_account_id::AccountIdRef;
let carol = AccountIdRef::new("carol.near").unwrap();
assert_eq!("carol.near", carol.as_str());pub fn is_top_level(&self) -> bool
pub fn is_top_level(&self) -> bool
Returns true if the account ID is a top-level NEAR Account ID.
See Top-level Accounts.
§Examples
use near_account_id::AccountIdRef;
let near_tla = AccountIdRef::new("near").unwrap();
assert!(near_tla.is_top_level());
// "alice.near" is a sub account of "near" account
let alice = AccountIdRef::new("alice.near").unwrap();
assert!(!alice.is_top_level());pub fn sub_account(
&self,
name: impl AsRef<str>,
) -> Result<AccountId, ParseAccountError>
pub fn sub_account( &self, name: impl AsRef<str>, ) -> Result<AccountId, ParseAccountError>
Returns sub account of current account: {name}.{parent}.
Returns Err if the resulting sub account is invalid (e.g. too long).
§Examples
use near_account_id::AccountIdRef;
let parent = AccountIdRef::new("near").unwrap();
let child = parent.sub_account("alice").unwrap();
assert!(child.is_sub_account_of(parent));pub fn parent(&self) -> Option<&AccountIdRef>
pub fn parent(&self) -> Option<&AccountIdRef>
Returns parent account if current account is a subaccount.
§Examples
use near_account_id::AccountIdRef;
let tla = AccountIdRef::new("near").unwrap();
assert_eq!(tla.parent(), None);
let child = AccountIdRef::new("alice.near").unwrap();
let parent = child.parent().unwrap();
assert_eq!(parent, "near");
assert!(child.is_sub_account_of(parent));pub fn is_sub_account_of(&self, parent: impl AsRef<AccountIdRef>) -> bool
pub fn is_sub_account_of(&self, parent: impl AsRef<AccountIdRef>) -> bool
Returns true if the AccountId is a direct sub-account of the provided parent account.
See Subaccounts.
§Examples
use near_account_id::AccountId;
let near_tla: AccountId = "near".parse().unwrap();
assert!(near_tla.is_top_level());
let alice: AccountId = "alice.near".parse().unwrap();
assert!(alice.is_sub_account_of(&near_tla));
let alice_app: AccountId = "app.alice.near".parse().unwrap();
// While app.alice.near is a sub account of alice.near,
// app.alice.near is not a sub account of near
assert!(alice_app.is_sub_account_of(&alice));
assert!(!alice_app.is_sub_account_of(&near_tla));pub fn get_account_type(&self) -> AccountType
pub fn get_account_type(&self) -> AccountType
Returns AccountType::EthImplicitAccount if the AccountId is a 40 characters long hexadecimal prefixed with ‘0x’.
Returns AccountType::NearImplicitAccount if the AccountId is a 64 characters long hexadecimal.
Otherwise, returns AccountType::NamedAccount.
See Implicit-Accounts.
§Examples
use near_account_id::{AccountId, AccountType};
let alice: AccountId = "alice.near".parse().unwrap();
assert!(alice.get_account_type() == AccountType::NamedAccount);
let eth_rando = "0xb794f5ea0ba39494ce839613fffba74279579268"
.parse::<AccountId>()
.unwrap();
assert!(eth_rando.get_account_type() == AccountType::EthImplicitAccount);
let near_rando = "98793cd91a3f870fb126f66285808c7e094afcfc4eda8a970f6648cdf0dbd6de"
.parse::<AccountId>()
.unwrap();
assert!(near_rando.get_account_type() == AccountType::NearImplicitAccount);pub fn is_system(&self) -> bool
pub fn is_system(&self) -> bool
Returns true if this AccountId is the system account.
See System account.
§Examples
use near_account_id::AccountId;
let alice: AccountId = "alice.near".parse().unwrap();
assert!(!alice.is_system());
let system: AccountId = "system".parse().unwrap();
assert!(system.is_system());pub fn get_parent_account_id(&self) -> Option<&AccountIdRef>
pub fn get_parent_account_id(&self) -> Option<&AccountIdRef>
Returns parent’s account id reference
§Examples
use near_account_id::AccountIdRef;
let alice: &AccountIdRef = AccountIdRef::new_or_panic("alice.near");
let parent: &AccountIdRef = alice.get_parent_account_id().unwrap();
assert!(alice.is_sub_account_of(parent));
let near: &AccountIdRef = AccountIdRef::new_or_panic("near");
assert!(near.get_parent_account_id().is_none());
let implicit: &AccountIdRef = AccountIdRef::new_or_panic("248e104d1d4764d713c4211c13808c8fc887869c580f4178e60538ac5c2a0b26");
assert!(implicit.get_parent_account_id().is_none());Trait Implementations§
§impl AccountIdExt for AccountIdRef
impl AccountIdExt for AccountIdRef
§fn is_implicit(&self) -> bool
fn is_implicit(&self) -> bool
§fn is_evm_implicit(&self) -> bool
fn is_evm_implicit(&self) -> bool
§impl<'a> Arbitrary<'a> for &'a AccountIdRef
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for &'a AccountIdRef
arbitrary only.§fn size_hint(_depth: usize) -> (usize, Option<usize>)
fn size_hint(_depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read more§fn arbitrary(u: &mut Unstructured<'a>) -> Result<&'a AccountIdRef, Error>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<&'a AccountIdRef, Error>
Self from the given unstructured data. Read more§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<&'a AccountIdRef, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<&'a AccountIdRef, Error>
Self from the entirety of the given
unstructured data. Read more§impl AsRef<AccountIdRef> for AccountId
impl AsRef<AccountIdRef> for AccountId
§fn as_ref(&self) -> &AccountIdRef
fn as_ref(&self) -> &AccountIdRef
§impl AsRef<AccountIdRef> for AccountIdRef
fn foo(account_id: impl AsRef<AccountIdRef>) {}
let account_id: AccountId = "alice.near".parse().unwrap();
let account_id_ref: &AccountIdRef = account_id.as_ref();
foo(account_id_ref);
foo(account_id);
impl AsRef<AccountIdRef> for AccountIdRef
fn foo(account_id: impl AsRef<AccountIdRef>) {}
let account_id: AccountId = "alice.near".parse().unwrap();
let account_id_ref: &AccountIdRef = account_id.as_ref();
foo(account_id_ref);
foo(account_id);§fn as_ref(&self) -> &AccountIdRef
fn as_ref(&self) -> &AccountIdRef
§impl AsRef<str> for AccountIdRef
impl AsRef<str> for AccountIdRef
§impl Borrow<AccountIdRef> for AccountId
impl Borrow<AccountIdRef> for AccountId
§fn borrow(&self) -> &AccountIdRef
fn borrow(&self) -> &AccountIdRef
§impl BorshSchema for AccountIdRef
impl BorshSchema for AccountIdRef
§fn declaration() -> String
fn declaration() -> String
§fn add_definitions_recursively(definitions: &mut BTreeMap<String, Definition>)
fn add_definitions_recursively(definitions: &mut BTreeMap<String, Definition>)
§impl BorshSerialize for AccountIdRef
impl BorshSerialize for AccountIdRef
§impl Debug for AccountIdRef
impl Debug for AccountIdRef
§impl<'de> Deserialize<'de> for &'de AccountIdRef
impl<'de> Deserialize<'de> for &'de AccountIdRef
§fn deserialize<D>(
deserializer: D,
) -> Result<&'de AccountIdRef, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<&'de AccountIdRef, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl Display for AccountIdRef
impl Display for AccountIdRef
§impl<'a> From<&'a AccountIdRef> for AccountId
impl<'a> From<&'a AccountIdRef> for AccountId
§fn from(id: &'a AccountIdRef) -> AccountId
fn from(id: &'a AccountIdRef) -> AccountId
§impl Hash for AccountIdRef
impl Hash for AccountIdRef
§impl JsonSchema for AccountIdRef
Available on crate feature schemars-v0_8 only.
impl JsonSchema for AccountIdRef
schemars-v0_8 only.§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref keyword. Read more§fn schema_name() -> String
fn schema_name() -> String
§fn json_schema(_: &mut SchemaGenerator) -> Schema
fn json_schema(_: &mut SchemaGenerator) -> Schema
§impl Ord for AccountIdRef
impl Ord for AccountIdRef
§impl<'a> PartialEq<&'a AccountIdRef> for AccountId
impl<'a> PartialEq<&'a AccountIdRef> for AccountId
§fn eq(&self, other: &&'a AccountIdRef) -> bool
fn eq(&self, other: &&'a AccountIdRef) -> bool
self and other values to be equal, and is used by ==.§impl<'a> PartialEq<&'a AccountIdRef> for str
impl<'a> PartialEq<&'a AccountIdRef> for str
§fn eq(&self, other: &&'a AccountIdRef) -> bool
fn eq(&self, other: &&'a AccountIdRef) -> bool
self and other values to be equal, and is used by ==.§impl<'a> PartialEq<&'a str> for AccountIdRef
impl<'a> PartialEq<&'a str> for AccountIdRef
§impl<'a> PartialEq<AccountId> for &'a AccountIdRef
impl<'a> PartialEq<AccountId> for &'a AccountIdRef
§impl PartialEq<AccountId> for AccountIdRef
impl PartialEq<AccountId> for AccountIdRef
§impl<'a> PartialEq<AccountIdRef> for &'a str
impl<'a> PartialEq<AccountIdRef> for &'a str
§impl PartialEq<AccountIdRef> for AccountId
impl PartialEq<AccountIdRef> for AccountId
§impl PartialEq<AccountIdRef> for str
impl PartialEq<AccountIdRef> for str
§impl<'a> PartialEq<String> for &'a AccountIdRef
impl<'a> PartialEq<String> for &'a AccountIdRef
§impl PartialEq<String> for AccountIdRef
impl PartialEq<String> for AccountIdRef
§impl<'a> PartialEq<str> for &'a AccountIdRef
impl<'a> PartialEq<str> for &'a AccountIdRef
§impl PartialEq<str> for AccountIdRef
impl PartialEq<str> for AccountIdRef
§impl PartialEq for AccountIdRef
impl PartialEq for AccountIdRef
§impl<'a> PartialOrd<&'a AccountIdRef> for AccountId
impl<'a> PartialOrd<&'a AccountIdRef> for AccountId
§impl<'a> PartialOrd<&'a AccountIdRef> for str
impl<'a> PartialOrd<&'a AccountIdRef> for str
§impl<'a> PartialOrd<&'a str> for AccountIdRef
impl<'a> PartialOrd<&'a str> for AccountIdRef
§impl<'a> PartialOrd<AccountId> for &'a AccountIdRef
impl<'a> PartialOrd<AccountId> for &'a AccountIdRef
§impl PartialOrd<AccountId> for AccountIdRef
impl PartialOrd<AccountId> for AccountIdRef
§impl<'a> PartialOrd<AccountIdRef> for &'a str
impl<'a> PartialOrd<AccountIdRef> for &'a str
§impl PartialOrd<AccountIdRef> for AccountId
impl PartialOrd<AccountIdRef> for AccountId
§impl PartialOrd<AccountIdRef> for str
impl PartialOrd<AccountIdRef> for str
§impl<'a> PartialOrd<String> for &'a AccountIdRef
impl<'a> PartialOrd<String> for &'a AccountIdRef
§impl PartialOrd<String> for AccountIdRef
impl PartialOrd<String> for AccountIdRef
§impl<'a> PartialOrd<str> for &'a AccountIdRef
impl<'a> PartialOrd<str> for &'a AccountIdRef
§impl PartialOrd<str> for AccountIdRef
impl PartialOrd<str> for AccountIdRef
§impl PartialOrd for AccountIdRef
impl PartialOrd for AccountIdRef
§impl Serialize for AccountIdRef
impl Serialize for AccountIdRef
§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,
§impl ToOwned for AccountIdRef
impl ToOwned for AccountIdRef
§impl<'s> TryFrom<&'s str> for &'s AccountIdRef
impl<'s> TryFrom<&'s str> for &'s AccountIdRef
§impl TryIntoAccountId for &AccountIdRef
impl TryIntoAccountId for &AccountIdRef
impl Eq for AccountIdRef
impl StructuralPartialEq for AccountIdRef
Auto Trait Implementations§
impl Freeze for AccountIdRef
impl RefUnwindSafe for AccountIdRef
impl Send for AccountIdRef
impl !Sized for AccountIdRef
impl Sync for AccountIdRef
impl Unpin for AccountIdRef
impl UnsafeUnpin for AccountIdRef
impl UnwindSafe for AccountIdRef
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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.