-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from TheSignPainter98/context/rc-file-contents
Use `Rc<str>` for file contents
- Loading branch information
Showing
3 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use std::{ | ||
borrow::Borrow, | ||
fmt::Display, | ||
ops::{Bound, Range, RangeBounds}, | ||
rc::Rc, | ||
}; | ||
|
||
pub trait FileSlice: AsRef<str> { | ||
fn slice<R: RangeBounds<usize>>(&self, index: R) -> FileContentSlice; | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct FileContent { | ||
inner: Rc<str>, | ||
} | ||
|
||
impl FileContent { | ||
pub(crate) fn new(contents: &str) -> Self { | ||
Self { | ||
inner: contents.into(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for FileContent { | ||
fn default() -> Self { | ||
Self::new("") | ||
} | ||
} | ||
|
||
impl FileSlice for FileContent { | ||
fn slice<R: RangeBounds<usize>>(&self, index: R) -> FileContentSlice { | ||
let start = match index.start_bound() { | ||
Bound::Included(i) => *i, | ||
Bound::Excluded(_) => panic!("internal error: excluded left lower bound"), | ||
Bound::Unbounded => 0, | ||
}; | ||
let end = match index.end_bound() { | ||
Bound::Included(i) => *i + 1, | ||
Bound::Excluded(i) => *i, | ||
Bound::Unbounded => self.as_ref().len(), | ||
}; | ||
FileContentSlice { | ||
raw: self.clone(), | ||
range: start..end, | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<str> for FileContent { | ||
fn as_ref(&self) -> &str { | ||
self.inner.as_ref() | ||
} | ||
} | ||
|
||
impl Borrow<str> for FileContent { | ||
fn borrow(&self) -> &str { | ||
self.inner.borrow() | ||
} | ||
} | ||
|
||
impl PartialEq<&str> for FileContent { | ||
fn eq(&self, other: &&str) -> bool { | ||
self.inner.as_ref() == *other | ||
} | ||
} | ||
|
||
impl PartialEq<FileContent> for &str { | ||
fn eq(&self, other: &FileContent) -> bool { | ||
*self == other.inner.as_ref() | ||
} | ||
} | ||
|
||
impl PartialEq<&str> for &FileContent { | ||
fn eq(&self, other: &&str) -> bool { | ||
self.inner.as_ref() == *other | ||
} | ||
} | ||
|
||
impl PartialEq<&FileContent> for &str { | ||
fn eq(&self, other: &&FileContent) -> bool { | ||
*self == other.inner.as_ref() | ||
} | ||
} | ||
|
||
impl Display for FileContent { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.inner.fmt(f) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct FileContentSlice { | ||
raw: FileContent, | ||
range: Range<usize>, | ||
} | ||
|
||
impl FileSlice for FileContentSlice { | ||
fn slice<R: RangeBounds<usize>>(&self, index: R) -> FileContentSlice { | ||
let start = self.range.start | ||
+ match index.start_bound() { | ||
Bound::Included(i) => *i, | ||
Bound::Excluded(_) => panic!("internal error: excluded left lower bound"), | ||
Bound::Unbounded => 0, | ||
}; | ||
let end = self.range.end | ||
+ match index.end_bound() { | ||
Bound::Included(i) => *i + 1, | ||
Bound::Excluded(i) => *i, | ||
Bound::Unbounded => self.as_ref().len(), | ||
}; | ||
Self { | ||
raw: self.raw.clone(), | ||
range: start..end, | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<str> for FileContentSlice { | ||
fn as_ref(&self) -> &str { | ||
&self.raw.as_ref()[self.range.clone()] | ||
} | ||
} | ||
|
||
impl Display for FileContentSlice { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.as_ref().fmt(f) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn slice_unbounded() { | ||
let original = "hello big world"; | ||
let content = FileContent::new(original); | ||
assert_eq!(original, content.slice(..).as_ref()); | ||
} | ||
|
||
#[test] | ||
fn slice_exclusive() { | ||
let original = "hello big world"; | ||
let content = FileContent::new(original); | ||
let range = 1..10; | ||
assert_eq!(&original[range.clone()], content.slice(range).as_ref()); | ||
} | ||
|
||
#[test] | ||
fn slice_inclusive() { | ||
let original = "hello big world"; | ||
let content = FileContent::new(original); | ||
let range = 1..=10; | ||
assert_eq!(&original[range.clone()], content.slice(range).as_ref()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod file_content; | ||
pub(crate) mod file_name; | ||
mod module; | ||
mod resource_limit; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters