/src/test/codegen-units/overloaded-operators.rs

https://gitlab.com/0072016/0072016-rusty · Rust · 72 lines · 39 code · 15 blank · 18 comment · 8 complexity · 208951c88c52635f578704ca28334f89 MD5 · raw file

  1. // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. // ignore-tidy-linelength
  11. // compile-flags:-Zprint-trans-items=eager
  12. #![deny(dead_code)]
  13. #![crate_type="lib"]
  14. use std::ops::{Index, IndexMut, Add, Deref};
  15. pub struct Indexable {
  16. data: [u8; 3]
  17. }
  18. impl Index<usize> for Indexable {
  19. type Output = u8;
  20. //~ TRANS_ITEM fn overloaded_operators::Indexable.Index<usize>[0]::index[0]
  21. fn index(&self, index: usize) -> &Self::Output {
  22. if index >= 3 {
  23. &self.data[0]
  24. } else {
  25. &self.data[index]
  26. }
  27. }
  28. }
  29. impl IndexMut<usize> for Indexable {
  30. //~ TRANS_ITEM fn overloaded_operators::Indexable.IndexMut<usize>[0]::index_mut[0]
  31. fn index_mut(&mut self, index: usize) -> &mut Self::Output {
  32. if index >= 3 {
  33. &mut self.data[0]
  34. } else {
  35. &mut self.data[index]
  36. }
  37. }
  38. }
  39. //~ TRANS_ITEM fn overloaded_operators::Equatable.::std::cmp::PartialEq[0]::eq[0]
  40. //~ TRANS_ITEM fn overloaded_operators::Equatable.::std::cmp::PartialEq[0]::ne[0]
  41. #[derive(PartialEq)]
  42. pub struct Equatable(u32);
  43. impl Add<u32> for Equatable {
  44. type Output = u32;
  45. //~ TRANS_ITEM fn overloaded_operators::Equatable.Add<u32>[0]::add[0]
  46. fn add(self, rhs: u32) -> u32 {
  47. self.0 + rhs
  48. }
  49. }
  50. impl Deref for Equatable {
  51. type Target = u32;
  52. //~ TRANS_ITEM fn overloaded_operators::Equatable.Deref[0]::deref[0]
  53. fn deref(&self) -> &Self::Target {
  54. &self.0
  55. }
  56. }
  57. //~ TRANS_ITEM drop-glue i8