Trait bevy::utils::smallvec::alloc::borrow::Borrow

1.0.0 · source ·
pub trait Borrow<Borrowed>
where Borrowed: ?Sized,
{ // Required method fn borrow(&self) -> &Borrowed; }
Expand description

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 identically 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.

In particular Eq, Ord and Hash must be equivalent for borrowed and owned values: x.borrow() == y.borrow() should give the same result as x == y.

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§

source

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);

Implementors§

source§

impl Borrow<RawDisplayHandle> for DisplayHandle<'_>

source§

impl Borrow<RawWindowHandle> for WindowHandle<'_>

source§

impl Borrow<str> for SmolStr

source§

impl Borrow<str> for String

source§

impl Borrow<ObjectId> for WlBuffer

source§

impl Borrow<ObjectId> for WlCallback

source§

impl Borrow<ObjectId> for WlCompositor

source§

impl Borrow<ObjectId> for WlDataDevice

source§

impl Borrow<ObjectId> for WlDataDeviceManager

source§

impl Borrow<ObjectId> for WlDataOffer

source§

impl Borrow<ObjectId> for WlDataSource

source§

impl Borrow<ObjectId> for WlDisplay

source§

impl Borrow<ObjectId> for WlKeyboard

source§

impl Borrow<ObjectId> for WlOutput

source§

impl Borrow<ObjectId> for WlPointer

source§

impl Borrow<ObjectId> for WlRegion

source§

impl Borrow<ObjectId> for WlRegistry

source§

impl Borrow<ObjectId> for WlSeat

source§

impl Borrow<ObjectId> for WlShell

source§

impl Borrow<ObjectId> for WlShellSurface

source§

impl Borrow<ObjectId> for WlShm

source§

impl Borrow<ObjectId> for WlShmPool

source§

impl Borrow<ObjectId> for WlSubcompositor

source§

impl Borrow<ObjectId> for WlSubsurface

source§

impl Borrow<ObjectId> for WlSurface

source§

impl Borrow<ObjectId> for WlTouch

source§

impl Borrow<ObjectId> for ExtForeignToplevelHandleV1

source§

impl Borrow<ObjectId> for ExtForeignToplevelListV1

source§

impl Borrow<ObjectId> for ExtIdleNotificationV1

source§

impl Borrow<ObjectId> for ExtIdleNotifierV1

source§

impl Borrow<ObjectId> for ExtSessionLockManagerV1

source§

impl Borrow<ObjectId> for ExtSessionLockSurfaceV1

source§

impl Borrow<ObjectId> for ExtSessionLockV1

source§

impl Borrow<ObjectId> for ExtTransientSeatManagerV1

source§

impl Borrow<ObjectId> for ExtTransientSeatV1

source§

impl Borrow<ObjectId> for WpContentTypeManagerV1

source§

impl Borrow<ObjectId> for WpContentTypeV1

source§

impl Borrow<ObjectId> for WpCursorShapeDeviceV1

source§

impl Borrow<ObjectId> for WpCursorShapeManagerV1

source§

impl Borrow<ObjectId> for WpDrmLeaseConnectorV1

source§

impl Borrow<ObjectId> for WpDrmLeaseDeviceV1

source§

impl Borrow<ObjectId> for WpDrmLeaseRequestV1

source§

impl Borrow<ObjectId> for WpDrmLeaseV1

source§

impl Borrow<ObjectId> for WpFractionalScaleManagerV1

source§

impl Borrow<ObjectId> for WpFractionalScaleV1

source§

impl Borrow<ObjectId> for ZwpFullscreenShellModeFeedbackV1

source§

impl Borrow<ObjectId> for ZwpFullscreenShellV1

source§

impl Borrow<ObjectId> for ZwpIdleInhibitManagerV1

source§

impl Borrow<ObjectId> for ZwpIdleInhibitorV1

source§

impl Borrow<ObjectId> for ZwpInputMethodContextV1

source§

impl Borrow<ObjectId> for ZwpInputMethodV1

source§

impl Borrow<ObjectId> for ZwpInputPanelSurfaceV1

source§

impl Borrow<ObjectId> for ZwpInputPanelV1

source§

impl Borrow<ObjectId> for ZwpInputTimestampsManagerV1

source§

impl Borrow<ObjectId> for ZwpInputTimestampsV1

source§

impl Borrow<ObjectId> for ZwpKeyboardShortcutsInhibitManagerV1

source§

impl Borrow<ObjectId> for ZwpKeyboardShortcutsInhibitorV1

source§

impl Borrow<ObjectId> for ZwpLinuxBufferParamsV1

source§

impl Borrow<ObjectId> for ZwpLinuxDmabufFeedbackV1

source§

impl Borrow<ObjectId> for ZwpLinuxDmabufV1

source§

impl Borrow<ObjectId> for ZwpLinuxBufferReleaseV1

source§

impl Borrow<ObjectId> for ZwpLinuxExplicitSynchronizationV1

source§

impl Borrow<ObjectId> for ZwpLinuxSurfaceSynchronizationV1

source§

impl Borrow<ObjectId> for ZwpConfinedPointerV1

source§

impl Borrow<ObjectId> for ZwpLockedPointerV1

source§

impl Borrow<ObjectId> for ZwpPointerConstraintsV1

source§

impl Borrow<ObjectId> for ZwpPointerGestureHoldV1

source§

impl Borrow<ObjectId> for ZwpPointerGesturePinchV1

source§

impl Borrow<ObjectId> for ZwpPointerGestureSwipeV1

source§

impl Borrow<ObjectId> for ZwpPointerGesturesV1

source§

impl Borrow<ObjectId> for WpPresentation

source§

impl Borrow<ObjectId> for WpPresentationFeedback

source§

impl Borrow<ObjectId> for ZwpPrimarySelectionDeviceManagerV1

source§

impl Borrow<ObjectId> for ZwpPrimarySelectionDeviceV1

source§

impl Borrow<ObjectId> for ZwpPrimarySelectionOfferV1

source§

impl Borrow<ObjectId> for ZwpPrimarySelectionSourceV1

source§

impl Borrow<ObjectId> for ZwpRelativePointerManagerV1

source§

impl Borrow<ObjectId> for ZwpRelativePointerV1

source§

impl Borrow<ObjectId> for WpSecurityContextManagerV1

source§

impl Borrow<ObjectId> for WpSecurityContextV1

source§

impl Borrow<ObjectId> for WpSinglePixelBufferManagerV1

source§

impl Borrow<ObjectId> for ZwpTabletManagerV1

source§

impl Borrow<ObjectId> for ZwpTabletSeatV1

source§

impl Borrow<ObjectId> for ZwpTabletToolV1

source§

impl Borrow<ObjectId> for ZwpTabletV1

source§

impl Borrow<ObjectId> for ZwpTabletManagerV2

source§

impl Borrow<ObjectId> for ZwpTabletPadGroupV2

source§

impl Borrow<ObjectId> for ZwpTabletPadRingV2

source§

impl Borrow<ObjectId> for ZwpTabletPadStripV2

source§

impl Borrow<ObjectId> for ZwpTabletPadV2

source§

impl Borrow<ObjectId> for ZwpTabletSeatV2

source§

impl Borrow<ObjectId> for ZwpTabletToolV2

source§

impl Borrow<ObjectId> for ZwpTabletV2

source§

impl Borrow<ObjectId> for WpTearingControlManagerV1

source§

impl Borrow<ObjectId> for WpTearingControlV1

source§

impl Borrow<ObjectId> for ZwpTextInputManagerV1

source§

impl Borrow<ObjectId> for ZwpTextInputV1

source§

impl Borrow<ObjectId> for ZwpTextInputManagerV3

source§

impl Borrow<ObjectId> for ZwpTextInputV3

source§

impl Borrow<ObjectId> for WpViewport

source§

impl Borrow<ObjectId> for WpViewporter

source§

impl Borrow<ObjectId> for XdgActivationTokenV1

source§

impl Borrow<ObjectId> for XdgActivationV1

source§

impl Borrow<ObjectId> for ZxdgDecorationManagerV1

source§

impl Borrow<ObjectId> for ZxdgToplevelDecorationV1

source§

impl Borrow<ObjectId> for ZxdgExportedV1

source§

impl Borrow<ObjectId> for ZxdgExporterV1

source§

impl Borrow<ObjectId> for ZxdgImportedV1

source§

impl Borrow<ObjectId> for ZxdgImporterV1

source§

impl Borrow<ObjectId> for ZxdgExportedV2

source§

impl Borrow<ObjectId> for ZxdgExporterV2

source§

impl Borrow<ObjectId> for ZxdgImportedV2

source§

impl Borrow<ObjectId> for ZxdgImporterV2

source§

impl Borrow<ObjectId> for XdgPopup

source§

impl Borrow<ObjectId> for XdgPositioner

source§

impl Borrow<ObjectId> for XdgSurface

source§

impl Borrow<ObjectId> for XdgToplevel

source§

impl Borrow<ObjectId> for XdgWmBase

source§

impl Borrow<ObjectId> for ZxdgOutputManagerV1

source§

impl Borrow<ObjectId> for ZxdgOutputV1

source§

impl Borrow<ObjectId> for ZwpXwaylandKeyboardGrabManagerV1

source§

impl Borrow<ObjectId> for ZwpXwaylandKeyboardGrabV1

source§

impl Borrow<ObjectId> for XwaylandShellV1

source§

impl Borrow<ObjectId> for XwaylandSurfaceV1

source§

impl Borrow<ObjectId> for OrgKdeKwinAppmenu

source§

impl Borrow<ObjectId> for OrgKdeKwinAppmenuManager

source§

impl Borrow<ObjectId> for OrgKdeKwinBlur

source§

impl Borrow<ObjectId> for OrgKdeKwinBlurManager

source§

impl Borrow<ObjectId> for OrgKdeKwinContrast

source§

impl Borrow<ObjectId> for OrgKdeKwinContrastManager

source§

impl Borrow<ObjectId> for OrgKdeKwinDpms

source§

impl Borrow<ObjectId> for OrgKdeKwinDpmsManager

source§

impl Borrow<ObjectId> for OrgKdeKwinFakeInput

source§

impl Borrow<ObjectId> for OrgKdeKwinIdle

source§

impl Borrow<ObjectId> for OrgKdeKwinIdleTimeout

source§

impl Borrow<ObjectId> for OrgKdeKwinKeystate

source§

impl Borrow<ObjectId> for OrgKdeKwinOutputdevice

source§

impl Borrow<ObjectId> for KdeOutputDeviceModeV2

source§

impl Borrow<ObjectId> for KdeOutputDeviceV2

source§

impl Borrow<ObjectId> for OrgKdeKwinOutputconfiguration

source§

impl Borrow<ObjectId> for OrgKdeKwinOutputmanagement

source§

impl Borrow<ObjectId> for KdeOutputConfigurationV2

source§

impl Borrow<ObjectId> for KdeOutputManagementV2

source§

impl Borrow<ObjectId> for OrgKdePlasmaShell

source§

impl Borrow<ObjectId> for OrgKdePlasmaSurface

source§

impl Borrow<ObjectId> for OrgKdePlasmaVirtualDesktop

source§

impl Borrow<ObjectId> for OrgKdePlasmaVirtualDesktopManagement

source§

impl Borrow<ObjectId> for OrgKdePlasmaActivation

source§

impl Borrow<ObjectId> for OrgKdePlasmaActivationFeedback

source§

impl Borrow<ObjectId> for OrgKdePlasmaWindow

source§

impl Borrow<ObjectId> for OrgKdePlasmaWindowManagement

source§

impl Borrow<ObjectId> for KdePrimaryOutputV1

source§

impl Borrow<ObjectId> for OrgKdeKwinRemoteAccessManager

source§

impl Borrow<ObjectId> for OrgKdeKwinRemoteBuffer

source§

impl Borrow<ObjectId> for ZkdeScreencastStreamUnstableV1

source§

impl Borrow<ObjectId> for ZkdeScreencastUnstableV1

source§

impl Borrow<ObjectId> for OrgKdeKwinServerDecoration

source§

impl Borrow<ObjectId> for OrgKdeKwinServerDecorationManager

source§

impl Borrow<ObjectId> for OrgKdeKwinServerDecorationPalette

source§

impl Borrow<ObjectId> for OrgKdeKwinServerDecorationPaletteManager

source§

impl Borrow<ObjectId> for OrgKdeKwinShadow

source§

impl Borrow<ObjectId> for OrgKdeKwinShadowManager

source§

impl Borrow<ObjectId> for OrgKdeKwinSlide

source§

impl Borrow<ObjectId> for OrgKdeKwinSlideManager

source§

impl Borrow<ObjectId> for WlTextInput

source§

impl Borrow<ObjectId> for WlTextInputManager

source§

impl Borrow<ObjectId> for ZwpTextInputManagerV2

source§

impl Borrow<ObjectId> for ZwpTextInputV2

source§

impl Borrow<ObjectId> for WlEglstreamController

source§

impl Borrow<ObjectId> for ZwlrDataControlDeviceV1

source§

impl Borrow<ObjectId> for ZwlrDataControlManagerV1

source§

impl Borrow<ObjectId> for ZwlrDataControlOfferV1

source§

impl Borrow<ObjectId> for ZwlrDataControlSourceV1

source§

impl Borrow<ObjectId> for ZwlrExportDmabufFrameV1

source§

impl Borrow<ObjectId> for ZwlrExportDmabufManagerV1

source§

impl Borrow<ObjectId> for ZwlrForeignToplevelHandleV1

source§

impl Borrow<ObjectId> for ZwlrForeignToplevelManagerV1

source§

impl Borrow<ObjectId> for ZwlrGammaControlManagerV1

source§

impl Borrow<ObjectId> for ZwlrGammaControlV1

source§

impl Borrow<ObjectId> for ZwlrInputInhibitManagerV1

source§

impl Borrow<ObjectId> for ZwlrInputInhibitorV1

source§

impl Borrow<ObjectId> for ZwlrLayerShellV1

source§

impl Borrow<ObjectId> for ZwlrLayerSurfaceV1

source§

impl Borrow<ObjectId> for ZwlrOutputConfigurationHeadV1

source§

impl Borrow<ObjectId> for ZwlrOutputConfigurationV1

source§

impl Borrow<ObjectId> for ZwlrOutputHeadV1

source§

impl Borrow<ObjectId> for ZwlrOutputManagerV1

source§

impl Borrow<ObjectId> for ZwlrOutputModeV1

source§

impl Borrow<ObjectId> for ZwlrOutputPowerManagerV1

source§

impl Borrow<ObjectId> for ZwlrOutputPowerV1

source§

impl Borrow<ObjectId> for ZwlrScreencopyFrameV1

source§

impl Borrow<ObjectId> for ZwlrScreencopyManagerV1

source§

impl Borrow<ObjectId> for ZwlrVirtualPointerManagerV1

source§

impl Borrow<ObjectId> for ZwlrVirtualPointerV1

source§

impl Borrow<Texture> for SurfaceTexture

source§

impl Borrow<Uuid> for Braced

source§

impl Borrow<Uuid> for Hyphenated

source§

impl Borrow<Uuid> for Simple

source§

impl Borrow<Uuid> for Urn

1.3.0 · source§

impl Borrow<CStr> for CString

source§

impl Borrow<OsStr> for OsString

source§

impl Borrow<Path> for PathBuf

source§

impl<'a, B> Borrow<B> for Cow<'a, B>
where B: ToOwned + ?Sized,

source§

impl<'a, T> Borrow<T> for CowArc<'a, T>
where T: ?Sized,

source§

impl<'k> Borrow<str> for Key<'k>

source§

impl<'s, T> Borrow<[T]> for SliceVec<'s, T>

source§

impl<A> Borrow<TextureSelector> for Texture<A>
where A: HalApi,

source§

impl<A> Borrow<[<A as Array>::Item]> for TinyVec<A>
where A: Array,

source§

impl<A> Borrow<[<A as Array>::Item]> for tinyvec::arrayvec::ArrayVec<A>
where A: Array,

source§

impl<A> Borrow<[<A as Array>::Item]> for SmallVec<A>
where A: Array,

source§

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

source§

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

source§

impl<T> Borrow<T> for NoIoDrop<T>

source§

impl<T> Borrow<T> for AsyncAsSync<'_, '_, T>

source§

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

source§

impl<T, A> Borrow<[T]> for Vec<T, A>
where A: Allocator,

source§

impl<T, A> Borrow<T> for allocator_api2::stable::boxed::Box<T, A>
where A: Allocator, T: ?Sized,

1.1.0 · source§

impl<T, A> Borrow<T> for bevy::utils::smallvec::alloc::boxed::Box<T, A>
where A: Allocator, T: ?Sized,

source§

impl<T, A> Borrow<T> for Rc<T, A>
where A: Allocator, T: ?Sized,

source§

impl<T, A> Borrow<T> for Arc<T, A>
where A: Allocator, T: ?Sized,

source§

impl<T, const CAP: usize> Borrow<[T]> for arrayvec::arrayvec::ArrayVec<T, CAP>

1.4.0 · source§

impl<T, const N: usize> Borrow<[T]> for [T; N]

source§

impl<const CAP: usize> Borrow<str> for ArrayString<CAP>