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 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
/*! Contains traits implemented on field paths,taking Structural types as parameters. */ #![allow(non_snake_case)] use crate::{ field::{errors::IsFieldErr, FailedAccess, InfallibleAccess, MovedOutFields}, path::{IsSingleFieldPath, ShallowFieldPath}, }; ///////////////////////////////////////////////////////////////////////////// mod components; mod nested_field_path; mod path_set_types; ///////////////////////////////////////////////////////////////////////////// /// Queries the type of a nested field in This, /// what field is queried is determined by `FieldPath`, /// /// # Example /// /// ``` /// use structural::{ /// field::RevGetFieldType, /// GetFieldType3,StructuralExt,Structural, /// FP,fp, /// }; /// /// fn main(){ /// let this=TopLevel::default(); /// /// let baz: &RevGetFieldType<FP!(foo.bar.baz), TopLevel>= /// this.field_(fp!(foo.bar.baz)); /// assert_eq!( *baz, Vec::new() ); /// /// let strand: &RevGetFieldType<FP!(foo.bar.strand), TopLevel>= /// this.field_(fp!(foo.bar.strand)); /// assert_eq!( *strand, String::new() ); /// } /// /// #[derive(Debug,Default,Structural)] /// struct TopLevel{ /// pub foo:Foo, /// } /// /// #[derive(Debug,Default,Structural)] /// struct Foo{ /// pub bar:Bar, /// } /// /// #[derive(Debug,Default,Structural)] /// struct Bar{ /// pub baz:Vec<()>, /// pub strand:String, /// } /// ``` pub type RevGetFieldType<FieldPath, This> = <FieldPath as RevFieldType<This>>::Ty; /// Queries the error type returned by `Rev*FieldImpl` methods. pub type RevFieldErrOut<FieldPath, This> = <FieldPath as RevFieldErr<This>>::Err; ///////////////////////////////////////////////////////////////////////////// /// Like `FieldType`,except that the parameters are reversed. /// `This` is the type we are accessing,and `Self` is a field path. pub trait RevFieldType<This: ?Sized>: IsSingleFieldPath { /// The type of the field. type Ty: ?Sized; } /// For querying the error type returned by single-field `Rev*` trait methods. pub trait RevFieldErr<This: ?Sized>: IsSingleFieldPath + RevFieldType<This> { /// The error returned by `rev_*` methods. /// /// This can be either: /// /// - [`InfallibleAccess`]: /// For `Rev*` accessors that return a field that always exists, /// most often in a struct. /// /// - [`FailedAccess`]: /// For `Rev*` accessors that attempt to return a field that may not exist, /// most often inside an enum. /// /// [`InfallibleAccess`]: ../errors/enum.InfallibleAccess.html /// [`FailedAccess`]: ../errors/struct.FailedAccess.html type Err: IsFieldErr; } ///////////////////////////////////////////////////////////////////////////// /// Like `Get*Field`,except that the parameters are reversed, /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// This is used by the [`StructuralExt::field_`](../../trait.StructuralExt.html#method.field_) /// method. /// /// # Use as bound /// /// For examples of using `RevGetFieldImpl` as a bound look at example for: /// /// - [RevGetField](./trait.RevGetField.html): /// For infallible field access,generally struct fields,not inside of a nested enum. /// /// - [OptRevGetField](./trait.OptRevGetField.html): /// Fallible field access,generally for getting a field in a (potentially nested) enum. /// /// # Example /// /// This example demonstrates implementing `RevGetFieldImpl` to index a slice from the end. /// /// /// ```rust /// use structural::field::{FailedAccess,RevFieldType,RevFieldErr,RevGetFieldImpl}; /// use structural::path::IsSingleFieldPath; /// use structural::StructuralExt; /// /// struct FromEnd(usize); /// /// impl IsSingleFieldPath for FromEnd{} /// /// impl<'a,T> RevFieldType<[T]> for FromEnd { /// type Ty = T; /// } /// impl<'a,T> RevFieldErr<[T]> for FromEnd { /// type Err = FailedAccess; /// } /// impl<'a,T> RevGetFieldImpl<'a,[T]> for FromEnd { /// fn rev_get_field(self, this: &'a [T]) -> Result<&'a T, FailedAccess>{ /// let len=this.len(); /// this.get(len.wrapping_sub(self.0 + 1)) /// .ok_or(FailedAccess) /// } /// } /// /// let slice=&[3,5,8,13][..]; /// /// assert_eq!( slice.field_(FromEnd(0)), Some(&13) ); /// assert_eq!( slice.field_(FromEnd(1)), Some(&8) ); /// assert_eq!( slice.field_(FromEnd(2)), Some(&5) ); /// assert_eq!( slice.field_(FromEnd(3)), Some(&3) ); /// assert_eq!( slice.field_(FromEnd(4)), None ); /// /// /// ``` pub trait RevGetFieldImpl<'a, This: ?Sized>: RevFieldErr<This> { /// Accesses the field that `self` represents inside of `this`,by reference. fn rev_get_field(self, this: &'a This) -> Result<&'a Self::Ty, Self::Err>; } ///////////////////////////////////////////////////////////////////////////// /// Like Get*FieldMut,except that the parameters are reversed, /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// This is used by the [`StructuralExt::field_mut`](../../trait.StructuralExt.html#method.field_mut) /// method. /// /// # Safety /// /// The `rev_get_field_raw_mut` function must return a valid pointer derived /// from the passed in pointer, that is safe to dereference mutably. /// /// ### Locality of Implementations /// /// You must only implement `RevGetFieldMutImpl` for field paths defined locally /// (in the same crate as the implementation itself). /// /// This allows crate authors to add more `RevGetFieldMutImpl` impls while /// lowering the rist of aliasing the returned pointer from `rev_get_field_raw_mut`. /// /// # Use as bound /// /// For examples of using `RevGetFieldMutImpl` as a bound look at example for: /// /// - [RevGetFieldMut](./trait.RevGetFieldMut.html): /// For infallible field access,generally struct fields,not inside of a nested enum. /// /// - [OptRevGetFieldMut](./trait.OptRevGetFieldMut.html): /// Fallible field access,generally for getting a field in a (potentially nested) enum. /// /// # Example /// /// This example demonstrates implementing `RevGetFieldMutImpl` to choose between 2 fields /// based on a runtime value. /// /// ```rust /// use structural::{ /// field::{FailedAccess, RevFieldErr, RevFieldType, RevGetFieldImpl, RevGetFieldMutImpl}, /// path::IsSingleFieldPath, /// StructuralExt, ts, /// }; /// /// let mut tup=(3,5,8,13); /// /// assert_eq!( tup.field_mut(Choose(Which::First, ts!(0), ts!(1))), &mut 3 ); /// assert_eq!( tup.field_mut(Choose(Which::Second, ts!(0), ts!(1))), &mut 5 ); /// assert_eq!( tup.field_mut(Choose(Which::First, ts!(1), ts!(2))), &mut 5 ); /// assert_eq!( tup.field_mut(Choose(Which::Second, ts!(1), ts!(2))), &mut 8 ); /// /// /// #[derive(Debug,Copy,Clone,PartialEq)] /// struct Choose<P0,P1>(Which,P0,P1); /// /// #[derive(Debug,Copy,Clone,PartialEq)] /// enum Which{ /// First, /// Second, /// } /// /// impl<P0,P1> IsSingleFieldPath for Choose<P0,P1>{} /// /// impl<P0,P1,T> RevFieldType<T> for Choose<P0,P1> /// where /// P0: RevFieldType<T>, /// P1: RevFieldType<T, Ty=P0::Ty>, /// { /// type Ty = P0::Ty; /// } /// /// impl<P0,P1,T> RevFieldErr<T> for Choose<P0,P1> /// where /// P0: RevFieldErr<T>, /// P1: RevFieldErr<T, Ty=P0::Ty, Err=P0::Err>, /// { /// type Err = P0::Err; /// } /// /// impl<'a,P0,P1,T> RevGetFieldImpl<'a,T> for Choose<P0,P1> /// where /// P0: RevGetFieldImpl<'a,T>, /// P1: RevGetFieldImpl<'a,T, Ty=P0::Ty, Err=P0::Err>, /// { /// fn rev_get_field(self, this: &'a T) -> Result<&'a P0::Ty, P0::Err>{ /// match self.0 { /// Which::First=>self.1.rev_get_field(this), /// Which::Second=>self.2.rev_get_field(this), /// } /// } /// } /// /// unsafe impl<'a,P0,P1,T> RevGetFieldMutImpl<'a,T> for Choose<P0,P1> /// where /// P0: RevGetFieldMutImpl<'a,T>, /// P1: RevGetFieldMutImpl<'a,T, Ty=P0::Ty, Err=P0::Err>, /// { /// fn rev_get_field_mut(self, this: &'a mut T) -> Result<&'a mut P0::Ty, P0::Err>{ /// match self.0 { /// Which::First=>self.1.rev_get_field_mut(this), /// Which::Second=>self.2.rev_get_field_mut(this), /// } /// } /// /// unsafe fn rev_get_field_raw_mut(self, this: *mut T) -> Result<*mut P0::Ty, P0::Err>{ /// match self.0 { /// Which::First=>self.1.rev_get_field_raw_mut(this), /// Which::Second=>self.2.rev_get_field_raw_mut(this), /// } /// } /// } /// /// ``` pub unsafe trait RevGetFieldMutImpl<'a, This: ?Sized>: RevGetFieldImpl<'a, This> { /// Accesses the field that `self` represents inside of `this`,by mutable reference. fn rev_get_field_mut(self, this: &'a mut This) -> Result<&'a mut Self::Ty, Self::Err>; /// Accesses the field that `self` represents inside of `this`,by raw pointer. /// /// # Safety /// /// You must pass a pointer to a fully initialized instance of `This`. unsafe fn rev_get_field_raw_mut(self, this: *mut This) -> Result<*mut Self::Ty, Self::Err>; } ///////////////////////////////////////////////////////////////////////////// /// Like `Into*Field::into_*field_`,except that the parameters are reversed, /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// This is used by the /// [`StructuralExt::into_field`](../../trait.StructuralExt.html#method.into_field) /// and [`StructuralExt::box_into_field`](../../trait.StructuralExt.html#method.box_into_field) /// method. /// /// # Use as bound /// /// For examples of using `RevIntoFieldImpl` as a bound look at example for: /// /// - [RevIntoField](./trait.RevIntoField.html): /// For infallible field access,generalerally struct fields,not inside of a nested enum. /// /// - [OptRevIntoField](./trait.OptRevIntoField.html): /// Fallible field access,generally for getting a field in a (potentially nested) enum. /// /// # Example /// /// This example demonstrates implementing `RevIntoFieldImpl`. /// /// The transmute in this example is only sound because of the /// `#[repr(transparent)]` attribute on the `Wrapper` struct, /// and the reference to` T` is converted into a reference to `Wrapper<T>`.<br> /// Transmutes like that are not sound in the more general case, /// like transmuting from an arbitrary `Foo<T>` to `Foo<Newtype<T>>` using `std::mem::transmute`, /// since the layout of `Foo` is allowed to change. /// /// ```rust /// use structural::{ /// field::{FailedAccess, RevFieldErr, RevFieldType, RevGetFieldImpl, RevIntoFieldImpl}, /// path::IsSingleFieldPath, /// StructuralExt, fp, /// }; /// /// use core::mem; /// /// let mut tup=(3,5,8,13); /// /// assert_eq!( tup.into_field(Wrapped(fp!(0))), Newtype(3) ); /// assert_eq!( tup.into_field(Wrapped(fp!(1))), Newtype(5) ); /// assert_eq!( tup.into_field(Wrapped(fp!(2))), Newtype(8) ); /// assert_eq!( tup.into_field(Wrapped(fp!(3))), Newtype(13) ); /// /// /// #[derive(Debug,Copy,Clone,PartialEq)] /// struct Wrapped<P>(P); /// /// #[repr(transparent)] /// #[derive(Debug,Copy,Clone,PartialEq)] /// pub struct Newtype<T:?Sized>(T); /// /// impl<P> IsSingleFieldPath for Wrapped<P>{} /// /// impl<P,T> RevFieldType<T> for Wrapped<P> /// where /// P: RevFieldType<T>, /// { /// type Ty = Newtype<P::Ty>; /// } /// /// impl<P,T> RevFieldErr<T> for Wrapped<P> /// where /// P: RevFieldErr<T>, /// { /// type Err = P::Err; /// } /// /// impl<P,T> RevIntoFieldImpl<T> for Wrapped<P> /// where /// P: RevIntoFieldImpl<T>, /// P::Ty: Sized, /// { /// fn rev_into_field(self, this: T) -> Result<Self::Ty, Self::Err> /// where /// Self::Ty: Sized /// { /// self.0.rev_into_field(this) /// .map(Newtype) /// } /// } /// ``` pub trait RevIntoFieldImpl<This: ?Sized>: RevFieldErr<This> { /// Accesses the field that `self` represents inside of `this`,by value. fn rev_into_field(self, this: This) -> Result<Self::Ty, Self::Err> where This: Sized, Self::Ty: Sized; } /// Like `Into*Field::move_out_*field_`,except that the parameters are reversed, /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// This is used by the implementations of the [`RevIntoMultiFieldImpl`] trait. /// /// [`RevIntoMultiFieldImpl`]: ../multi_fields/trait.RevIntoMultiFieldImpl.html /// /// # Safety /// /// Implementors of this traits must do any of: /// /// - Delegate their implementations to the `IntoField` and `IntoVariantField` traits. /// /// - Be implemented following the safety requirements of the /// [`IntoField`](../trait.IntoField.html#implementing-move_out_field_) /// (if it's moving out a struct field) or /// [`IntoVariantField`](../trait.IntoVariantField.html#implementing-move_out_vfield_) /// (if it's moving out an enum field). /// pub unsafe trait RevMoveOutFieldImpl<This: ?Sized>: RevIntoFieldImpl<This> + ShallowFieldPath { /// Moves out the field that `self` represents inside of `this`. /// /// # Safety /// /// The same instance of `MovedOutFields` must be passed to every call to /// `rev_move_out_field` on the same instance of `this`, /// as well as not mutating that `MovedOutFields` instance outside of /// `rev_move_out_field` with the same `This` parameter. /// /// Each field must be moved with any method at most once on the same instance of `This`. unsafe fn rev_move_out_field( self, this: &mut This, moved: &mut MovedOutFields, ) -> Result<Self::Ty, Self::Err> where Self::Ty: Sized; } ///////////////////////////////////////////////////////////////////////////// macro_rules! declare_accessor_trait_alias { ( $(#[$attr:meta])* $vis:vis trait $trait_name:ident<$($lt:lifetime,)?$This:ident>= $($supertraits:tt)* ) => ( $(#[$attr])* $vis trait $trait_name<$($lt,)? $This >:$($supertraits)* {} impl<$($lt,)? Path,$This> $trait_name<$($lt,)? $This > for Path where Path:$($supertraits)* {} ) } declare_accessor_trait_alias! { /// A trait alias for an infallible [`RevGetFieldImpl`](./trait.RevGetFieldImpl.html), /// generally used to access fields in structs(not in a nested enum inside the struct). /// /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// # Example /// /// This example shows how to access a nested struct field by reference. /// /// ```rust /// use structural::field::RevGetField; /// use structural::{StructuralExt,FP,fp}; /// /// /// # fn main(){ /// let tup=(3,5,(8,(13,21))); /// assert_eq!( get_nested(&tup), &13 ); /// # } /// /// fn get_nested<'a,T>(this:&'a T)->&'a i32 /// where /// FP!(2.1.0): RevGetField<'a,T,Ty=i32> /// { /// this.field_(fp!(2.1.0)) /// } /// /// /// ``` pub trait RevGetField<'a,This>= RevGetFieldImpl<'a,This,Err=InfallibleAccess> } declare_accessor_trait_alias! { /// A trait alias for a fallible [`RevGetFieldImpl`](./trait.RevGetFieldImpl.html), /// generally used to access fields inside enums. /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// # Example /// /// This example shows how you can access an enum field by reference. /// /// ```rust /// use structural::field::OptRevGetField; /// use structural::{StructuralExt,FP,fp}; /// /// let tup1=(3,5,(8,(Some(13),21))); /// let tup2=(3,5,(8,(None,21))); /// /// assert_eq!( get_nested(&tup1), Some(&13) ); /// assert_eq!( get_nested(&tup2), None ); /// /// fn get_nested<'a,T>(this:&'a T)->Option<&'a i32> /// where /// FP!(2.1.0?): OptRevGetField<'a,T,Ty=i32> /// { /// this.field_(fp!(2.1.0?)) /// } /// /// /// ``` pub trait OptRevGetField<'a,This>= RevGetFieldImpl<'a,This,Err=FailedAccess> } declare_accessor_trait_alias! { /// A trait alias for an infallible [`RevGetFieldMutImpl`](./trait.RevGetFieldMutImpl.html), /// generally used to access fields in structs(not in a nested enum inside the struct). /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// # Example /// /// This example shows how to access a nested struct field by mutable reference. /// /// ```rust /// use structural::field::RevGetFieldMut; /// use structural::for_examples::{StructFoo, StructBar, Struct3}; /// use structural::{StructuralExt,FP,fp}; /// /// let mut struct_=Struct3{ /// foo: Some(0), /// bar: "hi", /// baz: StructBar{ /// bar: StructFoo{ /// foo:101, /// }, /// }, /// }; /// /// assert_eq!( get_nested(&mut struct_), &mut 101 ); /// /// fn get_nested<'a,T>(this:&'a mut T)->&'a mut i32 /// where /// FP!(baz.bar.foo): RevGetFieldMut<'a,T,Ty=i32> /// { /// this.field_mut(fp!(baz.bar.foo)) /// } /// /// /// ``` pub trait RevGetFieldMut<'a,This>= RevGetFieldMutImpl<'a,This,Err=InfallibleAccess> } declare_accessor_trait_alias! { /// A trait alias for a fallible [`RevGetFieldMutImpl`](./trait.RevGetFieldMutImpl.html), /// generally used to access fields inside enums. /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// # Example /// /// This example shows how you can access an enum field by mutable reference. /// /// ```rust /// use structural::field::OptRevGetFieldMut; /// use structural::for_examples::{StructFoo, StructBar, Struct3}; /// use structural::{StructuralExt,FP,fp}; /// /// let mut struct_=Struct3{ /// foo: Some(0), /// bar: "hi", /// baz: StructBar{ /// bar: StructFoo{ /// foo:Some("hello"), /// }, /// }, /// }; /// /// assert_eq!( get_nested(&mut struct_), Some(&mut "hello") ); /// /// fn get_nested<'a,T>(this:&'a mut T)->Option<&'a mut &'static str> /// where /// FP!(baz.bar.foo?): OptRevGetFieldMut<'a,T,Ty=&'static str> /// { /// this.field_mut(fp!(baz.bar.foo?)) /// } /// /// /// ``` pub trait OptRevGetFieldMut<'a,This>= RevGetFieldMutImpl<'a,This,Err=FailedAccess> } declare_accessor_trait_alias! { /// A trait alias for an infallible [`RevIntoFieldImpl`](./trait.RevIntoFieldImpl.html), /// generally used to access fields in structs(not in a nested enum inside the struct). /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// # Example /// /// This example shows how to access a nested struct field by value. /// /// ```rust /// use structural::field::RevIntoField; /// use structural::for_examples::StructBar; /// use structural::{StructuralExt,FP,fp}; /// /// use std::cmp::Ordering; /// /// let struct_=StructBar{ /// bar: StructBar{ /// bar: StructBar{ /// bar: Ordering::Greater /// }, /// }, /// }; /// /// assert_eq!( get_nested(struct_), Ordering::Greater ); /// /// fn get_nested<T>(this:T)->Ordering /// where /// FP!(bar.bar.bar): RevIntoField<T,Ty=Ordering> /// { /// this.into_field(fp!(bar.bar.bar)) /// } /// /// /// ``` pub trait RevIntoField<This>= RevIntoFieldImpl<This,Err=InfallibleAccess> } declare_accessor_trait_alias! { /// A trait alias for a fallible [`RevIntoFieldImpl`](./trait.RevIntoFieldImpl.html), /// generally used to access fields inside enums. /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// # Example /// /// This example shows how you can access an enum field by value. /// /// ```rust /// use structural::field::OptRevIntoField; /// use structural::for_examples::{StructFoo,WithBoom}; /// use structural::{StructuralExt,FP,fp}; /// /// let nope=StructFoo{ foo: WithBoom::Nope }; /// let boom=StructFoo{ foo: WithBoom::Boom{ a: "hello", b: &[3,5,8,13] } }; /// /// assert_eq!( get_nested(nope), None ); /// assert_eq!( get_nested(boom), Some(&[3,5,8,13][..]) ); /// /// fn get_nested<T>(this:T)->Option<&'static [u16]> /// where /// FP!(foo::Boom.b): OptRevIntoField<T,Ty=&'static [u16]> /// { /// this.into_field(fp!(foo::Boom.b)) /// } /// /// /// ``` pub trait OptRevIntoField<This>= RevIntoFieldImpl<This,Err=FailedAccess> } declare_accessor_trait_alias! { /// A trait alias for infallible [`RevIntoFieldImpl`] + [`RevGetFieldMutImpl`], /// generally used to access fields in structs(not in a nested enum inside the struct). /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// [`RevIntoFieldImpl`]: ./trait.RevIntoFieldImpl.html /// [`RevGetFieldMutImpl`]: ./trait.RevGetFieldMutImpl.html /// /// # Example /// /// This example shows how to access a struct field by mutable reference,and by value. /// /// Also,how to write extension traits with `Rev*` traits. /// /// ```rust /// use structural::field::{RevIntoFieldMut,RevGetFieldType}; /// use structural::for_examples::StructBar; /// use structural::{StructuralExt,FP,fp}; /// /// let mut foo=StructBar{ /// bar: ([(0,3),(5,8)], [(40,50,60)]), /// }; /// /// assert_eq!( foo.get_nested_mut(), &mut (5,8) ); /// assert_eq!( foo.into_nested(), (5,8) ); /// /// let mut oop=StructBar{ /// bar: [["hello","world"],["uh","no"]], /// }; /// /// assert_eq!( oop.get_nested_mut(), &mut "world" ); /// assert_eq!( oop.into_nested(), "world" ); /// /// trait GetNested: Sized { /// fn get_nested_mut<'a,Ty>(&'a mut self)->&'a mut Ty /// where /// FP!(bar.0.1): RevIntoFieldMut<'a,Self,Ty=Ty> /// { /// self.field_mut(fp!(bar.0.1)) /// } /// /// fn into_nested<'a,Ty>(self)->Ty /// where /// FP!(bar.0.1): RevIntoFieldMut<'a,Self,Ty=Ty> /// { /// self.into_field(fp!(bar.0.1)) /// } /// } /// /// impl<T> GetNested for T {} /// /// ``` pub trait RevIntoFieldMut<'a,This>= RevIntoField<This> + RevGetFieldMut<'a,This> } declare_accessor_trait_alias! { /// A trait alias for fallible [`RevIntoFieldImpl`] + [`RevGetFieldMutImpl`], /// generally used to access fields inside enums. /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// [`RevIntoFieldImpl`]: ./trait.RevIntoFieldImpl.html /// [`RevGetFieldMutImpl`]: ./trait.RevGetFieldMutImpl.html /// /// # Example /// /// This example shows how to access an enum field by mutable reference,and by value. /// /// Also,how to write extension traits with `Rev*` traits. /// /// ```rust /// use structural::field::{OptRevIntoFieldMut,RevGetFieldType}; /// use structural::for_examples::{StructFoo,WithBoom}; /// use structural::{StructuralExt,FP,fp}; /// /// let mut nope=StructFoo{ foo: WithBoom::Nope }; /// let mut boom=StructFoo{ foo: WithBoom::Boom{ a: "hello", b: &[3,5,8,13] } }; /// /// assert_eq!( nope.get_nested_mut(), None ); /// assert_eq!( boom.get_nested_mut(), Some(&mut &[3,5,8,13][..]) ); /// /// assert_eq!( nope.into_nested(), None ); /// assert_eq!( boom.into_nested(), Some(&[3,5,8,13][..]) ); /// /// trait GetNested: Sized { /// fn get_nested_mut<'a,Ty>(&'a mut self)->Option<&'a mut Ty> /// where /// FP!(foo::Boom.b): OptRevIntoFieldMut<'a,Self,Ty=Ty> /// { /// self.field_mut(fp!(foo::Boom.b)) /// } /// /// fn into_nested<'a,Ty>(self)->Option<Ty> /// where /// FP!(foo::Boom.b): OptRevIntoFieldMut<'a,Self,Ty=Ty> /// { /// self.into_field(fp!(foo::Boom.b)) /// } /// } /// /// impl<T> GetNested for T {} /// /// ``` pub trait OptRevIntoFieldMut<'a,This>= OptRevIntoField<This> + OptRevGetFieldMut<'a,This> } declare_accessor_trait_alias! { /// A trait alias for infallible [`RevIntoFieldImpl`] + [`RevGetFieldImpl`], /// generally used to access fields in structs(not in a nested enum inside the struct). /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// [`RevIntoFieldImpl`]: ./trait.RevIntoFieldImpl.html /// [`RevGetFieldImpl`]: ./trait.RevGetFieldImpl.html /// /// # Example /// /// This example shows how to access a struct field by reference,and by value. /// /// Also,how to write extension traits with `Rev*` traits. /// /// ```rust /// use structural::field::{RevIntoFieldRef,RevGetFieldType}; /// use structural::for_examples::StructBar; /// use structural::{StructuralExt,FP,fp}; /// /// let foo=StructBar{ /// bar: ([(0,3),(5,8)], [(40,50,60)]), /// }; /// /// assert_eq!( foo.get_nested(), &(5,8) ); /// assert_eq!( foo.into_nested(), (5,8) ); /// /// let oop=StructBar{ /// bar: [["hello","world"],["uh","no"]], /// }; /// /// assert_eq!( oop.get_nested(), &"world" ); /// assert_eq!( oop.into_nested(), "world" ); /// /// trait GetNested: Sized { /// fn get_nested<'a,Ty>(&'a self)->&'a Ty /// where /// FP!(bar.0.1): RevIntoFieldRef<'a,Self,Ty=Ty> /// { /// self.field_(fp!(bar.0.1)) /// } /// /// fn into_nested<'a,Ty>(self)->Ty /// where /// FP!(bar.0.1): RevIntoFieldRef<'a,Self,Ty=Ty> /// { /// self.into_field(fp!(bar.0.1)) /// } /// } /// /// impl<T> GetNested for T {} /// /// ``` pub trait RevIntoFieldRef<'a,This>= RevIntoField<This> + RevGetFieldMut<'a,This> } declare_accessor_trait_alias! { /// A trait alias for fallible [`RevIntoFieldImpl`] + [`RevGetFieldImpl`], /// generally used to access fields inside enums. /// /// `This` is the type we are accessing,and `Self` is a field path. /// /// [`RevIntoFieldImpl`]: ./trait.RevIntoFieldImpl.html /// [`RevGetFieldImpl`]: ./trait.RevGetFieldImpl.html /// /// # Example /// /// This example shows how to access an enum field by reference,and by value. /// /// Also,how to write extension traits with `Rev*` traits. /// /// ```rust /// use structural::field::{OptRevIntoFieldRef,RevGetFieldType}; /// use structural::for_examples::{StructFoo,WithBoom}; /// use structural::{StructuralExt,FP,fp}; /// /// let nope=StructFoo{ foo: WithBoom::Nope }; /// let boom=StructFoo{ foo: WithBoom::Boom{ a: "hello", b: &[3,5,8,13] } }; /// /// assert_eq!( nope.get_nested(), None ); /// assert_eq!( boom.get_nested(), Some(&&[3,5,8,13][..]) ); /// /// assert_eq!( nope.into_nested(), None ); /// assert_eq!( boom.into_nested(), Some(&[3,5,8,13][..]) ); /// /// trait GetNested: Sized { /// fn get_nested<'a,Ty>(&'a self)->Option<&'a Ty> /// where /// FP!(foo::Boom.b): OptRevIntoFieldRef<'a,Self,Ty=Ty> /// { /// self.field_(fp!(foo::Boom.b)) /// } /// /// fn into_nested<'a,Ty>(self)->Option<Ty> /// where /// FP!(foo::Boom.b): OptRevIntoFieldRef<'a,Self,Ty=Ty> /// { /// self.into_field(fp!(foo::Boom.b)) /// } /// } /// /// impl<T> GetNested for T {} /// /// ``` pub trait OptRevIntoFieldRef<'a,This>= OptRevIntoField<This> + OptRevGetFieldMut<'a,This> }