1#![warn(missing_docs)]
2
3extern crate stable_deref_trait;
247pub use stable_deref_trait::{StableDeref as StableAddress, CloneStableDeref as CloneStableAddress};
248
249pub struct OwningRef<O, T: ?Sized> {
259 owner: O,
260 reference: *const T,
261}
262
263pub struct OwningRefMut<O, T: ?Sized> {
273 owner: O,
274 reference: *mut T,
275}
276
277pub trait Erased {}
281impl<T> Erased for T {}
282
283pub unsafe trait IntoErased<'a> {
287 type Erased;
289 fn into_erased(self) -> Self::Erased;
291}
292
293impl<O, T: ?Sized> OwningRef<O, T> {
298 pub fn new(o: O) -> Self
312 where O: StableAddress,
313 O: Deref<Target = T>,
314 {
315 OwningRef {
316 reference: &*o,
317 owner: o,
318 }
319 }
320
321 pub unsafe fn new_assert_stable_address(o: O) -> Self
327 where O: Deref<Target = T>,
328 {
329 OwningRef {
330 reference: &*o,
331 owner: o,
332 }
333 }
334
335 pub fn map<F, U: ?Sized>(self, f: F) -> OwningRef<O, U>
356 where O: StableAddress,
357 F: FnOnce(&T) -> &U
358 {
359 OwningRef {
360 reference: f(&self),
361 owner: self.owner,
362 }
363 }
364
365 pub fn map_with_owner<F, U: ?Sized>(self, f: F) -> OwningRef<O, U>
388 where O: StableAddress,
389 F: for<'a> FnOnce(&'a O, &'a T) -> &'a U
390 {
391 OwningRef {
392 reference: f(&self.owner, &self),
393 owner: self.owner,
394 }
395 }
396
397 pub fn try_map<F, U: ?Sized, E>(self, f: F) -> Result<OwningRef<O, U>, E>
420 where O: StableAddress,
421 F: FnOnce(&T) -> Result<&U, E>
422 {
423 Ok(OwningRef {
424 reference: f(&self)?,
425 owner: self.owner,
426 })
427 }
428
429 pub fn try_map_with_owner<F, U: ?Sized, E>(self, f: F) -> Result<OwningRef<O, U>, E>
453 where O: StableAddress,
454 F: for<'a> FnOnce(&'a O, &'a T) -> Result<&'a U, E>
455 {
456 Ok(OwningRef {
457 reference: f(&self.owner, &self)?,
458 owner: self.owner,
459 })
460 }
461
462 pub unsafe fn map_owner<F, P>(self, f: F) -> OwningRef<P, T>
468 where O: StableAddress,
469 P: StableAddress,
470 F: FnOnce(O) -> P
471 {
472 OwningRef {
473 reference: self.reference,
474 owner: f(self.owner),
475 }
476 }
477
478 pub fn map_owner_box(self) -> OwningRef<Box<O>, T> {
484 OwningRef {
485 reference: self.reference,
486 owner: Box::new(self.owner),
487 }
488 }
489
490 pub fn erase_owner<'a>(self) -> OwningRef<O::Erased, T>
523 where O: IntoErased<'a>,
524 {
525 OwningRef {
526 reference: self.reference,
527 owner: self.owner.into_erased(),
528 }
529 }
530
531 pub fn as_owner(&self) -> &O {
535 &self.owner
536 }
537
538 pub fn into_owner(self) -> O {
540 self.owner
541 }
542}
543
544impl<O, T: ?Sized> OwningRefMut<O, T> {
545 pub fn new(mut o: O) -> Self
559 where O: StableAddress,
560 O: DerefMut<Target = T>,
561 {
562 OwningRefMut {
563 reference: &mut *o,
564 owner: o,
565 }
566 }
567
568 pub unsafe fn new_assert_stable_address(mut o: O) -> Self
574 where O: DerefMut<Target = T>,
575 {
576 OwningRefMut {
577 reference: &mut *o,
578 owner: o,
579 }
580 }
581
582 pub fn map<F, U: ?Sized>(mut self, f: F) -> OwningRef<O, U>
603 where O: StableAddress,
604 F: FnOnce(&mut T) -> &U
605 {
606 OwningRef {
607 reference: f(&mut self),
608 owner: self.owner,
609 }
610 }
611
612 pub fn map_mut<F, U: ?Sized>(mut self, f: F) -> OwningRefMut<O, U>
633 where O: StableAddress,
634 F: FnOnce(&mut T) -> &mut U
635 {
636 OwningRefMut {
637 reference: f(&mut self),
638 owner: self.owner,
639 }
640 }
641
642 pub fn try_map<F, U: ?Sized, E>(mut self, f: F) -> Result<OwningRef<O, U>, E>
665 where O: StableAddress,
666 F: FnOnce(&mut T) -> Result<&U, E>
667 {
668 Ok(OwningRef {
669 reference: f(&mut self)?,
670 owner: self.owner,
671 })
672 }
673
674 pub fn try_map_mut<F, U: ?Sized, E>(mut self, f: F) -> Result<OwningRefMut<O, U>, E>
697 where O: StableAddress,
698 F: FnOnce(&mut T) -> Result<&mut U, E>
699 {
700 Ok(OwningRefMut {
701 reference: f(&mut self)?,
702 owner: self.owner,
703 })
704 }
705
706 pub unsafe fn map_owner<F, P>(self, f: F) -> OwningRefMut<P, T>
712 where O: StableAddress,
713 P: StableAddress,
714 F: FnOnce(O) -> P
715 {
716 OwningRefMut {
717 reference: self.reference,
718 owner: f(self.owner),
719 }
720 }
721
722 pub fn map_owner_box(self) -> OwningRefMut<Box<O>, T> {
728 OwningRefMut {
729 reference: self.reference,
730 owner: Box::new(self.owner),
731 }
732 }
733
734 pub fn erase_owner<'a>(self) -> OwningRefMut<O::Erased, T>
767 where O: IntoErased<'a>,
768 {
769 OwningRefMut {
770 reference: self.reference,
771 owner: self.owner.into_erased(),
772 }
773 }
774
775 pub fn as_owner(&self) -> &O {
779 &self.owner
780 }
781
782 pub fn as_owner_mut(&mut self) -> &mut O {
784 &mut self.owner
785 }
786
787 pub fn into_owner(self) -> O {
789 self.owner
790 }
791}
792
793use std::ops::{Deref, DerefMut};
798
799pub struct OwningHandle<O, H>
819 where O: StableAddress, H: Deref,
820{
821 handle: H,
822 _owner: O,
823}
824
825impl<O, H> Deref for OwningHandle<O, H>
826 where O: StableAddress, H: Deref,
827{
828 type Target = H::Target;
829 fn deref(&self) -> &H::Target {
830 self.handle.deref()
831 }
832}
833
834unsafe impl<O, H> StableAddress for OwningHandle<O, H>
835 where O: StableAddress, H: StableAddress,
836{}
837
838impl<O, H> DerefMut for OwningHandle<O, H>
839 where O: StableAddress, H: DerefMut,
840{
841 fn deref_mut(&mut self) -> &mut H::Target {
842 self.handle.deref_mut()
843 }
844}
845
846pub trait ToHandle {
848 type Handle: Deref;
850
851 unsafe fn to_handle(x: *const Self) -> Self::Handle;
854}
855
856pub trait ToHandleMut {
858 type HandleMut: DerefMut;
860
861 unsafe fn to_handle_mut(x: *const Self) -> Self::HandleMut;
864}
865
866impl<O, H> OwningHandle<O, H>
867 where O: StableAddress, O::Target: ToHandle<Handle = H>, H: Deref,
868{
869 pub fn new(o: O) -> Self {
873 OwningHandle::new_with_fn(o, |x| unsafe { O::Target::to_handle(x) })
874 }
875}
876
877impl<O, H> OwningHandle<O, H>
878 where O: StableAddress, O::Target: ToHandleMut<HandleMut = H>, H: DerefMut,
879{
880 pub fn new_mut(o: O) -> Self {
882 OwningHandle::new_with_fn(o, |x| unsafe { O::Target::to_handle_mut(x) })
883 }
884}
885
886impl<O, H> OwningHandle<O, H>
887 where O: StableAddress, H: Deref,
888{
889 pub fn new_with_fn<F>(o: O, f: F) -> Self
894 where F: FnOnce(*const O::Target) -> H
895 {
896 let h: H;
897 {
898 h = f(o.deref() as *const O::Target);
899 }
900
901 OwningHandle {
902 handle: h,
903 _owner: o,
904 }
905 }
906
907 pub fn try_new<F, E>(o: O, f: F) -> Result<Self, E>
912 where F: FnOnce(*const O::Target) -> Result<H, E>
913 {
914 let h: H;
915 {
916 h = f(o.deref() as *const O::Target)?;
917 }
918
919 Ok(OwningHandle {
920 handle: h,
921 _owner: o,
922 })
923 }
924
925 pub fn as_owner(&self) -> &O {
927 &self._owner
928 }
929
930 pub fn into_owner(self) -> O {
932 self._owner
933 }
934}
935
936use std::convert::From;
941use std::fmt::{self, Debug};
942use std::marker::{Send, Sync};
943use std::cmp::{Eq, PartialEq, Ord, PartialOrd, Ordering};
944use std::hash::{Hash, Hasher};
945use std::borrow::Borrow;
946
947impl<O, T: ?Sized> Deref for OwningRef<O, T> {
948 type Target = T;
949
950 fn deref(&self) -> &T {
951 unsafe {
952 &*self.reference
953 }
954 }
955}
956
957impl<O, T: ?Sized> Deref for OwningRefMut<O, T> {
958 type Target = T;
959
960 fn deref(&self) -> &T {
961 unsafe {
962 &*self.reference
963 }
964 }
965}
966
967impl<O, T: ?Sized> DerefMut for OwningRefMut<O, T> {
968 fn deref_mut(&mut self) -> &mut T {
969 unsafe {
970 &mut *self.reference
971 }
972 }
973}
974
975unsafe impl<O, T: ?Sized> StableAddress for OwningRef<O, T> {}
976
977unsafe impl<O, T: ?Sized> StableAddress for OwningRefMut<O, T> {}
978
979impl<O, T: ?Sized> AsRef<T> for OwningRef<O, T> {
980 fn as_ref(&self) -> &T {
981 &*self
982 }
983}
984
985impl<O, T: ?Sized> AsRef<T> for OwningRefMut<O, T> {
986 fn as_ref(&self) -> &T {
987 &*self
988 }
989}
990
991impl<O, T: ?Sized> AsMut<T> for OwningRefMut<O, T> {
992 fn as_mut(&mut self) -> &mut T {
993 &mut *self
994 }
995}
996
997impl<O, T: ?Sized> Borrow<T> for OwningRef<O, T> {
998 fn borrow(&self) -> &T {
999 &*self
1000 }
1001}
1002
1003impl<O, T: ?Sized> From<O> for OwningRef<O, T>
1004 where O: StableAddress,
1005 O: Deref<Target = T>,
1006{
1007 fn from(owner: O) -> Self {
1008 OwningRef::new(owner)
1009 }
1010}
1011
1012impl<O, T: ?Sized> From<O> for OwningRefMut<O, T>
1013 where O: StableAddress,
1014 O: DerefMut<Target = T>
1015{
1016 fn from(owner: O) -> Self {
1017 OwningRefMut::new(owner)
1018 }
1019}
1020
1021impl<O, T: ?Sized> From<OwningRefMut<O, T>> for OwningRef<O, T>
1022 where O: StableAddress,
1023 O: DerefMut<Target = T>
1024{
1025 fn from(other: OwningRefMut<O, T>) -> Self {
1026 OwningRef {
1027 owner: other.owner,
1028 reference: other.reference,
1029 }
1030 }
1031}
1032
1033impl<O, T: ?Sized> Debug for OwningRef<O, T>
1036 where O: Debug,
1037 T: Debug,
1038{
1039 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
1040 write!(f,
1041 "OwningRef {{ owner: {:?}, reference: {:?} }}",
1042 self.as_owner(),
1043 &**self)
1044 }
1045}
1046
1047impl<O, T: ?Sized> Debug for OwningRefMut<O, T>
1048 where O: Debug,
1049 T: Debug,
1050{
1051 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
1052 write!(f,
1053 "OwningRefMut {{ owner: {:?}, reference: {:?} }}",
1054 self.as_owner(),
1055 &**self)
1056 }
1057}
1058
1059impl<O, T: ?Sized> Clone for OwningRef<O, T>
1060 where O: CloneStableAddress,
1061{
1062 fn clone(&self) -> Self {
1063 OwningRef {
1064 owner: self.owner.clone(),
1065 reference: self.reference,
1066 }
1067 }
1068}
1069
1070unsafe impl<O, T: ?Sized> CloneStableAddress for OwningRef<O, T>
1071 where O: CloneStableAddress {}
1072
1073unsafe impl<O, T: ?Sized> Send for OwningRef<O, T>
1074 where O: Send, for<'a> (&'a T): Send {}
1075unsafe impl<O, T: ?Sized> Sync for OwningRef<O, T>
1076 where O: Sync, for<'a> (&'a T): Sync {}
1077
1078unsafe impl<O, T: ?Sized> Send for OwningRefMut<O, T>
1079 where O: Send, for<'a> (&'a mut T): Send {}
1080unsafe impl<O, T: ?Sized> Sync for OwningRefMut<O, T>
1081 where O: Sync, for<'a> (&'a mut T): Sync {}
1082
1083impl Debug for dyn Erased {
1084 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
1085 write!(f, "<dyn Erased>",)
1086 }
1087}
1088
1089impl<O, T: ?Sized> PartialEq for OwningRef<O, T> where T: PartialEq {
1090 fn eq(&self, other: &Self) -> bool {
1091 (&*self as &T).eq(&*other as &T)
1092 }
1093}
1094
1095impl<O, T: ?Sized> Eq for OwningRef<O, T> where T: Eq {}
1096
1097impl<O, T: ?Sized> PartialOrd for OwningRef<O, T> where T: PartialOrd {
1098 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1099 (&*self as &T).partial_cmp(&*other as &T)
1100 }
1101}
1102
1103impl<O, T: ?Sized> Ord for OwningRef<O, T> where T: Ord {
1104 fn cmp(&self, other: &Self) -> Ordering {
1105 (&*self as &T).cmp(&*other as &T)
1106 }
1107}
1108
1109impl<O, T: ?Sized> Hash for OwningRef<O, T> where T: Hash {
1110 fn hash<H: Hasher>(&self, state: &mut H) {
1111 (&*self as &T).hash(state);
1112 }
1113}
1114
1115impl<O, T: ?Sized> PartialEq for OwningRefMut<O, T> where T: PartialEq {
1116 fn eq(&self, other: &Self) -> bool {
1117 (&*self as &T).eq(&*other as &T)
1118 }
1119}
1120
1121impl<O, T: ?Sized> Eq for OwningRefMut<O, T> where T: Eq {}
1122
1123impl<O, T: ?Sized> PartialOrd for OwningRefMut<O, T> where T: PartialOrd {
1124 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1125 (&*self as &T).partial_cmp(&*other as &T)
1126 }
1127}
1128
1129impl<O, T: ?Sized> Ord for OwningRefMut<O, T> where T: Ord {
1130 fn cmp(&self, other: &Self) -> Ordering {
1131 (&*self as &T).cmp(&*other as &T)
1132 }
1133}
1134
1135impl<O, T: ?Sized> Hash for OwningRefMut<O, T> where T: Hash {
1136 fn hash<H: Hasher>(&self, state: &mut H) {
1137 (&*self as &T).hash(state);
1138 }
1139}
1140
1141use std::boxed::Box;
1146use std::rc::Rc;
1147use std::sync::Arc;
1148use std::sync::{MutexGuard, RwLockReadGuard, RwLockWriteGuard};
1149use std::cell::{Ref, RefCell, RefMut};
1150
1151impl<T: 'static> ToHandle for RefCell<T> {
1152 type Handle = Ref<'static, T>;
1153 unsafe fn to_handle(x: *const Self) -> Self::Handle { (*x).borrow() }
1154}
1155
1156impl<T: 'static> ToHandleMut for RefCell<T> {
1157 type HandleMut = RefMut<'static, T>;
1158 unsafe fn to_handle_mut(x: *const Self) -> Self::HandleMut { (*x).borrow_mut() }
1159}
1160
1161pub type BoxRef<T, U = T> = OwningRef<Box<T>, U>;
1167pub type VecRef<T, U = T> = OwningRef<Vec<T>, U>;
1169pub type StringRef = OwningRef<String, str>;
1171
1172pub type RcRef<T, U = T> = OwningRef<Rc<T>, U>;
1174pub type ArcRef<T, U = T> = OwningRef<Arc<T>, U>;
1176
1177pub type RefRef<'a, T, U = T> = OwningRef<Ref<'a, T>, U>;
1179pub type RefMutRef<'a, T, U = T> = OwningRef<RefMut<'a, T>, U>;
1181pub type MutexGuardRef<'a, T, U = T> = OwningRef<MutexGuard<'a, T>, U>;
1183pub type RwLockReadGuardRef<'a, T, U = T> = OwningRef<RwLockReadGuard<'a, T>, U>;
1185pub type RwLockWriteGuardRef<'a, T, U = T> = OwningRef<RwLockWriteGuard<'a, T>, U>;
1187
1188pub type BoxRefMut<T, U = T> = OwningRefMut<Box<T>, U>;
1190pub type VecRefMut<T, U = T> = OwningRefMut<Vec<T>, U>;
1192pub type StringRefMut = OwningRefMut<String, str>;
1194
1195pub type RefMutRefMut<'a, T, U = T> = OwningRefMut<RefMut<'a, T>, U>;
1197pub type MutexGuardRefMut<'a, T, U = T> = OwningRefMut<MutexGuard<'a, T>, U>;
1199pub type RwLockWriteGuardRefMut<'a, T, U = T> = OwningRefMut<RwLockWriteGuard<'a, T>, U>;
1201
1202unsafe impl<'a, T: 'a> IntoErased<'a> for Box<T> {
1203 type Erased = Box<dyn Erased + 'a>;
1204 fn into_erased(self) -> Self::Erased {
1205 self
1206 }
1207}
1208unsafe impl<'a, T: 'a> IntoErased<'a> for Rc<T> {
1209 type Erased = Rc<dyn Erased + 'a>;
1210 fn into_erased(self) -> Self::Erased {
1211 self
1212 }
1213}
1214unsafe impl<'a, T: 'a> IntoErased<'a> for Arc<T> {
1215 type Erased = Arc<dyn Erased + 'a>;
1216 fn into_erased(self) -> Self::Erased {
1217 self
1218 }
1219}
1220
1221pub type ErasedBoxRef<U> = OwningRef<Box<dyn Erased>, U>;
1223pub type ErasedRcRef<U> = OwningRef<Rc<dyn Erased>, U>;
1225pub type ErasedArcRef<U> = OwningRef<Arc<dyn Erased>, U>;
1227
1228pub type ErasedBoxRefMut<U> = OwningRefMut<Box<dyn Erased>, U>;
1230
1231#[cfg(test)]
1232mod tests {
1233 mod owning_ref {
1234 use super::super::OwningRef;
1235 use super::super::{RcRef, BoxRef, Erased, ErasedBoxRef};
1236 use std::cmp::{PartialEq, Ord, PartialOrd, Ordering};
1237 use std::hash::{Hash, Hasher};
1238 use std::collections::hash_map::DefaultHasher;
1239 use std::collections::HashMap;
1240 use std::rc::Rc;
1241
1242 #[derive(Debug, PartialEq)]
1243 struct Example(u32, String, [u8; 3]);
1244 fn example() -> Example {
1245 Example(42, "hello world".to_string(), [1, 2, 3])
1246 }
1247
1248 #[test]
1249 fn new_deref() {
1250 let or: OwningRef<Box<()>, ()> = OwningRef::new(Box::new(()));
1251 assert_eq!(&*or, &());
1252 }
1253
1254 #[test]
1255 fn into() {
1256 let or: OwningRef<Box<()>, ()> = Box::new(()).into();
1257 assert_eq!(&*or, &());
1258 }
1259
1260 #[test]
1261 fn map_offset_ref() {
1262 let or: BoxRef<Example> = Box::new(example()).into();
1263 let or: BoxRef<_, u32> = or.map(|x| &x.0);
1264 assert_eq!(&*or, &42);
1265
1266 let or: BoxRef<Example> = Box::new(example()).into();
1267 let or: BoxRef<_, u8> = or.map(|x| &x.2[1]);
1268 assert_eq!(&*or, &2);
1269 }
1270
1271 #[test]
1272 fn map_heap_ref() {
1273 let or: BoxRef<Example> = Box::new(example()).into();
1274 let or: BoxRef<_, str> = or.map(|x| &x.1[..5]);
1275 assert_eq!(&*or, "hello");
1276 }
1277
1278 #[test]
1279 fn map_static_ref() {
1280 let or: BoxRef<()> = Box::new(()).into();
1281 let or: BoxRef<_, str> = or.map(|_| "hello");
1282 assert_eq!(&*or, "hello");
1283 }
1284
1285 #[test]
1286 fn map_chained() {
1287 let or: BoxRef<String> = Box::new(example().1).into();
1288 let or: BoxRef<_, str> = or.map(|x| &x[1..5]);
1289 let or: BoxRef<_, str> = or.map(|x| &x[..2]);
1290 assert_eq!(&*or, "el");
1291 }
1292
1293 #[test]
1294 fn map_chained_inference() {
1295 let or = BoxRef::new(Box::new(example().1))
1296 .map(|x| &x[..5])
1297 .map(|x| &x[1..3]);
1298 assert_eq!(&*or, "el");
1299 }
1300
1301 #[test]
1302 fn as_owner() {
1303 let or: BoxRef<String> = Box::new(example().1).into();
1304 let or = or.map(|x| &x[..5]);
1305 assert_eq!(&*or, "hello");
1306 assert_eq!(&**or.as_owner(), "hello world");
1307 }
1308
1309 #[test]
1310 fn into_owner() {
1311 let or: BoxRef<String> = Box::new(example().1).into();
1312 let or = or.map(|x| &x[..5]);
1313 assert_eq!(&*or, "hello");
1314 let s = *or.into_owner();
1315 assert_eq!(&s, "hello world");
1316 }
1317
1318 #[test]
1319 fn fmt_debug() {
1320 let or: BoxRef<String> = Box::new(example().1).into();
1321 let or = or.map(|x| &x[..5]);
1322 let s = format!("{:?}", or);
1323 assert_eq!(&s, "OwningRef { owner: \"hello world\", reference: \"hello\" }");
1324 }
1325
1326 #[test]
1327 fn erased_owner() {
1328 let o1: BoxRef<Example, str> = BoxRef::new(Box::new(example()))
1329 .map(|x| &x.1[..]);
1330
1331 let o2: BoxRef<String, str> = BoxRef::new(Box::new(example().1))
1332 .map(|x| &x[..]);
1333
1334 let os: Vec<ErasedBoxRef<str>> = vec![o1.erase_owner(), o2.erase_owner()];
1335 assert!(os.iter().all(|e| &e[..] == "hello world"));
1336 }
1337
1338 #[test]
1339 fn non_static_erased_owner() {
1340 let foo = [413, 612];
1341 let bar = &foo;
1342
1343 fn borrow<'a>(a: &'a &[i32; 2]) -> &'a i32 {
1347 &a[0]
1348 }
1349
1350 let o: BoxRef<&[i32; 2]> = Box::new(bar).into();
1351 let o: BoxRef<&[i32; 2], i32> = o.map(borrow);
1352 let o: BoxRef<dyn Erased, i32> = o.erase_owner();
1353
1354 assert_eq!(*o, 413);
1355 }
1356
1357 #[test]
1358 fn raii_locks() {
1359 use super::super::{RefRef, RefMutRef};
1360 use std::cell::RefCell;
1361 use super::super::{MutexGuardRef, RwLockReadGuardRef, RwLockWriteGuardRef};
1362 use std::sync::{Mutex, RwLock};
1363
1364 {
1365 let a = RefCell::new(1);
1366 let a = {
1367 let a = RefRef::new(a.borrow());
1368 assert_eq!(*a, 1);
1369 a
1370 };
1371 assert_eq!(*a, 1);
1372 drop(a);
1373 }
1374 {
1375 let a = RefCell::new(1);
1376 let a = {
1377 let a = RefMutRef::new(a.borrow_mut());
1378 assert_eq!(*a, 1);
1379 a
1380 };
1381 assert_eq!(*a, 1);
1382 drop(a);
1383 }
1384 {
1385 let a = Mutex::new(1);
1386 let a = {
1387 let a = MutexGuardRef::new(a.lock().unwrap());
1388 assert_eq!(*a, 1);
1389 a
1390 };
1391 assert_eq!(*a, 1);
1392 drop(a);
1393 }
1394 {
1395 let a = RwLock::new(1);
1396 let a = {
1397 let a = RwLockReadGuardRef::new(a.read().unwrap());
1398 assert_eq!(*a, 1);
1399 a
1400 };
1401 assert_eq!(*a, 1);
1402 drop(a);
1403 }
1404 {
1405 let a = RwLock::new(1);
1406 let a = {
1407 let a = RwLockWriteGuardRef::new(a.write().unwrap());
1408 assert_eq!(*a, 1);
1409 a
1410 };
1411 assert_eq!(*a, 1);
1412 drop(a);
1413 }
1414 }
1415
1416 #[test]
1417 fn eq() {
1418 let or1: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
1419 let or2: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
1420 assert_eq!(or1.eq(&or2), true);
1421 }
1422
1423 #[test]
1424 fn cmp() {
1425 let or1: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
1426 let or2: BoxRef<[u8]> = BoxRef::new(vec![4, 5, 6].into_boxed_slice());
1427 assert_eq!(or1.cmp(&or2), Ordering::Less);
1428 }
1429
1430 #[test]
1431 fn partial_cmp() {
1432 let or1: BoxRef<[u8]> = BoxRef::new(vec![4, 5, 6].into_boxed_slice());
1433 let or2: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
1434 assert_eq!(or1.partial_cmp(&or2), Some(Ordering::Greater));
1435 }
1436
1437 #[test]
1438 fn hash() {
1439 let mut h1 = DefaultHasher::new();
1440 let mut h2 = DefaultHasher::new();
1441
1442 let or1: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
1443 let or2: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
1444
1445 or1.hash(&mut h1);
1446 or2.hash(&mut h2);
1447
1448 assert_eq!(h1.finish(), h2.finish());
1449 }
1450
1451 #[test]
1452 fn borrow() {
1453 let mut hash = HashMap::new();
1454 let key = RcRef::<String>::new(Rc::new("foo-bar".to_string())).map(|s| &s[..]);
1455
1456 hash.insert(key.clone().map(|s| &s[..3]), 42);
1457 hash.insert(key.clone().map(|s| &s[4..]), 23);
1458
1459 assert_eq!(hash.get("foo"), Some(&42));
1460 assert_eq!(hash.get("bar"), Some(&23));
1461 }
1462
1463 #[test]
1464 fn total_erase() {
1465 let a: OwningRef<Vec<u8>, [u8]>
1466 = OwningRef::new(vec![]).map(|x| &x[..]);
1467 let b: OwningRef<Box<[u8]>, [u8]>
1468 = OwningRef::new(vec![].into_boxed_slice()).map(|x| &x[..]);
1469
1470 let c: OwningRef<Rc<Vec<u8>>, [u8]> = unsafe {a.map_owner(Rc::new)};
1471 let d: OwningRef<Rc<Box<[u8]>>, [u8]> = unsafe {b.map_owner(Rc::new)};
1472
1473 let e: OwningRef<Rc<dyn Erased>, [u8]> = c.erase_owner();
1474 let f: OwningRef<Rc<dyn Erased>, [u8]> = d.erase_owner();
1475
1476 let _g = e.clone();
1477 let _h = f.clone();
1478 }
1479
1480 #[test]
1481 fn total_erase_box() {
1482 let a: OwningRef<Vec<u8>, [u8]>
1483 = OwningRef::new(vec![]).map(|x| &x[..]);
1484 let b: OwningRef<Box<[u8]>, [u8]>
1485 = OwningRef::new(vec![].into_boxed_slice()).map(|x| &x[..]);
1486
1487 let c: OwningRef<Box<Vec<u8>>, [u8]> = a.map_owner_box();
1488 let d: OwningRef<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
1489
1490 let _e: OwningRef<Box<dyn Erased>, [u8]> = c.erase_owner();
1491 let _f: OwningRef<Box<dyn Erased>, [u8]> = d.erase_owner();
1492 }
1493
1494 #[test]
1495 fn try_map1() {
1496 use std::any::Any;
1497
1498 let x = Box::new(123_i32);
1499 let y: Box<dyn Any> = x;
1500
1501 OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).unwrap();
1502 }
1503
1504 #[test]
1505 fn try_map2() {
1506 use std::any::Any;
1507
1508 let x = Box::new(123_u32);
1509 let y: Box<dyn Any> = x;
1510
1511 OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).unwrap_err();
1512 }
1513
1514 #[test]
1515 fn map_with_owner() {
1516 let owning_ref: BoxRef<Example> = Box::new(example()).into();
1517 let owning_ref = owning_ref.map(|owner| &owner.1);
1518
1519 owning_ref.map_with_owner(|owner, ref_field| {
1520 assert_eq!(owner.1, *ref_field);
1521 ref_field
1522 });
1523 }
1524
1525 #[test]
1526 fn try_map_with_owner_ok() {
1527 let owning_ref: BoxRef<Example> = Box::new(example()).into();
1528 let owning_ref = owning_ref.map(|owner| &owner.1);
1529
1530 owning_ref.try_map_with_owner(|owner, ref_field| {
1531 assert_eq!(owner.1, *ref_field);
1532 Ok(ref_field) as Result<_, ()>
1533 }).unwrap();
1534 }
1535
1536 #[test]
1537 fn try_map_with_owner_err() {
1538 let owning_ref: BoxRef<Example> = Box::new(example()).into();
1539 let owning_ref = owning_ref.map(|owner| &owner.1);
1540
1541 owning_ref.try_map_with_owner(|owner, ref_field| {
1542 assert_eq!(owner.1, *ref_field);
1543 Err(()) as Result<&(), _>
1544 }).unwrap_err();
1545 }
1546 }
1547
1548 mod owning_handle {
1549 use super::super::OwningHandle;
1550 use super::super::RcRef;
1551 use std::rc::Rc;
1552 use std::cell::RefCell;
1553 use std::sync::Arc;
1554 use std::sync::RwLock;
1555
1556 #[test]
1557 fn owning_handle() {
1558 use std::cell::RefCell;
1559 let cell = Rc::new(RefCell::new(2));
1560 let cell_ref = RcRef::new(cell);
1561 let mut handle = OwningHandle::new_with_fn(cell_ref, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
1562 assert_eq!(*handle, 2);
1563 *handle = 3;
1564 assert_eq!(*handle, 3);
1565 }
1566
1567 #[test]
1568 fn try_owning_handle_ok() {
1569 use std::cell::RefCell;
1570 let cell = Rc::new(RefCell::new(2));
1571 let cell_ref = RcRef::new(cell);
1572 let mut handle = OwningHandle::try_new::<_, ()>(cell_ref, |x| {
1573 Ok(unsafe {
1574 x.as_ref()
1575 }.unwrap().borrow_mut())
1576 }).unwrap();
1577 assert_eq!(*handle, 2);
1578 *handle = 3;
1579 assert_eq!(*handle, 3);
1580 }
1581
1582 #[test]
1583 fn try_owning_handle_err() {
1584 use std::cell::RefCell;
1585 let cell = Rc::new(RefCell::new(2));
1586 let cell_ref = RcRef::new(cell);
1587 let handle = OwningHandle::try_new::<_, ()>(cell_ref, |x| {
1588 if false {
1589 return Ok(unsafe {
1590 x.as_ref()
1591 }.unwrap().borrow_mut())
1592 }
1593 Err(())
1594 });
1595 assert!(handle.is_err());
1596 }
1597
1598 #[test]
1599 fn nested() {
1600 use std::cell::RefCell;
1601 use std::sync::{Arc, RwLock};
1602
1603 let result = {
1604 let complex = Rc::new(RefCell::new(Arc::new(RwLock::new("someString"))));
1605 let curr = RcRef::new(complex);
1606 let curr = OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
1607 let mut curr = OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().try_write().unwrap());
1608 assert_eq!(*curr, "someString");
1609 *curr = "someOtherString";
1610 curr
1611 };
1612 assert_eq!(*result, "someOtherString");
1613 }
1614
1615 #[test]
1616 fn owning_handle_safe() {
1617 use std::cell::RefCell;
1618 let cell = Rc::new(RefCell::new(2));
1619 let cell_ref = RcRef::new(cell);
1620 let handle = OwningHandle::new(cell_ref);
1621 assert_eq!(*handle, 2);
1622 }
1623
1624 #[test]
1625 fn owning_handle_mut_safe() {
1626 use std::cell::RefCell;
1627 let cell = Rc::new(RefCell::new(2));
1628 let cell_ref = RcRef::new(cell);
1629 let mut handle = OwningHandle::new_mut(cell_ref);
1630 assert_eq!(*handle, 2);
1631 *handle = 3;
1632 assert_eq!(*handle, 3);
1633 }
1634
1635 #[test]
1636 fn owning_handle_safe_2() {
1637 let result = {
1638 let complex = Rc::new(RefCell::new(Arc::new(RwLock::new("someString"))));
1639 let curr = RcRef::new(complex);
1640 let curr = OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
1641 let mut curr = OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().try_write().unwrap());
1642 assert_eq!(*curr, "someString");
1643 *curr = "someOtherString";
1644 curr
1645 };
1646 assert_eq!(*result, "someOtherString");
1647 }
1648 }
1649
1650 mod owning_ref_mut {
1651 use super::super::{OwningRefMut, BoxRefMut, Erased, ErasedBoxRefMut};
1652 use super::super::BoxRef;
1653 use std::cmp::{PartialEq, Ord, PartialOrd, Ordering};
1654 use std::hash::{Hash, Hasher};
1655 use std::collections::hash_map::DefaultHasher;
1656 use std::collections::HashMap;
1657
1658 #[derive(Debug, PartialEq)]
1659 struct Example(u32, String, [u8; 3]);
1660 fn example() -> Example {
1661 Example(42, "hello world".to_string(), [1, 2, 3])
1662 }
1663
1664 #[test]
1665 fn new_deref() {
1666 let or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
1667 assert_eq!(&*or, &());
1668 }
1669
1670 #[test]
1671 fn new_deref_mut() {
1672 let mut or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
1673 assert_eq!(&mut *or, &mut ());
1674 }
1675
1676 #[test]
1677 fn mutate() {
1678 let mut or: OwningRefMut<Box<usize>, usize> = OwningRefMut::new(Box::new(0));
1679 assert_eq!(&*or, &0);
1680 *or = 1;
1681 assert_eq!(&*or, &1);
1682 }
1683
1684 #[test]
1685 fn into() {
1686 let or: OwningRefMut<Box<()>, ()> = Box::new(()).into();
1687 assert_eq!(&*or, &());
1688 }
1689
1690 #[test]
1691 fn map_offset_ref() {
1692 let or: BoxRefMut<Example> = Box::new(example()).into();
1693 let or: BoxRef<_, u32> = or.map(|x| &mut x.0);
1694 assert_eq!(&*or, &42);
1695
1696 let or: BoxRefMut<Example> = Box::new(example()).into();
1697 let or: BoxRef<_, u8> = or.map(|x| &mut x.2[1]);
1698 assert_eq!(&*or, &2);
1699 }
1700
1701 #[test]
1702 fn map_heap_ref() {
1703 let or: BoxRefMut<Example> = Box::new(example()).into();
1704 let or: BoxRef<_, str> = or.map(|x| &mut x.1[..5]);
1705 assert_eq!(&*or, "hello");
1706 }
1707
1708 #[test]
1709 fn map_static_ref() {
1710 let or: BoxRefMut<()> = Box::new(()).into();
1711 let or: BoxRef<_, str> = or.map(|_| "hello");
1712 assert_eq!(&*or, "hello");
1713 }
1714
1715 #[test]
1716 fn map_mut_offset_ref() {
1717 let or: BoxRefMut<Example> = Box::new(example()).into();
1718 let or: BoxRefMut<_, u32> = or.map_mut(|x| &mut x.0);
1719 assert_eq!(&*or, &42);
1720
1721 let or: BoxRefMut<Example> = Box::new(example()).into();
1722 let or: BoxRefMut<_, u8> = or.map_mut(|x| &mut x.2[1]);
1723 assert_eq!(&*or, &2);
1724 }
1725
1726 #[test]
1727 fn map_mut_heap_ref() {
1728 let or: BoxRefMut<Example> = Box::new(example()).into();
1729 let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x.1[..5]);
1730 assert_eq!(&*or, "hello");
1731 }
1732
1733 #[test]
1734 fn map_mut_static_ref() {
1735 static mut MUT_S: [u8; 5] = *b"hello";
1736
1737 let mut_s: &'static mut [u8] = unsafe { &mut MUT_S };
1738
1739 let or: BoxRefMut<()> = Box::new(()).into();
1740 let or: BoxRefMut<_, [u8]> = or.map_mut(move |_| mut_s);
1741 assert_eq!(&*or, b"hello");
1742 }
1743
1744 #[test]
1745 fn map_mut_chained() {
1746 let or: BoxRefMut<String> = Box::new(example().1).into();
1747 let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x[1..5]);
1748 let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x[..2]);
1749 assert_eq!(&*or, "el");
1750 }
1751
1752 #[test]
1753 fn map_chained_inference() {
1754 let or = BoxRefMut::new(Box::new(example().1))
1755 .map_mut(|x| &mut x[..5])
1756 .map_mut(|x| &mut x[1..3]);
1757 assert_eq!(&*or, "el");
1758 }
1759
1760 #[test]
1761 fn try_map_mut() {
1762 let or: BoxRefMut<String> = Box::new(example().1).into();
1763 let or: Result<BoxRefMut<_, str>, ()> = or.try_map_mut(|x| Ok(&mut x[1..5]));
1764 assert_eq!(&*or.unwrap(), "ello");
1765
1766 let or: BoxRefMut<String> = Box::new(example().1).into();
1767 let or: Result<BoxRefMut<_, str>, ()> = or.try_map_mut(|_| Err(()));
1768 assert!(or.is_err());
1769 }
1770
1771 #[test]
1772 fn as_owner() {
1773 let or: BoxRefMut<String> = Box::new(example().1).into();
1774 let or = or.map_mut(|x| &mut x[..5]);
1775 assert_eq!(&*or, "hello");
1776 assert_eq!(&**or.as_owner(), "hello world");
1777 }
1778
1779 #[test]
1780 fn into_owner() {
1781 let or: BoxRefMut<String> = Box::new(example().1).into();
1782 let or = or.map_mut(|x| &mut x[..5]);
1783 assert_eq!(&*or, "hello");
1784 let s = *or.into_owner();
1785 assert_eq!(&s, "hello world");
1786 }
1787
1788 #[test]
1789 fn fmt_debug() {
1790 let or: BoxRefMut<String> = Box::new(example().1).into();
1791 let or = or.map_mut(|x| &mut x[..5]);
1792 let s = format!("{:?}", or);
1793 assert_eq!(&s,
1794 "OwningRefMut { owner: \"hello world\", reference: \"hello\" }");
1795 }
1796
1797 #[test]
1798 fn erased_owner() {
1799 let o1: BoxRefMut<Example, str> = BoxRefMut::new(Box::new(example()))
1800 .map_mut(|x| &mut x.1[..]);
1801
1802 let o2: BoxRefMut<String, str> = BoxRefMut::new(Box::new(example().1))
1803 .map_mut(|x| &mut x[..]);
1804
1805 let os: Vec<ErasedBoxRefMut<str>> = vec![o1.erase_owner(), o2.erase_owner()];
1806 assert!(os.iter().all(|e| &e[..] == "hello world"));
1807 }
1808
1809 #[test]
1810 fn non_static_erased_owner() {
1811 let mut foo = [413, 612];
1812 let bar = &mut foo;
1813
1814 fn borrow<'a>(a: &'a mut &mut [i32; 2]) -> &'a mut i32 {
1818 &mut a[0]
1819 }
1820
1821 let o: BoxRefMut<&mut [i32; 2]> = Box::new(bar).into();
1822 let o: BoxRefMut<&mut [i32; 2], i32> = o.map_mut(borrow);
1823 let o: BoxRefMut<dyn Erased, i32> = o.erase_owner();
1824
1825 assert_eq!(*o, 413);
1826 }
1827
1828 #[test]
1829 fn raii_locks() {
1830 use super::super::RefMutRefMut;
1831 use std::cell::RefCell;
1832 use super::super::{MutexGuardRefMut, RwLockWriteGuardRefMut};
1833 use std::sync::{Mutex, RwLock};
1834
1835 {
1836 let a = RefCell::new(1);
1837 let a = {
1838 let a = RefMutRefMut::new(a.borrow_mut());
1839 assert_eq!(*a, 1);
1840 a
1841 };
1842 assert_eq!(*a, 1);
1843 drop(a);
1844 }
1845 {
1846 let a = Mutex::new(1);
1847 let a = {
1848 let a = MutexGuardRefMut::new(a.lock().unwrap());
1849 assert_eq!(*a, 1);
1850 a
1851 };
1852 assert_eq!(*a, 1);
1853 drop(a);
1854 }
1855 {
1856 let a = RwLock::new(1);
1857 let a = {
1858 let a = RwLockWriteGuardRefMut::new(a.write().unwrap());
1859 assert_eq!(*a, 1);
1860 a
1861 };
1862 assert_eq!(*a, 1);
1863 drop(a);
1864 }
1865 }
1866
1867 #[test]
1868 fn eq() {
1869 let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
1870 let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
1871 assert_eq!(or1.eq(&or2), true);
1872 }
1873
1874 #[test]
1875 fn cmp() {
1876 let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
1877 let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![4, 5, 6].into_boxed_slice());
1878 assert_eq!(or1.cmp(&or2), Ordering::Less);
1879 }
1880
1881 #[test]
1882 fn partial_cmp() {
1883 let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![4, 5, 6].into_boxed_slice());
1884 let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
1885 assert_eq!(or1.partial_cmp(&or2), Some(Ordering::Greater));
1886 }
1887
1888 #[test]
1889 fn hash() {
1890 let mut h1 = DefaultHasher::new();
1891 let mut h2 = DefaultHasher::new();
1892
1893 let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
1894 let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
1895
1896 or1.hash(&mut h1);
1897 or2.hash(&mut h2);
1898
1899 assert_eq!(h1.finish(), h2.finish());
1900 }
1901
1902 #[test]
1903 fn borrow() {
1904 let mut hash = HashMap::new();
1905 let key1 = BoxRefMut::<String>::new(Box::new("foo".to_string())).map(|s| &s[..]);
1906 let key2 = BoxRefMut::<String>::new(Box::new("bar".to_string())).map(|s| &s[..]);
1907
1908 hash.insert(key1, 42);
1909 hash.insert(key2, 23);
1910
1911 assert_eq!(hash.get("foo"), Some(&42));
1912 assert_eq!(hash.get("bar"), Some(&23));
1913 }
1914
1915 #[test]
1916 fn total_erase() {
1917 let a: OwningRefMut<Vec<u8>, [u8]>
1918 = OwningRefMut::new(vec![]).map_mut(|x| &mut x[..]);
1919 let b: OwningRefMut<Box<[u8]>, [u8]>
1920 = OwningRefMut::new(vec![].into_boxed_slice()).map_mut(|x| &mut x[..]);
1921
1922 let c: OwningRefMut<Box<Vec<u8>>, [u8]> = unsafe {a.map_owner(Box::new)};
1923 let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = unsafe {b.map_owner(Box::new)};
1924
1925 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
1926 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
1927 }
1928
1929 #[test]
1930 fn total_erase_box() {
1931 let a: OwningRefMut<Vec<u8>, [u8]>
1932 = OwningRefMut::new(vec![]).map_mut(|x| &mut x[..]);
1933 let b: OwningRefMut<Box<[u8]>, [u8]>
1934 = OwningRefMut::new(vec![].into_boxed_slice()).map_mut(|x| &mut x[..]);
1935
1936 let c: OwningRefMut<Box<Vec<u8>>, [u8]> = a.map_owner_box();
1937 let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
1938
1939 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
1940 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
1941 }
1942
1943 #[test]
1944 fn try_map1() {
1945 use std::any::Any;
1946
1947 let x = Box::new(123_i32);
1948 let y: Box<dyn Any> = x;
1949
1950 OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).unwrap();
1951 }
1952
1953 #[test]
1954 fn try_map2() {
1955 use std::any::Any;
1956
1957 let x = Box::new(123_u32);
1958 let y: Box<dyn Any> = x;
1959
1960 OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).unwrap_err();
1961 }
1962
1963 #[test]
1964 fn try_map3() {
1965 use std::any::Any;
1966
1967 let x = Box::new(123_i32);
1968 let y: Box<dyn Any> = x;
1969
1970 OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).unwrap();
1971 }
1972
1973 #[test]
1974 fn try_map4() {
1975 use std::any::Any;
1976
1977 let x = Box::new(123_u32);
1978 let y: Box<dyn Any> = x;
1979
1980 OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).unwrap_err();
1981 }
1982
1983 #[test]
1984 fn into_owning_ref() {
1985 use super::super::BoxRef;
1986
1987 let or: BoxRefMut<()> = Box::new(()).into();
1988 let or: BoxRef<()> = or.into();
1989 assert_eq!(&*or, &());
1990 }
1991
1992 struct Foo {
1993 u: u32,
1994 }
1995 struct Bar {
1996 f: Foo,
1997 }
1998
1999 #[test]
2000 fn ref_mut() {
2001 use std::cell::RefCell;
2002
2003 let a = RefCell::new(Bar { f: Foo { u: 42 } });
2004 let mut b = OwningRefMut::new(a.borrow_mut());
2005 assert_eq!(b.f.u, 42);
2006 b.f.u = 43;
2007 let mut c = b.map_mut(|x| &mut x.f);
2008 assert_eq!(c.u, 43);
2009 c.u = 44;
2010 let mut d = c.map_mut(|x| &mut x.u);
2011 assert_eq!(*d, 44);
2012 *d = 45;
2013 assert_eq!(*d, 45);
2014 }
2015 }
2016}