diff options
author | John-John Tedro <udoprog@tedro.se> | 2018-05-09 21:26:59 +0200 |
---|---|---|
committer | John-John Tedro <udoprog@tedro.se> | 2018-05-09 21:54:22 +0200 |
commit | 0a5fe3fff9a11b9684eb41be302606de0770f22d (patch) | |
tree | 2e561e663ef29d9f8f8a9977335c77ef9684f1a9 /src | |
parent | 87916e99adb29aac618b380f55c936a9cabdfc3a (diff) | |
download | milf-rs-0a5fe3fff9a11b9684eb41be302606de0770f22d.tar.gz milf-rs-0a5fe3fff9a11b9684eb41be302606de0770f22d.zip |
Make spanned module private and hide internals
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/spanned.rs | 46 |
2 files changed, 41 insertions, 7 deletions
@@ -170,6 +170,6 @@ mod tokens; #[doc(hidden)] pub mod macros; -pub mod spanned; +mod spanned; #[doc(no_inline)] pub use spanned::Spanned; diff --git a/src/spanned.rs b/src/spanned.rs index 8fbc529..abbbd49 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -3,7 +3,7 @@ //! extern crate serde_derive; //! //! extern crate toml; -//! use toml::spanned::Spanned; +//! use toml::Spanned; //! //! #[derive(Deserialize)] //! struct Value { @@ -15,8 +15,10 @@ //! //! let u: Value = toml::from_str(t).unwrap(); //! -//! assert_eq!(u.s.start, 4); -//! assert_eq!(u.s.end, 11); +//! assert_eq!(u.s.start(), 4); +//! assert_eq!(u.s.end(), 11); +//! assert_eq!(u.s.get_ref(), "value"); +//! assert_eq!(u.s.into_inner(), String::from("value")); //! } //! ``` @@ -36,11 +38,43 @@ pub const VALUE: &'static str = "$__toml_private_value"; #[derive(Debug)] pub struct Spanned<T> { /// The start range. - pub start: usize, + start: usize, /// The end range (exclusive). - pub end: usize, + end: usize, /// The spanned value. - pub value: T, + value: T, +} + +impl<T> Spanned<T> { + /// Access the start of the span of the contained value. + pub fn start(&self) -> usize { + self.start + } + + /// Access the end of the span of the contained value. + pub fn end(&self) -> usize { + self.end + } + + /// Get the span of the contained value. + pub fn span(&self) -> (usize, usize) { + (self.start, self.end) + } + + /// Consumes the spanned value and returns the contained value. + pub fn into_inner(self) -> T { + self.value + } + + /// Returns a reference to the contained value. + pub fn get_ref(&self) -> &T { + &self.value + } + + /// Returns a mutable reference to the contained value. + pub fn get_mut(&self) -> &T { + &self.value + } } impl<'de, T> de::Deserialize<'de> for Spanned<T> |