pub trait AsRef<T> where
T: ?Sized, {
fn as_ref(&self) -> &Tⓘ;
}
Used to do a cheap reference-to-reference conversion.
This trait is similar to AsMut
which is used for converting between mutable references.
If you need to do a costly conversion it is better to implement From
with type
&T
or write a custom function.
AsRef
has the same signature as Borrow
, but Borrow
is different in few aspects:
- Unlike
AsRef
, Borrow
has a blanket impl for any T
, and can be used to accept either
a reference or a value.
Borrow
also requires that Hash
, Eq
and Ord
for borrowed value are
equivalent to those of the owned value. For this reason, if you want to
borrow only a single field of a struct you can implement AsRef
, but not Borrow
.
Note: This trait must not fail. If the conversion can fail, use a
dedicated method which returns an Option<T>
or a Result<T, E>
.
AsRef
auto-dereferences if the inner type is a reference or a mutable
reference (e.g.: foo.as_ref()
will work the same if foo
has type
&mut Foo
or &&mut Foo
)
By using trait bounds we can accept arguments of different types as long as they can be
converted to the specified type T
.
For example: By creating a generic function that takes an AsRef<str>
we express that we
want to accept all references that can be converted to [&str
] as an argument.
Since both String
and [&str
] implement AsRef<str>
we can accept both as input argument.
fn is_hello<T: AsRef<str>>(s: T) {
assert_eq!("hello", s.as_ref());
}
let s = "hello";
is_hello(s);
let s = "hello".to_string();
is_hello(s);
impl AsRef<Path> for OsStr
[src][−]
impl AsRef<Path> for str
[src][−]
impl<'_> AsRef<OsStr> for Components<'_>
[src][−]
impl AsRef<OsStr> for str
[src][−]
impl AsRef<Path> for Path
[src][−]
impl<'_> AsRef<OsStr> for Component<'_>
[src][−]
impl AsRef<OsStr> for OsStr
[src][−]
impl<'_> AsRef<Path> for Component<'_>
[src][−]
impl AsRef<OsStr> for PathBuf
[src][−]
impl AsRef<CStr> for CStr
[src][−]
impl<'_> AsRef<Path> for Iter<'_>
[src][−]
impl AsRef<OsStr> for Path
[src][−]
impl AsRef<OsStr> for OsString
[src][−]
impl<'_> AsRef<Path> for Components<'_>
[src][−]
impl AsRef<Path> for OsString
[src][−]
impl AsRef<Path> for PathBuf
[src][−]
impl AsRef<CStr> for CString
[src][−]
impl<'_> AsRef<OsStr> for Iter<'_>
[src][−]
impl<'_, T, U> AsRef<U> for &'_ T where
T: AsRef<U> + ?Sized,
U: ?Sized,
[src][−]
impl AsRef<[u8]> for str
[src][−]
impl<T> AsRef<[T]> for [T]
[src][−]
impl<'_, T, U> AsRef<U> for &'_ mut T where
T: AsRef<U> + ?Sized,
U: ?Sized,
[src][−]
impl AsRef<str> for str
[src][−]
impl<T, const N: usize> AsRef<[T]> for [T; N]
[src][−]
impl<T> AsRef<T> for Rc<T> where
T: ?Sized,
[src][−]
impl<T> AsRef<T> for Arc<T> where
T: ?Sized,
[src][−]
impl AsRef<[u8]> for String
[src][+]
impl AsRef<str> for String
[src][+]
impl AsRef<OsStr> for String
[src][+]
impl AsRef<Path> for String
[src][+]
impl<'_> AsRef<Path> for Cow<'_, OsStr>
[src][+]
impl<'_, T> AsRef<[T]> for nom::lib::std::slice::Iter<'_, T>
[src][+]
impl<'_, T> AsRef<T> for Cow<'_, T> where
T: ToOwned + ?Sized,
[src][+]
impl<'a, T> AsRef<[T]> for Drain<'a, T>
[src][+]
impl<T> AsRef<[T]> for Vec<T>
[src][+]
impl<T> AsRef<[T]> for IntoIter<T>
[src][+]
impl<T> AsRef<Vec<T>> for Vec<T>
[src][+]
impl<T> AsRef<T> for Box<T> where
T: ?Sized,
[src][+]
impl<A> AsRef<str> for ArrayString<A> where A: Array<Item = u8> + Copy,
| |
impl<A: Array> AsRef<[<A as Array>::Item]> for ArrayVec<A>
| |
impl<'a> AsRef<Ui> for UiCell<'a>
| |
impl<V> AsRef<Triangle<V>> for Triangle<V> where V: Vertex,
| |
impl<T> AsRef<T> for Owned<T>
| |
impl<L, R, Target> AsRef<Target> for Either<L, R> where L: AsRef<Target>, R: AsRef<Target>,
| |
impl<L, R> AsRef<str> for Either<L, R> where L: AsRef<str>, R: AsRef<str>,
| |
impl<L, R> AsRef<Path> for Either<L, R> where L: AsRef<Path>, R: AsRef<Path>,
| |
impl<L, R> AsRef<OsStr> for Either<L, R> where L: AsRef<OsStr>, R: AsRef<OsStr>,
| |
impl<L, R> AsRef<CStr> for Either<L, R> where L: AsRef<CStr>, R: AsRef<CStr>,
| |
impl<L, R, Target> AsRef<[Target]> for Either<L, R> where L: AsRef<[Target]>, R: AsRef<[Target]>,
| |
impl<T: AsRef<[u8]>> AsRef<[u8]> for Window<T>
| |
impl AsRef<[u8]> for Mmap
| |
impl AsRef<[u8]> for MmapMut
| |
impl AsRef<str> for Signal
| |
impl AsRef<sigset_t> for SigSet
| |
impl AsRef<timespec> for TimeSpec
| |
impl AsRef<timeval> for TimeVal
| |
impl<T: Float> AsRef<T> for OrderedFloat<T>
| |
impl<T: Float> AsRef<T> for NotNan<T>
| |
impl<A: Array> AsRef<[<A as Array>::Item]> for SmallVec<A>
| |
impl AsRef<str> for Field
| |
impl<P> AsRef<UnsafeCommandBuffer<P>> for SyncCommandBuffer<P>
| |
impl AsRef<DeviceMemory> for MappedDeviceMemory
| |
impl AsRef<Proxy<WlDisplay>> for WlDisplay
| |
impl AsRef<Proxy<WlRegistry>> for WlRegistry
| |
impl AsRef<Proxy<WlCallback>> for WlCallback
| |
impl AsRef<Proxy<WlCompositor>> for WlCompositor
| |
impl AsRef<Proxy<WlShmPool>> for WlShmPool
| |
impl AsRef<Proxy<WlShm>> for WlShm
| |
impl AsRef<Proxy<WlBuffer>> for WlBuffer
| |
impl AsRef<Proxy<WlDataOffer>> for WlDataOffer
| |
impl AsRef<Proxy<WlDataSource>> for WlDataSource
| |
impl AsRef<Proxy<WlDataDevice>> for WlDataDevice
| |
impl AsRef<Proxy<WlDataDeviceManager>> for WlDataDeviceManager
| |
impl AsRef<Proxy<WlShell>> for WlShell
| |
impl AsRef<Proxy<WlShellSurface>> for WlShellSurface
| |
impl AsRef<Proxy<WlSurface>> for WlSurface
| |
impl AsRef<Proxy<WlSeat>> for WlSeat
| |
impl AsRef<Proxy<WlPointer>> for WlPointer
| |
impl AsRef<Proxy<WlKeyboard>> for WlKeyboard
| |
impl AsRef<Proxy<WlTouch>> for WlTouch
| |
impl AsRef<Proxy<WlOutput>> for WlOutput
| |
impl AsRef<Proxy<WlRegion>> for WlRegion
| |
impl AsRef<Proxy<WlSubcompositor>> for WlSubcompositor
| |
impl AsRef<Proxy<WlSubsurface>> for WlSubsurface
| |
impl AsRef<Proxy<AnonymousObject>> for AnonymousObject
| |
impl AsRef<Proxy<ZwpFullscreenShellV1>> for ZwpFullscreenShellV1
| |
impl AsRef<Proxy<ZwpFullscreenShellModeFeedbackV1>> for ZwpFullscreenShellModeFeedbackV1
| |
impl AsRef<Proxy<ZwpIdleInhibitManagerV1>> for ZwpIdleInhibitManagerV1
| |
impl AsRef<Proxy<ZwpIdleInhibitorV1>> for ZwpIdleInhibitorV1
| |
impl AsRef<Proxy<ZwpInputMethodContextV1>> for ZwpInputMethodContextV1
| |
impl AsRef<Proxy<ZwpInputMethodV1>> for ZwpInputMethodV1
| |
impl AsRef<Proxy<ZwpInputPanelV1>> for ZwpInputPanelV1
| |
impl AsRef<Proxy<ZwpInputPanelSurfaceV1>> for ZwpInputPanelSurfaceV1
| |
impl AsRef<Proxy<ZwpInputTimestampsManagerV1>> for ZwpInputTimestampsManagerV1
| |
impl AsRef<Proxy<ZwpInputTimestampsV1>> for ZwpInputTimestampsV1
| |
impl AsRef<Proxy<ZwpKeyboardShortcutsInhibitManagerV1>> for ZwpKeyboardShortcutsInhibitManagerV1
| |
impl AsRef<Proxy<ZwpKeyboardShortcutsInhibitorV1>> for ZwpKeyboardShortcutsInhibitorV1
| |
impl AsRef<Proxy<ZwpLinuxDmabufV1>> for ZwpLinuxDmabufV1
| |
impl AsRef<Proxy<ZwpLinuxBufferParamsV1>> for ZwpLinuxBufferParamsV1
| |
impl AsRef<Proxy<ZwpLinuxExplicitSynchronizationV1>> for ZwpLinuxExplicitSynchronizationV1
| |
impl AsRef<Proxy<ZwpLinuxSurfaceSynchronizationV1>> for ZwpLinuxSurfaceSynchronizationV1
| |
impl AsRef<Proxy<ZwpLinuxBufferReleaseV1>> for ZwpLinuxBufferReleaseV1
| |
impl AsRef<Proxy<ZwpPointerConstraintsV1>> for ZwpPointerConstraintsV1
| |
impl AsRef<Proxy<ZwpLockedPointerV1>> for ZwpLockedPointerV1
| |
impl AsRef<Proxy<ZwpConfinedPointerV1>> for ZwpConfinedPointerV1
| |
impl AsRef<Proxy<ZwpPointerGesturesV1>> for ZwpPointerGesturesV1
| |
impl AsRef<Proxy<ZwpPointerGestureSwipeV1>> for ZwpPointerGestureSwipeV1
| |
impl AsRef<Proxy<ZwpPointerGesturePinchV1>> for ZwpPointerGesturePinchV1
| |
impl AsRef<Proxy<ZwpPrimarySelectionDeviceManagerV1>> for ZwpPrimarySelectionDeviceManagerV1
| |
impl AsRef<Proxy<ZwpPrimarySelectionDeviceV1>> for ZwpPrimarySelectionDeviceV1
| |
impl AsRef<Proxy<ZwpPrimarySelectionOfferV1>> for ZwpPrimarySelectionOfferV1
| |
impl AsRef<Proxy<ZwpPrimarySelectionSourceV1>> for ZwpPrimarySelectionSourceV1
| |
impl AsRef<Proxy<ZwpRelativePointerManagerV1>> for ZwpRelativePointerManagerV1
| |
impl AsRef<Proxy<ZwpRelativePointerV1>> for ZwpRelativePointerV1
| |
impl AsRef<Proxy<ZwpTabletManagerV1>> for ZwpTabletManagerV1
| |
impl AsRef<Proxy<ZwpTabletSeatV1>> for ZwpTabletSeatV1
| |
impl AsRef<Proxy<ZwpTabletToolV1>> for ZwpTabletToolV1
| |
impl AsRef<Proxy<ZwpTabletV1>> for ZwpTabletV1
| |
impl AsRef<Proxy<ZwpTabletManagerV2>> for ZwpTabletManagerV2
| |
impl AsRef<Proxy<ZwpTabletSeatV2>> for ZwpTabletSeatV2
| |
impl AsRef<Proxy<ZwpTabletToolV2>> for ZwpTabletToolV2
| |
impl AsRef<Proxy<ZwpTabletV2>> for ZwpTabletV2
| |
impl AsRef<Proxy<ZwpTabletPadRingV2>> for ZwpTabletPadRingV2
| |
impl AsRef<Proxy<ZwpTabletPadStripV2>> for ZwpTabletPadStripV2
| |
impl AsRef<Proxy<ZwpTabletPadGroupV2>> for ZwpTabletPadGroupV2
| |
impl AsRef<Proxy<ZwpTabletPadV2>> for ZwpTabletPadV2
| |
impl AsRef<Proxy<ZwpTextInputV1>> for ZwpTextInputV1
| |
impl AsRef<Proxy<ZwpTextInputManagerV1>> for ZwpTextInputManagerV1
| |
impl AsRef<Proxy<ZwpTextInputV3>> for ZwpTextInputV3
| |
impl AsRef<Proxy<ZwpTextInputManagerV3>> for ZwpTextInputManagerV3
| |
impl AsRef<Proxy<ZxdgDecorationManagerV1>> for ZxdgDecorationManagerV1
| |
impl AsRef<Proxy<ZxdgToplevelDecorationV1>> for ZxdgToplevelDecorationV1
| |
impl AsRef<Proxy<ZxdgExporterV1>> for ZxdgExporterV1
| |
impl AsRef<Proxy<ZxdgImporterV1>> for ZxdgImporterV1
| |
impl AsRef<Proxy<ZxdgExportedV1>> for ZxdgExportedV1
| |
impl AsRef<Proxy<ZxdgImportedV1>> for ZxdgImportedV1
| |
impl AsRef<Proxy<ZxdgExporterV2>> for ZxdgExporterV2
| |
impl AsRef<Proxy<ZxdgImporterV2>> for ZxdgImporterV2
| |
impl AsRef<Proxy<ZxdgExportedV2>> for ZxdgExportedV2
| |
impl AsRef<Proxy<ZxdgImportedV2>> for ZxdgImportedV2
| |
impl AsRef<Proxy<ZxdgOutputManagerV1>> for ZxdgOutputManagerV1
| |
impl AsRef<Proxy<ZxdgOutputV1>> for ZxdgOutputV1
| |
impl AsRef<Proxy<XdgShell>> for XdgShell
| |
impl AsRef<Proxy<XdgSurface>> for XdgSurface
| |
impl AsRef<Proxy<XdgPopup>> for XdgPopup
| |
impl AsRef<Proxy<ZxdgShellV6>> for ZxdgShellV6
| |
impl AsRef<Proxy<ZxdgPositionerV6>> for ZxdgPositionerV6
| |
impl AsRef<Proxy<ZxdgSurfaceV6>> for ZxdgSurfaceV6
| |
impl AsRef<Proxy<ZxdgToplevelV6>> for ZxdgToplevelV6
| |
impl AsRef<Proxy<ZxdgPopupV6>> for ZxdgPopupV6
| |
impl AsRef<Proxy<ZwpXwaylandKeyboardGrabManagerV1>> for ZwpXwaylandKeyboardGrabManagerV1
| |
impl AsRef<Proxy<ZwpXwaylandKeyboardGrabV1>> for ZwpXwaylandKeyboardGrabV1
| |
impl AsRef<Proxy<GtkPrimarySelectionDeviceManager>> for GtkPrimarySelectionDeviceManager
| |
impl AsRef<Proxy<GtkPrimarySelectionDevice>> for GtkPrimarySelectionDevice
| |
impl AsRef<Proxy<GtkPrimarySelectionOffer>> for GtkPrimarySelectionOffer
| |
impl AsRef<Proxy<GtkPrimarySelectionSource>> for GtkPrimarySelectionSource
| |
impl AsRef<Proxy<ZwlrDataControlManagerV1>> for ZwlrDataControlManagerV1
| |
impl AsRef<Proxy<ZwlrDataControlDeviceV1>> for ZwlrDataControlDeviceV1
| |
impl AsRef<Proxy<ZwlrDataControlSourceV1>> for ZwlrDataControlSourceV1
| |
impl AsRef<Proxy<ZwlrDataControlOfferV1>> for ZwlrDataControlOfferV1
| |
impl AsRef<Proxy<ZwlrExportDmabufManagerV1>> for ZwlrExportDmabufManagerV1
| |
impl AsRef<Proxy<ZwlrExportDmabufFrameV1>> for ZwlrExportDmabufFrameV1
| |
impl AsRef<Proxy<ZwlrForeignToplevelManagerV1>> for ZwlrForeignToplevelManagerV1
| |
impl AsRef<Proxy<ZwlrForeignToplevelHandleV1>> for ZwlrForeignToplevelHandleV1
| |
impl AsRef<Proxy<ZwlrGammaControlManagerV1>> for ZwlrGammaControlManagerV1
| |
impl AsRef<Proxy<ZwlrGammaControlV1>> for ZwlrGammaControlV1
| |
impl AsRef<Proxy<ZwlrInputInhibitManagerV1>> for ZwlrInputInhibitManagerV1
| |
impl AsRef<Proxy<ZwlrInputInhibitorV1>> for ZwlrInputInhibitorV1
| |
impl AsRef<Proxy<ZwlrLayerShellV1>> for ZwlrLayerShellV1
| |
impl AsRef<Proxy<ZwlrLayerSurfaceV1>> for ZwlrLayerSurfaceV1
| |
impl AsRef<Proxy<ZwlrScreencopyManagerV1>> for ZwlrScreencopyManagerV1
| |
impl AsRef<Proxy<ZwlrScreencopyFrameV1>> for ZwlrScreencopyFrameV1
| |
impl AsRef<Proxy<WpPresentation>> for WpPresentation
| |
impl AsRef<Proxy<WpPresentationFeedback>> for WpPresentationFeedback
| |
impl AsRef<Proxy<XdgWmBase>> for XdgWmBase
| |
impl AsRef<Proxy<XdgPositioner>> for XdgPositioner
| |
impl AsRef<Proxy<XdgSurface>> for XdgSurface
| |
impl AsRef<Proxy<XdgToplevel>> for XdgToplevel
| |
impl AsRef<Proxy<XdgPopup>> for XdgPopup
| |
impl AsRef<Proxy<WpViewporter>> for WpViewporter
| |
impl AsRef<Proxy<WpViewport>> for WpViewport
| |
impl AsRef<XAnyEvent> for XEvent
| |
impl AsRef<XButtonEvent> for XEvent
| |
impl AsRef<XCirculateEvent> for XEvent
| |
impl AsRef<XCirculateRequestEvent> for XEvent
| |
impl AsRef<XClientMessageEvent> for XEvent
| |
impl AsRef<XColormapEvent> for XEvent
| |
impl AsRef<XConfigureEvent> for XEvent
| |
impl AsRef<XConfigureRequestEvent> for XEvent
| |
impl AsRef<XCreateWindowEvent> for XEvent
| |
impl AsRef<XCrossingEvent> for XEvent
| |
impl AsRef<XDestroyWindowEvent> for XEvent
| |
impl AsRef<XErrorEvent> for XEvent
| |
impl AsRef<XExposeEvent> for XEvent
| |
impl AsRef<XFocusChangeEvent> for XEvent
| |
impl AsRef<XGenericEventCookie> for XEvent
| |
impl AsRef<XGraphicsExposeEvent> for XEvent
| |
impl AsRef<XGravityEvent> for XEvent
| |
impl AsRef<XKeyEvent> for XEvent
| |
impl AsRef<XKeymapEvent> for XEvent
| |
impl AsRef<XMapEvent> for XEvent
| |
impl AsRef<XMappingEvent> for XEvent
| |
impl AsRef<XMapRequestEvent> for XEvent
| |
impl AsRef<XMotionEvent> for XEvent
| |
impl AsRef<XNoExposeEvent> for XEvent
| |
impl AsRef<XPropertyEvent> for XEvent
| |
impl AsRef<XReparentEvent> for XEvent
| |
impl AsRef<XResizeRequestEvent> for XEvent
| |
impl AsRef<XSelectionClearEvent> for XEvent
| |
impl AsRef<XSelectionEvent> for XEvent
| |
impl AsRef<XSelectionRequestEvent> for XEvent
| |
impl AsRef<XUnmapEvent> for XEvent
| |
impl AsRef<XVisibilityEvent> for XEvent
| |
impl AsRef<[i8]> for ClientMessageData
| |
impl AsRef<[u8]> for ClientMessageData
| |
impl AsRef<[i16]> for ClientMessageData
| |
impl AsRef<[u16]> for ClientMessageData
| |
impl AsRef<[i64]> for ClientMessageData
| |
impl AsRef<[u64]> for ClientMessageData
| |
impl AsRef<XF86VidModeNotifyEvent> for XEvent
| |
impl AsRef<XRRScreenChangeNotifyEvent> for XEvent
| |
impl AsRef<XRRNotifyEvent> for XEvent
| |
impl AsRef<XRROutputChangeNotifyEvent> for XEvent
| |
impl AsRef<XRRCrtcChangeNotifyEvent> for XEvent
| |
impl AsRef<XRROutputPropertyNotifyEvent> for XEvent
| |
impl AsRef<XRRProviderChangeNotifyEvent> for XEvent
| |
impl AsRef<XRRProviderPropertyNotifyEvent> for XEvent
| |
impl AsRef<XRRResourceChangeNotifyEvent> for XEvent
| |
impl AsRef<XScreenSaverNotifyEvent> for XEvent
| |
impl AsRef<XAnyEvent> for XEvent
| |
impl AsRef<XButtonEvent> for XEvent
| |
impl AsRef<XCirculateEvent> for XEvent
| |
impl AsRef<XCirculateRequestEvent> for XEvent
| |
impl AsRef<XClientMessageEvent> for XEvent
| |
impl AsRef<XColormapEvent> for XEvent
| |
impl AsRef<XConfigureEvent> for XEvent
| |
impl AsRef<XConfigureRequestEvent> for XEvent
| |
impl AsRef<XCreateWindowEvent> for XEvent
| |
impl AsRef<XCrossingEvent> for XEvent
| |
impl AsRef<XDestroyWindowEvent> for XEvent
| |
impl AsRef<XErrorEvent> for XEvent
| |
impl AsRef<XExposeEvent> for XEvent
| |
impl AsRef<XFocusChangeEvent> for XEvent
| |
impl AsRef<XGenericEventCookie> for XEvent
| |
impl AsRef<XGraphicsExposeEvent> for XEvent
| |
impl AsRef<XGravityEvent> for XEvent
| |
impl AsRef<XKeyEvent> for XEvent
| |
impl AsRef<XKeymapEvent> for XEvent
| |
impl AsRef<XMapEvent> for XEvent
| |
impl AsRef<XMappingEvent> for XEvent
| |
impl AsRef<XMapRequestEvent> for XEvent
| |
impl AsRef<XMotionEvent> for XEvent
| |
impl AsRef<XNoExposeEvent> for XEvent
| |
impl AsRef<XPropertyEvent> for XEvent
| |
impl AsRef<XReparentEvent> for XEvent
| |
impl AsRef<XResizeRequestEvent> for XEvent
| |
impl AsRef<XSelectionClearEvent> for XEvent
| |
impl AsRef<XSelectionEvent> for XEvent
| |
impl AsRef<XSelectionRequestEvent> for XEvent
| |
impl AsRef<XUnmapEvent> for XEvent
| |
impl AsRef<XVisibilityEvent> for XEvent
| |
impl AsRef<[i8]> for ClientMessageData
| |
impl AsRef<[u8]> for ClientMessageData
| |
impl AsRef<[i16]> for ClientMessageData
| |
impl AsRef<[u16]> for ClientMessageData
| |
impl AsRef<[i64]> for ClientMessageData
| |
impl AsRef<[u64]> for ClientMessageData
| |
impl AsRef<XF86VidModeNotifyEvent> for XEvent
| |
impl AsRef<XScreenSaverNotifyEvent> for XEvent
| |
impl AsRef<XRRScreenChangeNotifyEvent> for XEvent
| |
impl AsRef<XRRNotifyEvent> for XEvent
| |
impl AsRef<XRROutputChangeNotifyEvent> for XEvent
| |
impl AsRef<XRRCrtcChangeNotifyEvent> for XEvent
| |
impl AsRef<XRROutputPropertyNotifyEvent> for XEvent
| |
impl AsRef<XRRProviderChangeNotifyEvent> for XEvent
| |
impl AsRef<XRRProviderPropertyNotifyEvent> for XEvent
| |
impl AsRef<XRRResourceChangeNotifyEvent> for XEvent
| |