use super::Location;
#[derive(Clone)]
pub struct Located<E> {
inner: E,
location: Location,
}
impl<E> Located<E> {
pub fn new(inner: E, location: Location) -> Self {
Located { inner, location }
}
pub fn unplaced(inner: E) -> Self {
Located {
inner,
location: Location::Unplaced,
}
}
pub fn inner(&self) -> &E {
&self.inner
}
pub fn into_inner(self) -> E {
self.inner
}
pub fn location(&self) -> &Location {
&self.location
}
pub fn into_location(self) -> Location {
self.location
}
pub fn into_parts(self) -> (E, Location) {
(self.inner, self.location)
}
pub fn map<F, T>(self, f: F) -> Located<T>
where
F: FnOnce(E) -> T,
{
Located {
inner: f(self.inner),
location: self.location,
}
}
}
impl<E> std::fmt::Display for Located<E>
where
E: std::fmt::Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl<E> std::fmt::Debug for Located<E>
where
E: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl<E> PartialEq for Located<E>
where
E: std::cmp::PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<E> Eq for Located<E> where E: std::cmp::Eq {}
impl<E> PartialOrd for Located<E>
where
E: std::cmp::PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.inner.partial_cmp(&other.inner)
}
}
impl<E> Ord for Located<E>
where
E: std::cmp::Ord,
{
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.inner.cmp(&other.inner)
}
}
impl<E> std::hash::Hash for Located<E>
where
E: std::hash::Hash,
{
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.inner.hash(state);
}
}
impl<E> std::ops::Deref for Located<E> {
type Target = E;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<E> std::borrow::Borrow<E> for Located<E> {
fn borrow(&self) -> &E {
&self.inner
}
}