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
//! This crate only provides the `MaybeOwned` enum
//!
//! Take a look at it's documentation for more information.
//!
#![warn(missing_docs)]
#[cfg(feature = "serde")]
extern crate serde;
#[cfg(feature = "serde")]
mod serde_impls;
mod transitive_impl;
use std::ops::Deref;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::fmt;
use std::borrow::{Cow, Borrow};
use std::str::FromStr;
use self::MaybeOwned::*;
/// This type provides a way to store data to which you either have a
/// reference to or which you do own.
///
/// It provides `From<T>`, `From<&'a T>` implementations and, in difference
/// to `Cow` does _not_ require `ToOwned` to be implemented which makes it
/// compatible with non cloneable data, as a draw back of this it does not
/// know about `ToOwned`. As a consequence of it can't know that `&str` should
/// be the borrowed version of `String` and not `&String` this is especially
/// bad wrt. `Box` as the borrowed version of `Box<T>` would be `&Box<T>`.
///
/// While this crate has some drawbacks compared to `Cow` is has the benefit,
/// that it works with Types which neither implement `Clone` nor `ToOwned`.
/// Another benefit lies in the ability to write API functions which accept
/// a generic parameter `E: Into<MaybeOwned<'a, T>>` as the API consumer can
/// pass `T`, `&'a T` and `MaybeOwned<'a, T>` as argument, without requiring
/// a explicit `Cow::Onwed` or a split into two functions one accepting
/// owed and the other borrowed values.
///
/// # Alternatives
///
/// If you mainly have values implementing `ToOwned` like `&str`/`String`, `Path`/`PathBuf` or
/// `&[T]`/`Vec<T>` using `std::borrow::Cow` might be preferable.
///
/// If you want to be able to treat `&T`, `&mut T`, `Box<T>` and `Arc<T>` the same
/// consider using [`reffers::rbma::RBMA`](https://docs.rs/reffers)
/// (through not all types/platforms are supported because
/// as it relies on the fact that for many pointers the lowest two bits are 0, and stores
/// the discriminant in them, nevertheless this is can only be used with 32bit-aligned data,
/// e.g. using a &u8 _might_ fail). RBMA also allows you to recover a `&mut T` if it was created
/// from `Box<T>`, `&mut T` or a unique `Arc`.
///
///
/// # Examples
///
/// ```
/// # use maybe_owned::MaybeOwned;
/// struct PseudoBigData(u8);
/// fn pseudo_register_fn<'a, E>(_val: E) where E: Into<MaybeOwned<'a, PseudoBigData>> { }
///
/// let data = PseudoBigData(12);
/// let data2 = PseudoBigData(13);
///
/// pseudo_register_fn(&data);
/// pseudo_register_fn(&data);
/// pseudo_register_fn(data2);
/// pseudo_register_fn(MaybeOwned::Owned(PseudoBigData(111)));
/// ```
///
/// ```
/// # use maybe_owned::MaybeOwned;
/// #[repr(C)]
/// struct OpaqueFFI {
/// ref1: * const u8
/// //we also might want to have PhantomData etc.
/// }
///
/// // does not work as it does not implement `ToOwned`
/// // let _ = Cow::Owned(OpaqueFFI { ref1: 0 as *const u8});
///
/// // ok, MaybeOwned can do this (but can't do &str<->String as tread of)
/// let _ = MaybeOwned::Owned(OpaqueFFI { ref1: 0 as *const u8 });
/// ```
///
/// ```
/// # #[macro_use]
/// # extern crate serde_derive;
/// # extern crate serde_json;
/// # extern crate maybe_owned;
/// # #[cfg(feature = "serde")]
/// # fn main() {
/// # use maybe_owned::MaybeOwned;
/// use std::collections::HashMap;
///
/// #[derive(Serialize, Deserialize)]
/// struct SerializedData<'a> {
/// data: MaybeOwned<'a, HashMap<String, i32>>,
/// }
///
/// let mut map = HashMap::new();
/// map.insert("answer".to_owned(), 42);
///
/// // serializing can use borrowed data to avoid unnecessary copying
/// let bytes = serde_json::to_vec(&SerializedData { data: (&map).into() }).unwrap();
///
/// // deserializing creates owned data
/// let deserialized: SerializedData = serde_json::from_slice(&bytes).unwrap();
/// assert_eq!(deserialized.data["answer"], 42);
/// # }
/// # #[cfg(not(feature = "serde"))] fn main() {}
/// ```
///
/// # Transitive `std::ops` implementations
///
/// There are transitive implementations for most operator in `std::ops`.
///
/// A Op between a `MaybeOwned<L>` and `MaybeOwned<R>` is implemented if:
///
/// - L impl the Op with R
/// - L impl the Op with &R
/// - &L impl the Op with R
/// - &L impl the Op with &R
/// - the `Output` of all aboves implementations is
/// the same type
///
///
/// The `Neg` (`-` prefix) op is implemented for `V` if:
///
/// - `V` impl `Neg`
/// - `&V` impl `Neg`
/// - both have the same `Output`
///
///
/// The `Not` (`!` prefix) op is implemented for `V` if:
///
/// - `V` impl `Not`
/// - `&V` impl `Not`
/// - both have the same `Output`
///
/// Adding implementations for Ops which add a `MaybeOwned` to
/// a non `MaybeOwned` value (like `MaybeOwned<T> + T`) requires
/// far reaching specialization in rust and is therefore not done
/// for now.
#[derive(Debug)]
pub enum MaybeOwned<'a, T: 'a> {
/// owns T
Owned(T),
/// has a reference to T
Borrowed(&'a T)
}
impl<'a, T> MaybeOwned<'a, T> {
/// returns true if the data is owned else false
pub fn is_owned(&self) -> bool {
match *self {
Owned(_) => true,
Borrowed(_) => false
}
}
}
impl<'a, T> Deref for MaybeOwned<'a, T> {
type Target = T;
fn deref(&self) -> &T {
match *self {
Owned(ref v) => v,
Borrowed(v) => v
}
}
}
impl<'a, T> AsRef<T> for MaybeOwned<'a, T> {
fn as_ref(&self) -> &T {
self
}
}
impl<'a, T> Borrow<T> for MaybeOwned<'a, T> {
fn borrow(&self) -> &T {
self
}
}
impl<'a, T> From<&'a T> for MaybeOwned<'a, T> {
fn from(v: &'a T) -> MaybeOwned<'a, T> {
Borrowed(v)
}
}
impl<'a, T> From<T> for MaybeOwned<'a, T> {
fn from(v: T) -> MaybeOwned<'a, T> {
Owned(v)
}
}
impl<'a, T> From<Cow<'a, T>> for MaybeOwned<'a, T>
where T: ToOwned<Owned=T>,
{
fn from(cow: Cow<'a, T>) -> MaybeOwned<'a, T> {
match cow {
Cow::Owned(v) => MaybeOwned::Owned(v),
Cow::Borrowed(v) => MaybeOwned::Borrowed(v),
}
}
}
impl<'a, T> Into<Cow<'a, T>> for MaybeOwned<'a, T>
where T: ToOwned<Owned=T>,
{
fn into(self) -> Cow<'a, T> {
match self {
MaybeOwned::Owned(v) => Cow::Owned(v),
MaybeOwned::Borrowed(v) => Cow::Borrowed(v),
}
}
}
impl<'a, T> Default for MaybeOwned<'a, T> where T: Default {
fn default() -> Self {
Owned(T::default())
}
}
impl<'a, T> Clone for MaybeOwned<'a, T> where T: Clone {
fn clone(&self) -> MaybeOwned<'a, T> {
match *self {
Owned(ref v) => Owned(v.clone()),
Borrowed(v) => Borrowed(v)
}
}
}
impl<'a, 'b, A, B> PartialEq<MaybeOwned<'b, B>> for MaybeOwned<'a, A>
where A: PartialEq<B>
{
#[inline]
fn eq(&self, other: &MaybeOwned<'b, B>) -> bool {
PartialEq::eq(self.deref(), other.deref())
}
}
impl<'a, T> Eq for MaybeOwned<'a, T> where T: Eq {}
impl<'a, T> PartialOrd for MaybeOwned<'a, T>
where T: PartialOrd
{
#[inline]
fn partial_cmp(&self, other: &MaybeOwned<'a, T>) -> Option<Ordering> {
PartialOrd::partial_cmp(self.deref(), other.deref())
}
}
impl<'a, T> Ord for MaybeOwned<'a, T>
where T: Ord
{
#[inline]
fn cmp(&self, other: &MaybeOwned<'a, T>) -> Ordering {
Ord::cmp(self.deref(), other.deref())
}
}
impl<'a, T> Hash for MaybeOwned<'a, T>
where T: Hash
{
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(self.deref(), state)
}
}
impl<'a, T> fmt::Display for MaybeOwned<'a, T>
where T: fmt::Display
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Owned(ref o) => fmt::Display::fmt(o, f),
Borrowed(b) => fmt::Display::fmt(b, f),
}
}
}
impl<'a, T> FromStr for MaybeOwned<'a, T>
where T: FromStr
{
type Err = T::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(MaybeOwned::Owned(T::from_str(s)?))
}
}
impl<'a, T> MaybeOwned<'a, T> where T: Clone {
/// Aquires a mutable reference to owned data.
///
/// Clones data if it is not already owned.
///
/// ## Example
///
/// ```
/// use maybe_owned::MaybeOwned;
///
/// #[derive(Clone, Debug, PartialEq, Eq)]
/// struct PseudoBigData(u8);
///
/// let data = PseudoBigData(12);
///
/// let mut maybe: MaybeOwned<PseudoBigData> = (&data).into();
/// assert_eq!(false, maybe.is_owned());
///
/// {
/// let reference = maybe.to_mut();
/// assert_eq!(&mut PseudoBigData(12), reference);
/// }
/// assert!(maybe.is_owned());
/// ```
///
pub fn to_mut(&mut self) -> &mut T {
match *self {
Owned(ref mut v) => v,
Borrowed(v) => {
*self = Owned(v.clone());
match *self {
Owned(ref mut v) => v,
Borrowed(..) => unreachable!()
}
}
}
}
/// Extracts the owned data.
///
/// If the data is borrowed it is cloned before being extracted.
pub fn into_owned(self) -> T {
match self {
Owned(v) => v,
Borrowed(v) => v.clone()
}
}
}
#[cfg(test)]
mod tests {
use super::MaybeOwned;
type TestType = Vec<()>;
fn with_into<'a, I: Into<MaybeOwned<'a, TestType>>>(v: I) -> MaybeOwned<'a, TestType> {
v.into()
}
#[test]
fn is_owned() {
let data = TestType::default();
assert!(MaybeOwned::Owned(data).is_owned());
}
#[test]
fn into_with_owned() {
//ty check if it accepts references
let data = TestType::default();
assert!(with_into(data).is_owned())
}
#[test]
fn into_with_borrow() {
//ty check if it accepts references
let data = TestType::default();
assert!(!with_into(&data).is_owned());
}
#[test]
fn clone_owned() {
let maybe = MaybeOwned::<TestType>::default();
assert!(maybe.clone().is_owned());
}
#[test]
fn clone_borrow() {
let data = TestType::default();
let maybe: MaybeOwned<TestType> = (&data).into();
assert!(!maybe.clone().is_owned());
}
#[test]
fn to_mut() {
let data = TestType::default();
let mut maybe: MaybeOwned<TestType> = (&data).into();
assert!(!maybe.is_owned());
{
let _mut_ref = maybe.to_mut();
}
assert!(maybe.is_owned());
}
#[test]
fn into_inner() {
let data = vec![1u32,2];
let maybe: MaybeOwned<Vec<u32>> = (&data).into();
assert_eq!(data, maybe.into_owned());
}
#[test]
fn has_default() {
#[derive(Default)]
struct TestType(u8);
let _x: MaybeOwned<TestType> = Default::default();
}
#[test]
fn has_clone() {
#[derive(Clone)]
struct TestType(u8);
let _x = TestType(12).clone();
}
#[test]
fn has_partial_eq() {
#[derive(PartialEq)]
struct TestType(f32);
let n = TestType(33.0);
let a = MaybeOwned::Owned(TestType(42.0));
let b = MaybeOwned::Borrowed(&n);
let c = MaybeOwned::Owned(TestType(33.0));
assert_eq!(a == b, false);
assert_eq!(b == c, true);
assert_eq!(c == a, false);
}
#[test]
fn has_eq() {
#[derive(PartialEq, Eq)]
struct TestType(i32);
let n = TestType(33);
let a = MaybeOwned::Owned(TestType(42));
let b = MaybeOwned::Borrowed(&n);
let c = MaybeOwned::Owned(TestType(33));
assert_eq!(a == b, false);
assert_eq!(b == c, true);
assert_eq!(c == a, false);
}
#[test]
fn has_partial_ord() {
#[derive(PartialEq, PartialOrd)]
struct TestType(f32);
let n = TestType(33.0);
let a = MaybeOwned::Owned(TestType(42.0));
let b = MaybeOwned::Borrowed(&n);
let c = MaybeOwned::Owned(TestType(33.0));
assert_eq!(a > b, true);
assert_eq!(b > c, false);
assert_eq!(a < c, false);
}
#[test]
fn has_ord() {
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct TestType(i32);
let n = TestType(33);
let a = MaybeOwned::Owned(TestType(42));
let b = MaybeOwned::Borrowed(&n);
let c = MaybeOwned::Owned(TestType(33));
assert_eq!(a > b, true);
assert_eq!(b > c, false);
assert_eq!(a < c, false);
}
#[test]
fn has_hash() {
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert(MaybeOwned::Owned(42), 33);
assert_eq!(map.get(&MaybeOwned::Borrowed(&42)), Some(&33));
}
#[test]
fn has_display() {
let n = 33;
let a = MaybeOwned::Owned(42);
let b = MaybeOwned::Borrowed(&n);
let s = format!("{} {}", a, b);
assert_eq!(s, "42 33");
}
#[test]
fn from_cow() {
use std::borrow::Cow;
fn test<'a, V: Into<MaybeOwned<'a, i32>>>(v: V, n: i32) { assert_eq!(*v.into(), n) }
let n = 33;
test(Cow::Owned(42), 42);
test(Cow::Borrowed(&n), n);
}
#[test]
fn into_cow() {
use std::borrow::Cow;
fn test<'a, V: Into<Cow<'a, i32>>>(v: V, n: i32) { assert_eq!(*v.into(), n) }
let n = 33;
test(MaybeOwned::Owned(42), 42);
test(MaybeOwned::Borrowed(&n), n);
}
#[test]
fn from_str() {
let as_string = "12";
//assumption as_string is convertable to u32
assert_eq!(12u32, as_string.parse().unwrap());
assert_eq!(MaybeOwned::Owned(12u32), as_string.parse().unwrap());
}
#[test]
fn as_ref() {
let data = TestType::default();
let maybe_owned = MaybeOwned::Borrowed(&data);
let _ref: &TestType = maybe_owned.as_ref();
assert_eq!(
&data as *const _ as usize,
_ref as *const _ as usize
);
}
#[test]
fn borrow() {
use std::borrow::Borrow;
let data = TestType::default();
let maybe_owned = MaybeOwned::Borrowed(&data);
let _ref: &TestType = maybe_owned.borrow();
assert_eq!(
&data as *const _ as usize,
_ref as *const _ as usize
);
}
}