rustc_builtin_macros/deriving/
ord.rs1use rustc_expand::base::ExtCtxt;
2use rustc_span::{Span, sym};
3use thin_vec::thin_vec;
4
5use crate::deriving::generic::*;
6use crate::deriving::partial_ord::{OrdlikeDerive, cmp_body, discr_data_order};
7use crate::deriving::path_std;
8
9pub(crate) fn expand_derive_ord(
10 cx: &ExtCtxt<'_>,
11 span: Span,
12 item: &ast::Item,
13 push: &mut dyn FnMut(Box<ast::Item>),
14 is_const: bool,
15) {
16 let discr_then_data = discr_data_order(item);
17
18 let trait_def = TraitDef {
19 span,
20 path: cx.std_path(span, { &[sym::cmp, sym::Ord] })path_std!(cx, span, cmp::Ord),
21 needs_copy_as_bound_if_packed: true,
22 additional_bounds: SmallVec::new(),
23 supports_unions: false,
24 methods: {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(MethodDef {
name: sym::cmp,
generics: cx.empty_generics(span),
args: {
let len = [(), ()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.param_self_ref(span));
vec.push(cx.param(span, sym::other, cx.ty_self_ref(span)));
vec
},
ret_ty: cx.ty_path(cx.std_path(span,
{ &[sym::cmp, sym::Ordering] })),
attributes: {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.generated_attr(span,
ast::GeneratedAttr::Inline));
vec
},
body: MethodBody::SubstructureFunc {
body_func: Box::new(|cx, span, substr|
{
let expr =
cmp_body(cx, span, substr, discr_then_data,
OrdlikeDerive::Ord);
BlockOrExpr::new_expr(expr)
}),
strategy: FieldlessVariantsStrategy::Unify,
has_other_selflike_arg: true,
},
});
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[MethodDef {
name: sym::cmp,
generics: cx.empty_generics(span),
args: {
let len = [(), ()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.param_self_ref(span));
vec.push(cx.param(span, sym::other, cx.ty_self_ref(span)));
vec
},
ret_ty: cx.ty_path(cx.std_path(span,
{ &[sym::cmp, sym::Ordering] })),
attributes: {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.generated_attr(span,
ast::GeneratedAttr::Inline));
vec
},
body: MethodBody::SubstructureFunc {
body_func: Box::new(|cx, span, substr|
{
let expr =
cmp_body(cx, span, substr, discr_then_data,
OrdlikeDerive::Ord);
BlockOrExpr::new_expr(expr)
}),
strategy: FieldlessVariantsStrategy::Unify,
has_other_selflike_arg: true,
},
}])))
}
}smallvec![MethodDef {
25 name: sym::cmp,
26 generics: cx.empty_generics(span),
27 args: thin_vec![
28 cx.param_self_ref(span),
29 cx.param(span, sym::other, cx.ty_self_ref(span))
30 ],
31 ret_ty: cx.ty_path(path_std!(cx, span, cmp::Ordering)),
32 attributes: thin_vec![cx.generated_attr(span, ast::GeneratedAttr::Inline)],
33 body: MethodBody::SubstructureFunc {
34 body_func: Box::new(|cx, span, substr| {
35 let expr = cmp_body(cx, span, substr, discr_then_data, OrdlikeDerive::Ord);
43 BlockOrExpr::new_expr(expr)
44 }),
45 strategy: FieldlessVariantsStrategy::Unify,
46 has_other_selflike_arg: true,
47 },
48 }],
49 is_const,
50 ..
51 };
52
53 trait_def.expand(cx, item, push)
54}