opencv_binding_generator/
map_borrowed.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
use std::borrow::{Borrow, Cow};

pub trait CowMapBorrowedExt<'c, 'b, IN, OUT>
where
	IN: ToOwned + ?Sized + 'b,
	OUT: ToOwned + ?Sized + 'b,
{
	fn map_borrowed<F>(self, f: F) -> Cow<'c, OUT>
	where
		F: for<'f> FnOnce(&'f IN) -> Cow<'f, OUT>;
}

impl<'c, 'b, IN, OUT> CowMapBorrowedExt<'c, 'b, IN, OUT> for Cow<'c, IN>
where
	IN: ToOwned + ?Sized + 'b,
	OUT: ToOwned + ?Sized + 'b,
{
	#[inline(always)]
	fn map_borrowed<F>(self, f: F) -> Cow<'c, OUT>
	where
		F: for<'f> FnOnce(&'f IN) -> Cow<'f, OUT>,
	{
		match self {
			Cow::Borrowed(v) => f(v),
			Cow::Owned(v) => Cow::Owned(f(v.borrow()).into_owned()),
		}
	}
}