1#![stable(feature = "rust1", since = "1.0.0")]
88
89use crate::{fmt, hash, intrinsics};
90
91#[stable(feature = "rust1", since = "1.0.0")]
112#[rustc_diagnostic_item = "Any"]
113pub trait Any: 'static {
114 #[stable(feature = "get_type_id", since = "1.34.0")]
134 fn type_id(&self) -> TypeId;
135}
136
137#[stable(feature = "rust1", since = "1.0.0")]
138impl<T: 'static + ?Sized> Any for T {
139 fn type_id(&self) -> TypeId {
140 TypeId::of::<T>()
141 }
142}
143
144#[stable(feature = "rust1", since = "1.0.0")]
149impl fmt::Debug for dyn Any {
150 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 f.debug_struct("Any").finish_non_exhaustive()
152 }
153}
154
155#[stable(feature = "rust1", since = "1.0.0")]
159impl fmt::Debug for dyn Any + Send {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 f.debug_struct("Any").finish_non_exhaustive()
162 }
163}
164
165#[stable(feature = "any_send_sync_methods", since = "1.28.0")]
166impl fmt::Debug for dyn Any + Send + Sync {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 f.debug_struct("Any").finish_non_exhaustive()
169 }
170}
171
172impl dyn Any {
173 #[stable(feature = "rust1", since = "1.0.0")]
192 #[inline]
193 pub fn is<T: Any>(&self) -> bool {
194 let t = TypeId::of::<T>();
196
197 let concrete = self.type_id();
199
200 t == concrete
202 }
203
204 #[stable(feature = "rust1", since = "1.0.0")]
224 #[inline]
225 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
226 if self.is::<T>() {
227 unsafe { Some(self.downcast_ref_unchecked()) }
231 } else {
232 None
233 }
234 }
235
236 #[stable(feature = "rust1", since = "1.0.0")]
260 #[inline]
261 pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
262 if self.is::<T>() {
263 unsafe { Some(self.downcast_mut_unchecked()) }
267 } else {
268 None
269 }
270 }
271
272 #[unstable(feature = "downcast_unchecked", issue = "90850")]
293 #[inline]
294 pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
295 debug_assert!(self.is::<T>());
296 unsafe { &*(self as *const dyn Any as *const T) }
298 }
299
300 #[unstable(feature = "downcast_unchecked", issue = "90850")]
323 #[inline]
324 pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
325 debug_assert!(self.is::<T>());
326 unsafe { &mut *(self as *mut dyn Any as *mut T) }
328 }
329}
330
331impl dyn Any + Send {
332 #[stable(feature = "rust1", since = "1.0.0")]
351 #[inline]
352 pub fn is<T: Any>(&self) -> bool {
353 <dyn Any>::is::<T>(self)
354 }
355
356 #[stable(feature = "rust1", since = "1.0.0")]
375 #[inline]
376 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
377 <dyn Any>::downcast_ref::<T>(self)
378 }
379
380 #[stable(feature = "rust1", since = "1.0.0")]
403 #[inline]
404 pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
405 <dyn Any>::downcast_mut::<T>(self)
406 }
407
408 #[unstable(feature = "downcast_unchecked", issue = "90850")]
429 #[inline]
430 pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
431 unsafe { <dyn Any>::downcast_ref_unchecked::<T>(self) }
433 }
434
435 #[unstable(feature = "downcast_unchecked", issue = "90850")]
458 #[inline]
459 pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
460 unsafe { <dyn Any>::downcast_mut_unchecked::<T>(self) }
462 }
463}
464
465impl dyn Any + Send + Sync {
466 #[stable(feature = "any_send_sync_methods", since = "1.28.0")]
485 #[inline]
486 pub fn is<T: Any>(&self) -> bool {
487 <dyn Any>::is::<T>(self)
488 }
489
490 #[stable(feature = "any_send_sync_methods", since = "1.28.0")]
509 #[inline]
510 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
511 <dyn Any>::downcast_ref::<T>(self)
512 }
513
514 #[stable(feature = "any_send_sync_methods", since = "1.28.0")]
537 #[inline]
538 pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
539 <dyn Any>::downcast_mut::<T>(self)
540 }
541
542 #[unstable(feature = "downcast_unchecked", issue = "90850")]
562 #[inline]
563 pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
564 unsafe { <dyn Any>::downcast_ref_unchecked::<T>(self) }
566 }
567
568 #[unstable(feature = "downcast_unchecked", issue = "90850")]
590 #[inline]
591 pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
592 unsafe { <dyn Any>::downcast_mut_unchecked::<T>(self) }
594 }
595}
596
597#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
709#[stable(feature = "rust1", since = "1.0.0")]
710#[lang = "type_id"]
711pub struct TypeId {
712 pub(crate) data: [*const (); 16 / size_of::<*const ()>()],
718}
719
720#[stable(feature = "rust1", since = "1.0.0")]
722unsafe impl Send for TypeId {}
723#[stable(feature = "rust1", since = "1.0.0")]
725unsafe impl Sync for TypeId {}
726
727#[stable(feature = "rust1", since = "1.0.0")]
728#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
729impl const PartialEq for TypeId {
730 #[inline]
731 fn eq(&self, other: &Self) -> bool {
732 #[cfg(miri)]
733 return crate::intrinsics::type_id_eq(*self, *other);
734 #[cfg(not(miri))]
735 {
736 let this = self;
737 crate::intrinsics::const_eval_select!(
738 @capture { this: &TypeId, other: &TypeId } -> bool:
739 if const {
740 crate::intrinsics::type_id_eq(*this, *other)
741 } else {
742 unsafe {
751 crate::mem::transmute::<_, u128>(*this) == crate::mem::transmute::<_, u128>(*other)
752 }
753 }
754 )
755 }
756 }
757}
758
759impl TypeId {
760 #[must_use]
775 #[stable(feature = "rust1", since = "1.0.0")]
776 #[rustc_const_stable(feature = "const_type_id", since = "CURRENT_RUSTC_VERSION")]
777 pub const fn of<T: ?Sized + 'static>() -> TypeId {
778 const { intrinsics::type_id::<T>() }
779 }
780
781 fn as_u128(self) -> u128 {
782 let mut bytes = [0; 16];
783
784 for (i, chunk) in self.data.iter().copied().enumerate() {
786 let chunk = chunk.addr().to_ne_bytes();
787 let start = i * chunk.len();
788 bytes[start..(start + chunk.len())].copy_from_slice(&chunk);
789 }
790 u128::from_ne_bytes(bytes)
791 }
792}
793
794#[stable(feature = "rust1", since = "1.0.0")]
795impl hash::Hash for TypeId {
796 #[inline]
797 fn hash<H: hash::Hasher>(&self, state: &mut H) {
798 let data =
811 unsafe { crate::ptr::read_unaligned(self.data.as_ptr().cast::<u64>().offset(1)) };
815 data.hash(state);
816 }
817}
818
819#[stable(feature = "rust1", since = "1.0.0")]
820impl fmt::Debug for TypeId {
821 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
822 write!(f, "TypeId({:#034x})", self.as_u128())
823 }
824}
825
826#[must_use]
854#[stable(feature = "type_name", since = "1.38.0")]
855#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
856pub const fn type_name<T: ?Sized>() -> &'static str {
857 const { intrinsics::type_name::<T>() }
858}
859
860#[must_use]
894#[stable(feature = "type_name_of_val", since = "1.76.0")]
895#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
896pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
897 type_name::<T>()
898}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4