// SPDX-License-Identifier: Apache-2.0 OR MIT #![allow( dead_code, unreachable_pub, clippy::items_after_statements, clippy::missing_safety_doc, clippy::needless_pass_by_value, clippy::no_effect_underscore_binding, clippy::undocumented_unsafe_blocks )] use std::{pin::Pin, rc::Rc}; use easy_ext::ext; #[test] fn simple() { #[ext] impl str { fn foo(&self, pat: &str) -> String { self.replace(pat, "_") } } assert_eq!("--".foo("-"), "__"); } #[test] fn params() { #[ext] impl Result { fn err_into(self) -> Result where E: Into, { self.map_err(Into::into) } } let err: Result<(), _> = Err(1_u32); assert_eq!(err.err_into::().unwrap_err(), 1_u64); } #[test] fn lifetime() { #[ext(OptionExt)] impl<'a, T> &'a mut Option { fn into_ref(self) -> Option<&'a T> { self.as_ref() } } let _: Option<&u8> = Some(1).into_ref(); } mod bar { use easy_ext::ext; // assoc-item-level visibility + named #[ext(E1)] impl str { pub const FOO1: &'static str = "_"; pub fn foo1(&self, pat: &str) -> String { self.replace(pat, Self::FOO1) } } // assoc-item-level visibility + unnamed #[ext] impl str { pub const FOO2: &'static str = "_"; pub fn foo2(&self, pat: &str) -> String { self.replace(pat, Self::FOO2) } } // impl-level visibility + named #[ext(E2)] pub impl str { const FOO3: &'static str = "_"; fn foo3(&self, pat: &str) -> String { self.replace(pat, Self::FOO3) } } // impl-level visibility + unnamed #[ext] pub impl str { const FOO4: &'static str = "_"; fn foo4(&self, pat: &str) -> String { self.replace(pat, Self::FOO4) } } pub(super) mod baz { use easy_ext::ext; #[ext(E4)] impl str { pub(super) fn bar(&self, pat: &str) -> String { self.replace(pat, "_") } } #[ext(E5)] impl str { pub fn baz(&self, pat: &str) -> String { self.replace(pat, "_") } pub fn baz2(&self, pat: &str) -> String { self.replace(pat, "-") } } #[ext(E6)] pub(super) impl str { fn bar2(&self, pat: &str) -> String { self.replace(pat, "_") } } #[ext(E7)] pub(crate) impl str { fn baz3(&self, pat: &str) -> String { self.replace(pat, "_") } fn baz4(&self, pat: &str) -> String { self.replace(pat, "-") } } } } #[test] fn visibility() { use self::bar::{ E1, E2, baz::{E5, E7}, }; assert_eq!("..".foo1("."), "__"); assert_eq!("..".foo3("."), "__"); assert_eq!("..".baz("."), "__"); assert_eq!("..".baz2("."), "--"); assert_eq!("..".baz3("."), "__"); assert_eq!("..".baz4("."), "--"); } #[test] fn generics() { #[ext(IterExt)] impl I { fn next2(self) -> Option { self.into_iter().next() } } assert_eq!(vec![1, 2, 3].next2(), Some(1_u8)); } #[test] fn trait_generics() { #[derive(Debug, PartialEq, Eq)] struct A {} impl Iterator for A { type Item = (); fn next(&mut self) -> Option { None } } #[ext(ConstInit)] impl A { const INIT1: Self = Self {}; const INIT2: A = A {}; } #[ext(Ext1)] impl I { const CONST1: Self = Self::INIT1; const CONST2: I = I::INIT1; type Item2 = Self::Item; type Item3 = I::Item; fn method1(mut self) -> Option { self.next() } fn method2(mut self) -> Option { self.next() } fn method3(mut self) -> Option { self.next() } fn method4(mut self) -> Option<::Item3> { self.next() } } fn a(mut x: T) { let y = T::CONST1; let _ = T::CONST2; assert_eq!(x, y); assert!(x.next().is_none()); } assert_eq!(A {}.method1(), None); assert_eq!(A {}.method2(), None); a(A::INIT1); a(A::INIT2); #[ext(Ext2)] impl I { const CONST3: I = { fn a() {} I::INIT1 }; type Item4 = I::Item; fn method5(self, _: I::Item) -> (Option, ::Item4) { fn a() {} unimplemented!() } } } #[test] fn type_parameter_defaults() { #[ext(Ext)] impl () {} impl Ext for u8 {} // The code above is equivalent to the code below. trait Trait {} impl Trait for () {} impl Trait for u8 {} } // See also ui/maybe.rs #[test] fn maybe() { #[ext] impl T { fn f(&self) {} } #[ext] impl T where T: ?Sized, { fn f(&self) {} } #[ext] impl T { fn f(&self) {} } #[ext] impl T where T: Send + ?Sized + Sync, { fn f(&self) {} } #[ext] impl T where T: Iterator, T: ?Sized, T: Send, { fn f(&self) {} } } #[test] fn inline() { #[ext] impl str { #[inline] fn auto(&self) {} #[inline(always)] fn always(&self) {} #[inline(never)] fn never(&self) {} } } #[test] fn assoc_ty() { #[ext(StrExt)] impl str { type Assoc = String; fn owned(&self) -> Self::Assoc { self.to_owned() } } let s: ::Assoc = "?".owned(); assert_eq!(s, "?"); #[ext(TryIterator)] impl>, T, E> I { type Ok = T; type Error = E; fn try_next(&mut self) -> Result, Self::Error> { self.next().transpose() } } let mut iter = vec![Ok(1), Err(1)].into_iter(); assert_eq!(iter.try_next(), Ok(Some(1))); assert_eq!(iter.try_next(), Err(1)); assert_eq!(iter.try_next(), Ok(None)); } #[allow(clippy::let_underscore_future)] #[test] fn syntax() { // TODO: `unsafe impl` became syntactically invalid in https://github.com/rust-lang/rust/pull/144386#issuecomment-3133213586 #[ext(E1)] impl str { fn normal(&self) {} unsafe fn unsafety(&self) {} extern "C" fn abi1() {} extern "C" fn abi2() {} unsafe extern "C" fn unsafe_abi1() {} unsafe extern "C" fn unsafe_abi2() {} async fn asyncness(&self) {} async unsafe fn unsafe_asyncness(&self) {} } "a".normal(); unsafe { "?".unsafety() } str::abi1(); unsafe { str::unsafe_abi1() } let _ = async { "a".asyncness().await; unsafe { "b".unsafe_asyncness().await } }; struct S {} impl E1 for S { fn normal(&self) {} unsafe fn unsafety(&self) {} extern "C" fn abi1() {} extern "C" fn abi2() {} unsafe extern "C" fn unsafe_abi1() {} unsafe extern "C" fn unsafe_abi2() {} async fn asyncness(&self) {} async unsafe fn unsafe_asyncness(&self) {} } } // test for angle bracket #[test] fn angle_bracket() { #[ext] impl fn() -> () { const FUNC: fn() -> fn() -> fn() -> () = Self::func; type Func = fn() -> fn() -> (); fn func() -> fn() -> fn() -> () { || || {} } } #[ext(E1)] impl T where Self::Assoc3: Sized, T::Assoc3: Sized, Self: E2, T: E2, { const ASSOC1: ::Assoc1 = ::assoc1; type Assoc1 = fn() -> ::Assoc2; type Assoc2 = (); fn assoc1() -> ::Assoc2 where ::Assoc1: Sized, ::Assoc1: Sized, Self::Assoc1: Sized, T::Assoc3: Sized, Self: E2, T: E2, { } } struct A {} #[ext(E2)] impl A { const ASSOC1: ::Assoc3 = ::assoc2; type Assoc3 = fn() -> ::Assoc4; type Assoc4 = (); fn assoc2() -> ::Assoc4 where ::Assoc3: Sized, Self::Assoc3: Sized, { } } #[ext] impl fn() -> T, E> Result where E: FnOnce() -> Result, &'static dyn Fn() -> T: Fn() -> T + 'static, fn() -> fn() -> T: Fn() -> fn() -> T, { fn where_clause fn() -> T, F>(self, _f: F) -> Self where F: FnOnce() -> Result, &'static dyn Fn() -> T: Fn() -> T + 'static, fn() -> fn() -> T: Fn() -> fn() -> T, { unimplemented!() } } } #[test] fn min_const_generics() { struct S1([T; CAP]); #[ext(E1)] impl S1 { const CAPACITY: usize = CAP; fn f() -> S1 { unimplemented!() } } struct S2; impl E1<(), { CAP }> for S2<{ CAP }> { const CAPACITY: usize = CAP; fn f() -> S1<(), { C }> { S1([(); C]) } } let _: [(); 2] = >::f::<2>().0; struct S3(T); #[ext(E2)] impl str { fn method1(&self) -> S1 ()>, 1> { S1([Some(|| {})]) } #[allow(unused_braces)] fn method2(&self) -> S1 ()>, { 1 }> { S1([Some(|| {})]) } fn method3(&self) -> S3 (), 'a'> { S3(|| {}) } #[allow(unused_braces)] fn method4(&self) -> S3 (), { 'a' }> { S3(|| {}) } } } mod const_generics_defaults { // https://github.com/rust-lang/rust/tree/1.80.0/tests/ui/const-generics/defaults use easy_ext::ext; #[ext(Ext)] impl () {} impl Ext for u8 {} // The code above is equivalent to the code below. trait Trait {} impl Trait for () {} impl Trait for u8 {} // https://github.com/rust-lang/rust/blob/1.84.0/tests/ui/const-generics/defaults/const-param-as-default-value.rs #[ext(Ext2)] impl () {} } mod generic_associated_types { // https://github.com/rust-lang/rust/blob/1.84.0/tests/ui/generic-associated-types/collections.rs use easy_ext::ext; trait CollectionFamily { type Member: Collection; } struct VecFamily; impl CollectionFamily for VecFamily { type Member = Vec; } #[ext(Collection)] impl Vec { // TODO: handle where clause in GAT: https://github.com/rust-lang/rust/pull/90076 // type Iter<'iter> = std::slice::Iter<'iter, T> // where // T: 'iter, // Self: 'iter; type Family = VecFamily; type Sibling = <>::Family as CollectionFamily>::Member; fn empty() -> Self { vec![] } fn add(&mut self, value: T) { self.push(value); } // TODO: handle where clause in GAT: https://github.com/rust-lang/rust/pull/90076 // fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> { // self.iter() // } } } mod associated_type_bounds { // https://github.com/rust-lang/rust/blob/1.84.0/tests/ui/associated-type-bounds/fn-where.rs mod fn_where { use easy_ext::ext; use super::fn_aux::*; #[ext(E1)] impl T { fn where_bound(beta: B) -> usize where B: Beta, { desugared_bound(beta) } fn where_bound_region(beta: B) -> usize where B: Beta, { desugared_bound_region(beta) } fn where_bound_multi(beta: B) -> usize where B: Copy + Beta, { desugared_bound_multi(beta) } fn where_bound_region_specific<'a, B>(gamma: &'a B::Gamma) -> usize where B: Beta>, { desugared_bound_region_specific::(gamma) } fn where_bound_region_forall(beta: B) -> usize where B: Beta Epsilon<'a>>, { desugared_bound_region_forall(beta) } fn where_bound_region_forall2(beta: B) -> usize where B: Beta Epsilon<'a, Zeta: Eta>>, { desugared_bound_region_forall2(beta) } fn where_constraint_region_forall(beta: B) -> usize where for<'a> &'a B: Beta, { desugared_constraint_region_forall(beta) } fn where_bound_nested(beta: B) -> usize where B: Beta>, { desugared_bound_nested(beta) } } } // https://github.com/rust-lang/rust/blob/1.84.0/tests/ui/associated-type-bounds/auxiliary/fn-aux.rs mod fn_aux { // Traits: pub trait Alpha { fn alpha(self) -> usize; } pub trait Beta { type Gamma; fn gamma(self) -> Self::Gamma; } pub trait Delta { fn delta(self) -> usize; } pub trait Epsilon<'a> { type Zeta; fn zeta(&'a self) -> Self::Zeta; fn epsilon(&'a self) -> usize; } pub trait Eta { fn eta(self) -> usize; } // Assertions: pub fn assert_alpha(x: T) -> usize { x.alpha() } pub fn assert_static(_: T) -> usize { 24 } pub fn assert_delta(x: T) -> usize { x.delta() } pub fn assert_epsilon_specific<'a, T: 'a + Epsilon<'a>>(x: &'a T) -> usize { x.epsilon() } pub fn assert_epsilon_forall Epsilon<'a>>() {} pub fn assert_forall_epsilon_zeta_satisfies_eta(x: T) -> usize where T: for<'a> Epsilon<'a>, for<'a> >::Zeta: Eta, { x.epsilon() + x.zeta().eta() } // Implementations and types: #[derive(Clone, Copy)] pub struct BetaType; #[derive(Clone, Copy)] pub struct GammaType; #[derive(Clone, Copy)] pub struct ZetaType; impl Beta for BetaType { type Gamma = GammaType; fn gamma(self) -> Self::Gamma { GammaType } } impl Beta for &BetaType { type Gamma = GammaType; fn gamma(self) -> Self::Gamma { GammaType } } impl Beta for GammaType { type Gamma = Self; fn gamma(self) -> Self::Gamma { self } } impl Alpha for GammaType { fn alpha(self) -> usize { 42 } } impl Delta for GammaType { fn delta(self) -> usize { 1337 } } impl<'a> Epsilon<'a> for GammaType { type Zeta = ZetaType; fn zeta(&'a self) -> Self::Zeta { ZetaType } fn epsilon(&'a self) -> usize { 7331 } } impl Eta for ZetaType { fn eta(self) -> usize { 7 } } // Desugared forms to check against: pub fn desugared_bound(beta: B) -> usize where B: Beta, B::Gamma: Alpha, { let gamma: B::Gamma = beta.gamma(); assert_alpha::(gamma) } pub fn desugared_bound_region(beta: B) -> usize where B: Beta, B::Gamma: 'static, { assert_static::(beta.gamma()) } pub fn desugared_bound_multi(beta: B) -> usize where B: Copy + Beta, B::Gamma: Alpha + 'static + Delta, { assert_alpha::(beta.gamma()) + assert_static::(beta.gamma()) + assert_delta::(beta.gamma()) } pub fn desugared_bound_region_specific<'a, B>(gamma: &'a B::Gamma) -> usize where B: Beta, B::Gamma: 'a + Epsilon<'a>, { assert_epsilon_specific::(gamma) } pub fn desugared_bound_region_forall(beta: B) -> usize where B: Beta, B::Gamma: Copy + for<'a> Epsilon<'a>, { assert_epsilon_forall::(); let g1: B::Gamma = beta.gamma(); let g2: B::Gamma = g1; assert_epsilon_specific::(&g1) + assert_epsilon_specific::(&g2) } pub fn desugared_bound_region_forall2(beta: B) -> usize where B: Beta, B::Gamma: Copy + for<'a> Epsilon<'a>, for<'a> >::Zeta: Eta, { let gamma = beta.gamma(); assert_forall_epsilon_zeta_satisfies_eta::(gamma) } pub fn desugared_constraint_region_forall(beta: B) -> usize where for<'a> &'a B: Beta, for<'a> <&'a B as Beta>::Gamma: Alpha, { let g1 = beta.gamma(); let g2 = beta.gamma(); assert_alpha(g1) + assert_alpha(g2) } pub fn desugared_bound_nested(beta: B) -> usize where B: Beta, B::Gamma: Copy + Alpha + Beta, ::Gamma: Delta, { let go = beta.gamma(); let gi = go.gamma(); go.alpha() + gi.delta() } pub fn desugared() { let beta = BetaType; let gamma = beta.gamma(); assert_eq!(42, desugared_bound(beta)); assert_eq!(24, desugared_bound_region(beta)); assert_eq!(42 + 24 + 1337, desugared_bound_multi(beta)); assert_eq!(7331, desugared_bound_region_specific::(&gamma)); assert_eq!(7331 * 2, desugared_bound_region_forall(beta)); assert_eq!(42 + 1337, desugared_bound_nested(beta)); } } } #[test] fn macros() { macro_rules! m { ( $impl:ident $path:path [$($generics:tt)*] $where:ident {$( [$vis:vis, $($fn_sig:ident)*] )*} ) => { $( #[ext] $impl Result { $vis $($fn_sig)* (self) -> Result where E: Into, { unimplemented!() } } )* }; } m!(impl Result [T,E] where { [, fn a] [pub, fn b] [pub, unsafe fn c] [pub(crate), fn d] }); } // https://github.com/taiki-e/easy-ext/issues/36 #[test] fn where_clause() { pub trait Trait {} #[rustfmt::skip] #[ext] pub impl Vec where Self: Trait> { } } #[allow(clippy::needless_pub_self)] // This is intentional pub mod visibility { use easy_ext::ext; pub struct Pub; #[ext] impl str { pub const ASSOC: u8 = 1; pub type Assoc = u8; pub fn assoc() {} } pub struct PubCrate; #[ext] impl PubCrate { pub(crate) const ASSOC: u8 = 1; pub(crate) type Assoc = u8; pub(crate) fn assoc() {} } pub struct PubSelf; #[ext] impl PubSelf { pub(self) const ASSOC: u8 = 1; pub(self) type Assoc = u8; pub(self) fn assoc() {} } pub mod m { use easy_ext::ext; pub struct PubSuper; #[ext] impl PubSuper { pub(super) const ASSOC: u8 = 1; pub(super) type Assoc = u8; pub(super) fn assoc() {} } pub struct PubIn; #[ext] impl PubIn { pub(in super::m) const ASSOC: u8 = 1; pub(in super::m) type Assoc = u8; pub(in super::m) fn assoc() {} } } } #[test] fn arg_pat() { #[ext] impl str { fn f((x, y): (u8, u8)) { let _x = x; let _y = y; } } } #[test] fn arbitrary_self_types() { #[ext] #[allow(clippy::needless_arbitrary_self_type)] impl String { fn recv(self: Self) {} fn recv_ref(self: &Self) {} fn recv_mut(self: &mut Self) {} fn recv_rc(self: Rc) {} fn recv_rc_ref(self: &Rc) {} fn recv_rc_mut(self: &mut Rc) {} fn recv_pin_box(self: Pin>) {} } String::default().recv(); String::default().recv_ref(); String::default().recv_mut(); Rc::new(String::default()).recv_rc(); Rc::new(String::default()).recv_rc_ref(); Rc::new(String::default()).recv_rc_mut(); Box::pin(String::default()).recv_pin_box(); }