Trait twilio_async::Borrow 1.0.0[−][src]
A trait for borrowing data.
In Rust, it is common to provide different representations of a type for
different use cases. For instance, storage location and management for a
value can be specifically chosen as appropriate for a particular use via
pointer types such as Box<T>
or Rc<T>
. Beyond these generic
wrappers that can be used with any type, some types provide optional
facets providing potentially costly functionality. An example for such a
type is String
which adds the ability to extend a string to the basic
str
. This requires keeping additional information unnecessary for a
simple, immutable string.
These types provide access to the underlying data through references
to the type of that data. They are said to be ‘borrowed as’ that type.
For instance, a Box<T>
can be borrowed as T
while a String
can be borrowed as str
.
Types express that they can be borrowed as some type T
by implementing
Borrow<T>
, providing a reference to a T
in the trait’s
borrow
method. A type is free to borrow as several different types.
If it wishes to mutably borrow as the type – allowing the underlying data
to be modified, it can additionally implement BorrowMut<T>
.
Further, when providing implementations for additional traits, it needs
to be considered whether they should behave identical to those of the
underlying type as a consequence of acting as a representation of that
underlying type. Generic code typically uses Borrow<T>
when it relies
on the identical behavior of these additional trait implementations.
These traits will likely appear as additional trait bounds.
If generic code merely needs to work for all types that can
provide a reference to related type T
, it is often better to use
AsRef<T>
as more types can safely implement it.
Examples
As a data collection, HashMap<K, V>
owns both keys and values. If
the key’s actual data is wrapped in a managing type of some kind, it
should, however, still be possible to search for a value using a
reference to the key’s data. For instance, if the key is a string, then
it is likely stored with the hash map as a String
, while it should
be possible to search using a &str
. Thus, insert
needs to
operate on a String
while get
needs to be able to use a &str
.
Slightly simplified, the relevant parts of HashMap<K, V>
look like
this:
use std::borrow::Borrow; use std::hash::Hash; pub struct HashMap<K, V> { // fields omitted } impl<K, V> HashMap<K, V> { pub fn insert(&self, key: K, value: V) -> Option<V> where K: Hash + Eq { // ... } pub fn get<Q>(&self, k: &Q) -> Option<&V> where K: Borrow<Q>, Q: Hash + Eq + ?Sized { // ... } }
The entire hash map is generic over a key type K
. Because these keys
are stored with the hash map, this type has to own the key’s data.
When inserting a key-value pair, the map is given such a K
and needs
to find the correct hash bucket and check if the key is already present
based on that K
. It therefore requires K: Hash + Eq
.
When searching for a value in the map, however, having to provide a
reference to a K
as the key to search for would require to always
create such an owned value. For string keys, this would mean a String
value needs to be created just for the search for cases where only a
str
is available.
Instead, the get
method is generic over the type of the underlying key
data, called Q
in the method signature above. It states that K
borrows as a Q
by requiring that K: Borrow<Q>
. By additionally
requiring Q: Hash + Eq
, it signals the requirement that K
and Q
have implementations of the Hash
and Eq
traits that produce identical
results.
The implementation of get
relies in particular on identical
implementations of Hash
by determining the key’s hash bucket by calling
Hash::hash
on the Q
value even though it inserted the key based on
the hash value calculated from the K
value.
As a consequence, the hash map breaks if a K
wrapping a Q
value
produces a different hash than Q
. For instance, imagine you have a
type that wraps a string but compares ASCII letters ignoring their case:
pub struct CaseInsensitiveString(String); impl PartialEq for CaseInsensitiveString { fn eq(&self, other: &Self) -> bool { self.0.eq_ignore_ascii_case(&other.0) } } impl Eq for CaseInsensitiveString { }
Because two equal values need to produce the same hash value, the
implementation of Hash
needs to ignore ASCII case, too:
impl Hash for CaseInsensitiveString { fn hash<H: Hasher>(&self, state: &mut H) { for c in self.0.as_bytes() { c.to_ascii_lowercase().hash(state) } } }
Can CaseInsensitiveString
implement Borrow<str>
? It certainly can
provide a reference to a string slice via its contained owned string.
But because its Hash
implementation differs, it behaves differently
from str
and therefore must not, in fact, implement Borrow<str>
.
If it wants to allow others access to the underlying str
, it can do
that via AsRef<str>
which doesn’t carry any extra requirements.
Required Methods
fn borrow(&self) -> &Borrowed
Immutably borrows from an owned value.
Examples
use std::borrow::Borrow; fn check<T: Borrow<str>>(s: T) { assert_eq!("Hello", s.borrow()); } let s = "Hello".to_string(); check(s); let s = "Hello"; check(s);
Implementations on Foreign Types
impl Borrow<CStr> for CString
[src]
impl Borrow<CStr> for CString
impl Borrow<Path> for PathBuf
[src]
impl Borrow<Path> for PathBuf
impl Borrow<OsStr> for OsString
[src]
impl Borrow<OsStr> for OsString
impl<T> Borrow<[T]> for [T; 20]
[src]
impl<T> Borrow<[T]> for [T; 20]
impl<T> Borrow<[T]> for [T; 31]
[src]
impl<T> Borrow<[T]> for [T; 31]
impl<T> Borrow<[T]> for [T; 5]
[src]
impl<T> Borrow<[T]> for [T; 5]
impl<T> Borrow<[T]> for [T; 2]
[src]
impl<T> Borrow<[T]> for [T; 2]
impl<T> Borrow<[T]> for [T; 8]
[src]
impl<T> Borrow<[T]> for [T; 8]
impl<T> Borrow<[T]> for [T; 21]
[src]
impl<T> Borrow<[T]> for [T; 21]
impl<T> Borrow<[T]> for [T; 7]
[src]
impl<T> Borrow<[T]> for [T; 7]
impl<'a, T> Borrow<T> for &'a mut T where
T: ?Sized,
[src]
impl<'a, T> Borrow<T> for &'a mut T where
T: ?Sized,
impl<T> Borrow<[T]> for [T; 6]
[src]
impl<T> Borrow<[T]> for [T; 6]
impl<T> Borrow<[T]> for [T; 22]
[src]
impl<T> Borrow<[T]> for [T; 22]
impl<T> Borrow<[T]> for [T; 16]
[src]
impl<T> Borrow<[T]> for [T; 16]
impl<T> Borrow<[T]> for [T; 32]
[src]
impl<T> Borrow<[T]> for [T; 32]
impl<T> Borrow<[T]> for [T; 23]
[src]
impl<T> Borrow<[T]> for [T; 23]
impl<T> Borrow<[T]> for [T; 0]
[src]
impl<T> Borrow<[T]> for [T; 0]
impl<T> Borrow<[T]> for [T; 3]
[src]
impl<T> Borrow<[T]> for [T; 3]
impl<T> Borrow<[T]> for [T; 15]
[src]
impl<T> Borrow<[T]> for [T; 15]
impl<T> Borrow<[T]> for [T; 24]
[src]
impl<T> Borrow<[T]> for [T; 24]
impl<T> Borrow<[T]> for [T; 11]
[src]
impl<T> Borrow<[T]> for [T; 11]
impl<T> Borrow<[T]> for [T; 10]
[src]
impl<T> Borrow<[T]> for [T; 10]
impl<T> Borrow<[T]> for [T; 28]
[src]
impl<T> Borrow<[T]> for [T; 28]
impl<T> Borrow<[T]> for [T; 19]
[src]
impl<T> Borrow<[T]> for [T; 19]
impl<T> Borrow<[T]> for [T; 25]
[src]
impl<T> Borrow<[T]> for [T; 25]
impl<'a, T> Borrow<T> for &'a T where
T: ?Sized,
[src]
impl<'a, T> Borrow<T> for &'a T where
T: ?Sized,
impl<T> Borrow<[T]> for [T; 14]
[src]
impl<T> Borrow<[T]> for [T; 14]
impl<T> Borrow<[T]> for [T; 12]
[src]
impl<T> Borrow<[T]> for [T; 12]
impl<T> Borrow<[T]> for [T; 18]
[src]
impl<T> Borrow<[T]> for [T; 18]
impl<T> Borrow<[T]> for [T; 13]
[src]
impl<T> Borrow<[T]> for [T; 13]
impl<T> Borrow<[T]> for [T; 26]
[src]
impl<T> Borrow<[T]> for [T; 26]
impl<T> Borrow<[T]> for [T; 30]
[src]
impl<T> Borrow<[T]> for [T; 30]
impl<T> Borrow<[T]> for [T; 17]
[src]
impl<T> Borrow<[T]> for [T; 17]
impl<T> Borrow<[T]> for [T; 1]
[src]
impl<T> Borrow<[T]> for [T; 1]
impl<T> Borrow<[T]> for [T; 29]
[src]
impl<T> Borrow<[T]> for [T; 29]
impl<T> Borrow<[T]> for [T; 4]
[src]
impl<T> Borrow<[T]> for [T; 4]
impl<T> Borrow<[T]> for [T; 9]
[src]
impl<T> Borrow<[T]> for [T; 9]
impl<T> Borrow<[T]> for [T; 27]
[src]
impl<T> Borrow<[T]> for [T; 27]
impl<T> Borrow<T> for Arc<T> where
T: ?Sized,
[src]
impl<T> Borrow<T> for Arc<T> where
T: ?Sized,
impl<T> Borrow<T> for Box<T> where
T: ?Sized,
[src]
impl<T> Borrow<T> for Box<T> where
T: ?Sized,
impl<T> Borrow<[T]> for Vec<T>
[src]
impl<T> Borrow<[T]> for Vec<T>
impl<'a, B> Borrow<B> for Cow<'a, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: 'a,
[src]
impl<'a, B> Borrow<B> for Cow<'a, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: 'a,
impl Borrow<str> for String
[src]
impl Borrow<str> for String
impl Borrow<[u8]> for BytesMut
[src]
impl Borrow<[u8]> for BytesMut
impl Borrow<[u8]> for Bytes
[src]
impl Borrow<[u8]> for Bytes
impl<T> Borrow<T> for Owned<T>
impl<T> Borrow<T> for Owned<T>
impl<A> Borrow<[<A as Array>::Item]> for ArrayVec<A> where
A: Array,
[src]
impl<A> Borrow<[<A as Array>::Item]> for ArrayVec<A> where
A: Array,
impl<A> Borrow<str> for ArrayString<A> where
A: Array<Item = u8>,
[src]
impl<A> Borrow<str> for ArrayString<A> where
A: Array<Item = u8>,
impl<A> Borrow<[<A as Array>::Item]> for SmallVec<A> where
A: Array,
impl<A> Borrow<[<A as Array>::Item]> for SmallVec<A> where
A: Array,
impl Borrow<X509Ref> for X509
impl Borrow<X509Ref> for X509
impl Borrow<RsaRef> for Rsa
impl Borrow<RsaRef> for Rsa
impl Borrow<ConfRef> for Conf
impl Borrow<ConfRef> for Conf
impl Borrow<SslSessionRef> for SslSession
impl Borrow<SslSessionRef> for SslSession
fn borrow(&self) -> &SslSessionRef
fn borrow(&self) -> &SslSessionRef
impl Borrow<EcKeyRef> for EcKey
impl Borrow<EcKeyRef> for EcKey
impl Borrow<DsaRef> for Dsa
impl Borrow<DsaRef> for Dsa
impl Borrow<Asn1ObjectRef> for Asn1Object
impl Borrow<Asn1ObjectRef> for Asn1Object
fn borrow(&self) -> &Asn1ObjectRef
fn borrow(&self) -> &Asn1ObjectRef
impl Borrow<EcPointRef> for EcPoint
impl Borrow<EcPointRef> for EcPoint
fn borrow(&self) -> &EcPointRef
fn borrow(&self) -> &EcPointRef
impl Borrow<Pkcs12Ref> for Pkcs12
impl Borrow<Pkcs12Ref> for Pkcs12
impl Borrow<X509NameEntryRef> for X509NameEntry
impl Borrow<X509NameEntryRef> for X509NameEntry
fn borrow(&self) -> &X509NameEntryRef
fn borrow(&self) -> &X509NameEntryRef
impl Borrow<X509NameRef> for X509Name
impl Borrow<X509NameRef> for X509Name
fn borrow(&self) -> &X509NameRef
fn borrow(&self) -> &X509NameRef
impl Borrow<OcspBasicResponseRef> for OcspBasicResponse
impl Borrow<OcspBasicResponseRef> for OcspBasicResponse
fn borrow(&self) -> &OcspBasicResponseRef
fn borrow(&self) -> &OcspBasicResponseRef
impl Borrow<OcspCertIdRef> for OcspCertId
impl Borrow<OcspCertIdRef> for OcspCertId
fn borrow(&self) -> &OcspCertIdRef
fn borrow(&self) -> &OcspCertIdRef
impl Borrow<BigNumRef> for BigNum
impl Borrow<BigNumRef> for BigNum
impl Borrow<X509StoreContextRef> for X509StoreContext
impl Borrow<X509StoreContextRef> for X509StoreContext
fn borrow(&self) -> &X509StoreContextRef
fn borrow(&self) -> &X509StoreContextRef
impl Borrow<Asn1BitStringRef> for Asn1BitString
impl Borrow<Asn1BitStringRef> for Asn1BitString
fn borrow(&self) -> &Asn1BitStringRef
fn borrow(&self) -> &Asn1BitStringRef
impl Borrow<BigNumContextRef> for BigNumContext
impl Borrow<BigNumContextRef> for BigNumContext
fn borrow(&self) -> &BigNumContextRef
fn borrow(&self) -> &BigNumContextRef
impl Borrow<OpensslStringRef> for OpensslString
impl Borrow<OpensslStringRef> for OpensslString
fn borrow(&self) -> &OpensslStringRef
fn borrow(&self) -> &OpensslStringRef
impl Borrow<CmsContentInfoRef> for CmsContentInfo
impl Borrow<CmsContentInfoRef> for CmsContentInfo
fn borrow(&self) -> &CmsContentInfoRef
fn borrow(&self) -> &CmsContentInfoRef
impl Borrow<X509StoreRef> for X509Store
impl Borrow<X509StoreRef> for X509Store
fn borrow(&self) -> &X509StoreRef
fn borrow(&self) -> &X509StoreRef
impl<T> Borrow<StackRef<T>> for Stack<T> where
T: Stackable,
[src]
impl<T> Borrow<StackRef<T>> for Stack<T> where
T: Stackable,
impl Borrow<GeneralNameRef> for GeneralName
impl Borrow<GeneralNameRef> for GeneralName
fn borrow(&self) -> &GeneralNameRef
fn borrow(&self) -> &GeneralNameRef
impl Borrow<PKeyCtxRef> for PKeyCtx
impl Borrow<PKeyCtxRef> for PKeyCtx
fn borrow(&self) -> &PKeyCtxRef
fn borrow(&self) -> &PKeyCtxRef
impl Borrow<Asn1IntegerRef> for Asn1Integer
impl Borrow<Asn1IntegerRef> for Asn1Integer
fn borrow(&self) -> &Asn1IntegerRef
fn borrow(&self) -> &Asn1IntegerRef
impl Borrow<DhRef> for Dh
impl Borrow<DhRef> for Dh
impl Borrow<X509StoreBuilderRef> for X509StoreBuilder
impl Borrow<X509StoreBuilderRef> for X509StoreBuilder
fn borrow(&self) -> &X509StoreBuilderRef
fn borrow(&self) -> &X509StoreBuilderRef
impl Borrow<OcspRequestRef> for OcspRequest
impl Borrow<OcspRequestRef> for OcspRequest
fn borrow(&self) -> &OcspRequestRef
fn borrow(&self) -> &OcspRequestRef
impl Borrow<OcspOneReqRef> for OcspOneReq
impl Borrow<OcspOneReqRef> for OcspOneReq
fn borrow(&self) -> &OcspOneReqRef
fn borrow(&self) -> &OcspOneReqRef
impl Borrow<X509ReqRef> for X509Req
impl Borrow<X509ReqRef> for X509Req
fn borrow(&self) -> &X509ReqRef
fn borrow(&self) -> &X509ReqRef
impl Borrow<Asn1StringRef> for Asn1String
impl Borrow<Asn1StringRef> for Asn1String
fn borrow(&self) -> &Asn1StringRef
fn borrow(&self) -> &Asn1StringRef
impl Borrow<SslRef> for Ssl
impl Borrow<SslRef> for Ssl
impl Borrow<PKeyRef> for PKey
impl Borrow<PKeyRef> for PKey
impl Borrow<OcspResponseRef> for OcspResponse
impl Borrow<OcspResponseRef> for OcspResponse
fn borrow(&self) -> &OcspResponseRef
fn borrow(&self) -> &OcspResponseRef
impl Borrow<Asn1GeneralizedTimeRef> for Asn1GeneralizedTime
impl Borrow<Asn1GeneralizedTimeRef> for Asn1GeneralizedTime
fn borrow(&self) -> &Asn1GeneralizedTimeRef
fn borrow(&self) -> &Asn1GeneralizedTimeRef
impl Borrow<EcGroupRef> for EcGroup
impl Borrow<EcGroupRef> for EcGroup
fn borrow(&self) -> &EcGroupRef
fn borrow(&self) -> &EcGroupRef
impl Borrow<X509ExtensionRef> for X509Extension
impl Borrow<X509ExtensionRef> for X509Extension
fn borrow(&self) -> &X509ExtensionRef
fn borrow(&self) -> &X509ExtensionRef
impl Borrow<SslContextRef> for SslContext
impl Borrow<SslContextRef> for SslContext
fn borrow(&self) -> &SslContextRef
fn borrow(&self) -> &SslContextRef
impl Borrow<Asn1TimeRef> for Asn1Time
impl Borrow<Asn1TimeRef> for Asn1Time
fn borrow(&self) -> &Asn1TimeRef
fn borrow(&self) -> &Asn1TimeRef
impl Borrow<X509AlgorithmRef> for X509Algorithm
impl Borrow<X509AlgorithmRef> for X509Algorithm
fn borrow(&self) -> &X509AlgorithmRef
fn borrow(&self) -> &X509AlgorithmRef
impl Borrow<EcKeyBuilderRef> for EcKeyBuilder
impl Borrow<EcKeyBuilderRef> for EcKeyBuilder
fn borrow(&self) -> &EcKeyBuilderRef
fn borrow(&self) -> &EcKeyBuilderRef