SRAM Rival 22 mid-cage derailleurs work with 11-28 to 11-32 cassettes. Rival 22 cranks come in 52/36, 50/34 and 46/36 so I'm guessing the total capacity is (32-11) + (50-34) = 37.
If you will be running as pseudo 1x setup by not using the big ring, you will have plenty of capacity in the rear for a 11-40 cassette. Derailleur hanger extensions do apparently work but you may find you have some shifting issues.
Consider disabling the front derailleur, changing to the big ring by mistake could wreck the rear derailleur.