// This file is part of Intrusive.
// Intrusive is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Intrusive is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with Intrusive. If not, see .
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 or the MIT license
// , at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[cfg(all(feature="nostd",not(test)))]
use core::prelude::*;
use std::mem;
use std::ptr;
#[allow(raw_pointer_derive)]
#[derive(Debug)]
pub struct Rawlink {
p: *mut T
}
impl Copy for Rawlink {}
unsafe impl Send for Rawlink {}
unsafe impl Sync for Rawlink {}
/// Rawlink is a type like Option but for holding a raw pointer
impl Rawlink {
/// Like Option::None for Rawlink
pub fn none() -> Rawlink {
Rawlink{p: ptr::null_mut()}
}
/// Like Option::Some for Rawlink
pub fn some(n: &mut T) -> Rawlink {
Rawlink{p: n}
}
/// Convert the `Rawlink` into an Option value
pub fn resolve<'a>(&self) -> Option<&'a T> {
unsafe {
mem::transmute(self.p.as_ref())
}
}
/// Convert the `Rawlink` into an Option value
pub fn resolve_mut<'a>(&mut self) -> Option<&'a mut T> {
if self.p.is_null() {
None
} else {
Some(unsafe { mem::transmute(self.p) })
}
}
/// Return the `Rawlink` and replace with `Rawlink::none()`
pub fn take(&mut self) -> Rawlink {
mem::replace(self, Rawlink::none())
}
}
impl PartialEq for Rawlink {
#[inline]
fn eq(&self, other: &Rawlink) -> bool {
self.p == other.p
}
}
impl Clone for Rawlink {
#[inline]
fn clone(&self) -> Rawlink {
Rawlink{p: self.p}
}
}
impl Default for Rawlink {
fn default() -> Rawlink { Rawlink::none() }
}