wiwi/
chain.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
use crate::prelude_std::*;

pub use array::ArrayChain;
pub use vec::{ vec_chain, VecChain };

mod array;
mod vec;

pub trait Chain
where
	Self: Sized + private::Sealed + Into<Self::Inner> + AsRef<Self::Inner> + AsMut<Self::Inner>,
	Self::Inner: ChainInner<Chain = Self>
{
	type Inner;

	#[inline]
	fn into_inner(self) -> Self::Inner {
		self.into()
	}

	#[inline]
	fn from_inner(inner: Self::Inner) -> Self {
		inner.into()
	}

	#[inline]
	fn as_inner(&self) -> &Self::Inner {
		self.as_ref()
	}

	#[inline]
	fn as_inner_mut(&mut self) -> &mut Self::Inner {
		self.as_mut()
	}

	/// Takes a closure that is called, passing in a reference to the inner value
	///
	/// This is useful for if there is something we have not implemented, or you
	/// otherwise need to borrow the inner struct for something, but are in the
	/// middle of some chain. It lets you do otherwise nonchainable operations
	/// inline with other chaining operations, so no need to break the chain c:
	///
	/// The closure passed in is allowed to return anything, but the return value
	/// is simply ignored. This makes it so you can call a function for its side
	/// effect, even if that function returns something you wouldn't have needed
	/// anyways.
	///
	/// For example, [`MaybeUninit::write`] returns `&mut T`, but you might not
	/// need that reference, so instead of writing `
	/// .with_inner(|val| { val.write(...); })`, you can simply write
	/// `.with_inner(|val| val.write(...))`.
	///
	/// # Examples
	///
	/// ```
	/// # use wiwi::chain::{ Chain as _, VecChain };
	/// let chain = VecChain::<usize>::new();
	///
	/// // let's pretend `push` and `reserve` don't already have chainable versions...
	/// let chain = chain
	///    .with_inner(|v| v.reserve(10))
	///    .with_inner(|v| v.push(1))
	///    .with_inner(|v| v.push(2));
	///
	/// assert!(chain.as_inner().len() == 2);
	/// assert!(chain.as_inner().capacity() >= 10);
	/// ```
	#[inline]
	fn with_inner<F, Void>(mut self, f: F) -> Self
	where
		F: FnOnce(&mut Self::Inner) -> Void
	{
		let _void = f(self.as_inner_mut());
		self
	}
}

pub trait ChainInner
where
	Self: Sized + private::Sealed + Into<Self::Chain>,
	Self::Chain: Chain<Inner = Self>
{
	type Chain;

	#[inline]
	fn into_chain(self) -> Self::Chain {
		self.into()
	}

	#[inline]
	fn from_chain(chain: Self::Chain) -> Self {
		chain.into()
	}
}

/// Trait implemented on chains and their inner types, allowing you to get a reference
/// to the inner type regardless of if the chain or the inner type is passed in
pub trait AsChainInner<I>
where
	Self: Sized + private::Sealed
{
	fn as_inner(&self) -> &I;
	fn as_inner_mut(&mut self) -> &mut I;
}

/// Trait for output locations that can be passed to a chainer
///
/// # Safety
///
/// Consumers of this trait must call [`store`] before they return again,
/// implementors must make sure that `self` is written to when called, so users
/// can rely on the fact that the output location was written to. For example,
/// users can pass a reference to [`MaybeUninit`] and rely on the fact that it
/// got initialised, and safely call [`assume_init`](MaybeUninit::assume_init).
///
/// [`store`]: OutputStorage::store
pub unsafe trait OutputStorage<T>
where
	Self: Sized + private::OutputStorageSealed<T>
{
	/// # Safety
	///
	/// This can and should only be called once, and you must call it before returning,
	/// so users can rely on the fact that something got stored in `self`
	unsafe fn store(self, item: T);
}

impl<T> private::OutputStorageSealed<T> for &mut T {}
// SAFETY: we always write once to `self`
unsafe impl<T> OutputStorage<T> for &mut T {
	#[inline]
	unsafe fn store(self, item: T) {
		*self = item;
	}
}

impl<T> private::OutputStorageSealed<T> for &mut MaybeUninit<T> {}
// SAFETY: we always write once to `self`
unsafe impl<T> OutputStorage<T> for &mut MaybeUninit<T> {
	#[inline]
	unsafe fn store(self, item: T) {
		self.write(item);
	}
}

impl<T> private::OutputStorageSealed<T> for &mut Option<T> {}
// SAFETY: we always write once to `self`
unsafe impl<T> OutputStorage<T> for &mut Option<T> {
	#[inline]
	unsafe fn store(self, item: T) {
		*self = Some(item);
	}
}

macro_rules! decl_chain {
	{
		$(#[$meta:meta])*
		$(
			generics_decl: [$($generics_decl:tt)*]
			generics: [$($generics:tt)*]
		)?
		chain: $chain:ident
		inner: $($inner:tt)+
	} => {
		// the struct declaration
		$(#[$meta])*
		#[must_use = "a chain always takes ownership of itself, performs the operation, then returns itself again"]
		#[repr(transparent)]
		pub struct $chain$(<$($generics_decl)*>)? {
			_inner: $($inner)+
		}

		// the private::Sealed impls
		impl$(<$($generics_decl)*>)? $crate::chain::private::Sealed for $chain$(<$($generics)*>)? {}
		impl$(<$($generics_decl)*>)? $crate::chain::private::Sealed for $($inner)+ {}

		// impl Chain
		impl$(<$($generics_decl)*>)? $crate::chain::Chain for $chain$(<$($generics)*>)? {
			type Inner = $($inner)+;
		}

		// impl ChainInner
		impl$(<$($generics_decl)*>)? $crate::chain::ChainInner for $($inner)+ {
			type Chain = $chain$(<$($generics)*>)?;
		}

		impl$(<$($generics_decl)*>)? $crate::chain::AsChainInner<$($inner)+> for $chain$(<$($generics)*>)? {
			#[inline]
			fn as_inner(&self) -> &$($inner)+ {
				&self._inner
			}

			#[inline]
			fn as_inner_mut(&mut self) -> &mut $($inner)+ {
				&mut self._inner
			}
		}

		impl$(<$($generics_decl)*>)? $crate::chain::AsChainInner<$($inner)+> for $($inner)+ {
			#[inline]
			fn as_inner(&self) -> &$($inner)+ {
				self
			}

			#[inline]
			fn as_inner_mut(&mut self) -> &mut $($inner)+ {
				self
			}
		}

		// impl From<Inner> for Chain
		impl$(<$($generics_decl)*>)? $crate::prelude_std::From<$($inner)+> for $chain$(<$($generics)*>)? {
			#[inline]
			fn from(inner: $($inner)+) -> Self {
				Self { _inner: inner }
			}
		}

		// impl From<&Inner> for Chain where Inner: Clone
		impl$(<$($generics_decl)*>)? $crate::prelude_std::From<&$($inner)+> for $chain$(<$($generics)*>)?
		where
			$($inner)+: $crate::prelude_std::Clone
		{
			#[inline]
			fn from(inner: &$($inner)+) -> Self {
				Self { _inner: inner.clone() }
			}
		}

		// impl From<&mut Inner> for Chain where Inner: Clone
		impl$(<$($generics_decl)*>)? $crate::prelude_std::From<&mut $($inner)+> for $chain$(<$($generics)*>)?
		where
			$($inner)+: $crate::prelude_std::Clone
		{
			#[inline]
			fn from(inner: &mut $($inner)+) -> Self {
				Self { _inner: inner.clone() }
			}
		}

		// impl From<Chain> for Inner
		impl$(<$($generics_decl)*>)? $crate::prelude_std::From<$chain$(<$($generics)*>)?> for $($inner)+ {
			#[inline]
			fn from(chain: $chain$(<$($generics)*>)?) -> Self {
				chain._inner
			}
		}

		// impl From<&Chain> for Inner where Inner: Clone
		impl$(<$($generics_decl)*>)? $crate::prelude_std::From<&$chain$(<$($generics)*>)?> for $($inner)+
		where
			$($inner)+: $crate::prelude_std::Clone
		{
			#[inline]
			fn from(chain: &$chain$(<$($generics)*>)?) -> Self {
				chain._inner.clone()
			}
		}

		// impl From<&mut Chain> for Inner where Inner: Clone
		impl$(<$($generics_decl)*>)? $crate::prelude_std::From<&mut $chain$(<$($generics)*>)?> for $($inner)+
		where
			$($inner)+: $crate::prelude_std::Clone
		{
			#[inline]
			fn from(chain: &mut $chain$(<$($generics)*>)?) -> Self {
				chain._inner.clone()
			}
		}

		// impl AsRef<Inner>
		impl$(<$($generics_decl)*>)? $crate::prelude_std::AsRef<$($inner)+> for $chain$(<$($generics)*>)? {
			#[inline]
			fn as_ref(&self) -> &$($inner)+ {
				&self._inner
			}
		}

		// impl AsMut<Inner>
		impl$(<$($generics_decl)*>)? $crate::prelude_std::AsMut<$($inner)+> for $chain$(<$($generics)*>)? {
			#[inline]
			fn as_mut(&mut self) -> &mut $($inner)+ {
				&mut self._inner
			}
		}

		// impl Clone
		impl$(<$($generics_decl)*>)? $crate::prelude_std::Clone for $chain$(<$($generics)*>)?
		where
			$($inner)+: $crate::prelude_std::Clone
		{
			#[inline]
			fn clone(&self) -> Self {
				let inner = <Self as $crate::chain::Chain>::as_inner(self);
				let inner = <<Self as $crate::chain::Chain>::Inner as $crate::prelude_std::Clone>::clone(inner);
				<Self as $crate::chain::Chain>::from_inner(inner)
			}

			#[inline]
			fn clone_from(&mut self, source: &Self) {
				let inner_self = <Self as $crate::chain::Chain>::as_inner_mut(self);
				let inner_source = <Self as $crate::chain::Chain>::as_inner(source);
				<<Self as $crate::chain::Chain>::Inner as $crate::prelude_std::Clone>::clone_from(inner_self, inner_source)
			}
		}

		// impl Copy
		impl$(<$($generics_decl)*>)? $crate::prelude_std::Copy for $chain$(<$($generics)*>)?
		where
			$($inner)+: $crate::prelude_std::Copy
		{}

		// impl Debug
		impl$(<$($generics_decl)*>)? $crate::prelude_std::Debug for $chain$(<$($generics)*>)?
		where
			$($inner)+: $crate::prelude_std::Debug
		{
			#[inline]
			fn fmt(&self, f: &mut $crate::prelude_std::fmt::Formatter<'_>) -> $crate::prelude_std::fmt::Result {
				let mut dbg_struct = $crate::prelude_std::fmt::Formatter::debug_struct(f, stringify!($chain));
				$crate::prelude_std::fmt::DebugStruct::field(
					&mut dbg_struct,
					"_",
					<Self as $crate::chain::Chain>::as_inner(self)
				);
				$crate::prelude_std::fmt::DebugStruct::finish(&mut dbg_struct)
			}
		}

		// impl Default
		impl$(<$($generics_decl)*>)? $crate::prelude_std::Default for $chain$(<$($generics)*>)?
		where
			$($inner)+: $crate::prelude_std::Default
		{
			#[inline]
			fn default() -> Self {
				let inner = <<Self as $crate::chain::Chain>::Inner as Default>::default();
				<Self as $crate::chain::Chain>::from_inner(inner)
			}
		}

		// impl Display
		impl$(<$($generics_decl)*>)? $crate::prelude_std::Display for $chain$(<$($generics)*>)?
		where
			$($inner)+: $crate::prelude_std::Display
		{
			#[inline]
			fn fmt(&self, f: &mut $crate::prelude_std::fmt::Formatter<'_>) -> $crate::prelude_std::fmt::Result {
				let inner = <Self as $crate::chain::Chain>::as_inner(self);
				<<Self as $crate::chain::Chain>::Inner as Display>::fmt(inner, f)
			}
		}

		// impl Eq
		// impl Ord

		// impl PartialEq/PartialOrd chain <-> inner
		$crate::chain::decl_chain! {
			@impl_partial_cmp
			[$([$($generics_decl)*])?]
			[$($inner)+]

			[$chain$(<$($generics)*>)?]
			[<$chain$(<$($generics)*>)? as $crate::chain::Chain>::as_inner]

			[$($inner)+]
			[$crate::prelude_std::identity]
		}

		// impl PartialEq/PartialOrd inner <-> chain
		$crate::chain::decl_chain! {
			@impl_partial_cmp
			[$([$($generics_decl)*])?]
			[$($inner)+]

			[$($inner)+]
			[$crate::prelude_std::identity]

			[$chain$(<$($generics)*>)?]
			[<$chain$(<$($generics)*>)? as $crate::chain::Chain>::as_inner]
		}

		// impl PartialEq/PartialOrd chain <-> chain
		$crate::chain::decl_chain! {
			@impl_partial_cmp
			[$([$($generics_decl)*])?]
			[$($inner)+]

			[$chain$(<$($generics)*>)?]
			[<$chain$(<$($generics)*>)? as $crate::chain::Chain>::as_inner]

			[$chain$(<$($generics)*>)?]
			[<$chain$(<$($generics)*>)? as $crate::chain::Chain>::as_inner]
		}
	};

	{
		@impl_partial_cmp
		[$([$($generics_decl:tt)*])?]
		[$($inner:tt)+]

		[$($left_ty:tt)+]
		[$left_expr:expr]

		[$($right_ty:tt)+]
		[$right_expr:expr]
	} => {
		impl$(<$($generics_decl)*>)? $crate::prelude_std::PartialEq<$($right_ty)+> for $($left_ty)+
		where
			$($inner)+: $crate::prelude_std::PartialEq<$($inner)+>
		{
			#[inline]
			fn eq(&self, other: &$($right_ty)+) -> $crate::prelude_std::std::primitive::bool {
				$crate::chain::decl_chain! {
					@impl_partial_cmp_helper
					[$($inner)+]
					PartialEq::eq($left_expr(self), $right_expr(other))
				}
			}

			#[expect(
				clippy::partialeq_ne_impl,
				reason = "inner might have overridden ne for whatever reason, and we should use it if so"
			)]
			#[inline]
			fn ne(&self, other: &$($right_ty)+) -> $crate::prelude_std::std::primitive::bool {
				$crate::chain::decl_chain! {
					@impl_partial_cmp_helper
					[$($inner)+]
					PartialEq::ne($left_expr(self), $right_expr(other))
				}
			}
		}

		impl$(<$($generics_decl)*>)? $crate::prelude_std::PartialOrd<$($right_ty)+> for $($left_ty)+
		where
			$($inner)+: $crate::prelude_std::PartialOrd<$($inner)+>
		{
			#[inline]
			fn partial_cmp(&self, other: &$($right_ty)+) -> $crate::prelude_std::Option<$crate::prelude_std::cmp::Ordering> {
				$crate::chain::decl_chain! {
					@impl_partial_cmp_helper
					[$($inner)+]
					PartialOrd::partial_cmp($left_expr(self), $right_expr(other))
				}
			}

			#[inline]
			fn lt(&self, other: &$($right_ty)+) -> $crate::prelude_std::std::primitive::bool {
				$crate::chain::decl_chain! {
					@impl_partial_cmp_helper
					[$($inner)+]
					PartialOrd::lt($left_expr(self), $right_expr(other))
				}
			}

			#[inline]
			fn le(&self, other: &$($right_ty)+) -> $crate::prelude_std::std::primitive::bool {
				$crate::chain::decl_chain! {
					@impl_partial_cmp_helper
					[$($inner)+]
					PartialOrd::le($left_expr(self), $right_expr(other))
				}
			}

			#[inline]
			fn gt(&self, other: &$($right_ty)+) -> $crate::prelude_std::std::primitive::bool {
				$crate::chain::decl_chain! {
					@impl_partial_cmp_helper
					[$($inner)+]
					PartialOrd::gt($left_expr(self), $right_expr(other))
				}
			}

			#[inline]
			fn ge(&self, other: &$($right_ty)+) -> $crate::prelude_std::std::primitive::bool {
				$crate::chain::decl_chain! {
					@impl_partial_cmp_helper
					[$($inner)+]
					PartialOrd::ge($left_expr(self), $right_expr(other))
				}
			}
		}
	};

	{
		@impl_partial_cmp_helper
		[$($inner:tt)+]
		$trait:ident::$trait_fn:ident($($stuff:tt)*)
	} => {
		<$($inner)+ as $crate::prelude_std::$trait<$($inner)+>>::$trait_fn($($stuff)*)
	};
}
use decl_chain;

macro_rules! chain_fn {
	{
		$(#[$meta:meta])*
		$fn_name:ident
		$([$($generics:tt)*])?
		($inner:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub fn $fn_name$(<$($generics)*>)?(mut self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			let $inner = <Self as $crate::chain::Chain>::as_inner_mut(&mut self);
			$crate::prelude_std::identity::<()>($body);
			self
		}
	};

	{
		$(#[$meta:meta])*
		self
		$fn_name:ident
		$([$($generics:tt)*])?
		($self:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub fn $fn_name$(<$($generics)*>)?(mut $self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			$crate::prelude_std::identity::<()>($body);
			$self
		}
	};

	{
		$(#[$meta:meta])*
		unsafe
		$fn_name:ident
		$([$($generics:tt)*])?
		($inner:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub unsafe fn $fn_name$(<$($generics)*>)?(mut self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			let $inner = <Self as $crate::chain::Chain>::as_inner_mut(&mut self);
			$crate::prelude_std::identity::<()>($body);
			self
		}
	};

	{
		$(#[$meta:meta])*
		unsafe self
		$fn_name:ident
		$([$($generics:tt)*])?
		($self:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub unsafe fn $fn_name$(<$($generics)*>)?(mut $self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			$crate::prelude_std::identity::<()>($body);
			$self
		}
	};

	{
		$(#[$meta:meta])*
		move
		$fn_name:ident
		$([$($generics:tt)*])?
		($inner:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub fn $fn_name$(<$($generics)*>)?($self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			let mut $inner = <Self as $crate::chain::Chain>::into_inner(self);
			<Self as $crate::chain::Chain>::from_inner($body)
		}
	};

	{
		$(#[$meta:meta])*
		move self
		$fn_name:ident
		$([$($generics:tt)*])?
		($self:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub fn $fn_name$(<$($generics)*>)?($self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			$self
		}
	};

	{
		$(#[$meta:meta])*
		unsafe move
		$fn_name:ident
		$([$($generics:tt)*])?
		($inner:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub unsafe fn $fn_name$(<$($generics)*>)?($self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			let mut $inner = <Self as $crate::chain::Chain>::into_inner(self);
			<Self as $crate::chain::Chain>::from_inner($body)
		}
	};

	{
		$(#[$meta:meta])*
		unsafe move self
		$fn_name:ident
		$([$($generics:tt)*])?
		($self:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub unsafe fn $fn_name$(<$($generics)*>)?($self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			$self
		}
	};

	{
		$(#[$meta:meta])*
		void
		$fn_name:ident
		$([$($generics:tt)*])?
		($inner:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub fn $fn_name$(<$($generics)*>)?(mut self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			let $inner = <Self as $crate::chain::Chain>::as_inner_mut(&mut self);
			let _ = $body;
			self
		}
	};

	{
		$(#[$meta:meta])*
		void self
		$fn_name:ident
		$([$($generics:tt)*])?
		($self:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub fn $fn_name$(<$($generics)*>)?(mut $self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			let _ = $body
			$self
		}
	};

	{
		$(#[$meta:meta])*
		unsafe void
		$fn_name:ident
		$([$($generics:tt)*])?
		($inner:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub unsafe fn $fn_name$(<$($generics)*>)?(mut self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			let $inner = <Self as $crate::chain::Chain>::as_inner_mut(&mut self);
			let _ = $body;
			self
		}
	};

	{
		$(#[$meta:meta])*
		unsafe void self
		$fn_name:ident
		$([$($generics:tt)*])?
		($self:ident $($args:tt)*)
		$(where { $($where_clause:tt)* })?
		=> $body:expr
	} => {
		$(#[$meta])*
		#[inline]
		pub unsafe fn $fn_name$(<$($generics)*>)?(mut $self $($args)*) -> Self
		$(where $($where_clause)*)?
		{
			let _ = $body
			$self
		}
	};
}
use chain_fn;

/// notouchie
mod private {
	/// notouchie
	pub trait Sealed {}
	/// notouchie
	pub trait OutputStorageSealed<T> {}
}