Struct Linear

Source
pub struct Linear<T>(/* private fields */);
Expand description

A linear type that must be destructured to access the inner value.

Implementations§

Source§

impl<T> Linear<T>

Source

pub const fn new(inner: T) -> Self

Creates a new linear type.

Examples found in repository?
examples/simple_states.rs (line 28)
26fn main() {
27    // Create a linear type and transition through the states
28    let file_content = Linear::new(Filename("README.md"))
29        .map(open_file)
30        .map_ok(read_text)
31        .unwrap_ok();
32
33    // destructure the file content
34    let FileContent(text) = file_content.into_inner();
35    assert!(text.contains("# Example"));
36}
Source

pub fn get_ref(&self) -> &T

Returns a reference to the inner value.

In a pure linear type-system even immutable access to the inner value is not available because this may leak unwanted interior mutability or enable to clone the inner which would be impure (in a linear type system). When one doesn’t do either then the rust type system/lifetimes are strong enough to be pure. This method is only available when one defines the semipure feature. It’s then the decision of the programmer not to use any interior mutability/cloning or bend the rules and do something impure to make this crate more convenient to use.

§Example
let linear = Linear::new(123);
assert_eq!(linear.get_ref(), &123);
Source

pub fn into_inner(self) -> T

Destructures the linear type and returns the inner type. This must eventually be called on any linear type, failing to do so will panic.

§Example
let linear = Linear::new(123);
let inner = linear.into_inner();
assert_eq!(inner, 123);
Examples found in repository?
examples/simple_states.rs (line 34)
26fn main() {
27    // Create a linear type and transition through the states
28    let file_content = Linear::new(Filename("README.md"))
29        .map(open_file)
30        .map_ok(read_text)
31        .unwrap_ok();
32
33    // destructure the file content
34    let FileContent(text) = file_content.into_inner();
35    assert!(text.contains("# Example"));
36}
Source

pub fn map<F: FnOnce(T) -> R, R>(self, f: F) -> Linear<R>

Transforms one linear type to another linear type. The inner value is passed to the closure and the return value is wrapped in a Linear.

§Example
let number = Linear::new(123);
let string = number.map(|x| x.to_string());
assert_eq!(string.into_inner(), "123");
Examples found in repository?
examples/simple_states.rs (line 29)
26fn main() {
27    // Create a linear type and transition through the states
28    let file_content = Linear::new(Filename("README.md"))
29        .map(open_file)
30        .map_ok(read_text)
31        .unwrap_ok();
32
33    // destructure the file content
34    let FileContent(text) = file_content.into_inner();
35    assert!(text.contains("# Example"));
36}
Source§

impl<T: Debug, E: Debug> Linear<Result<T, E>>

Additional methods for Linear<Result<R,E>>, only fundamental map and unwrap methods are supported. Anything beyond that needs to be handled manually.

Source

pub fn map_ok<F: FnOnce(T) -> Result<R, E>, R>( self, f: F, ) -> Linear<Result<R, E>>

Transforms a Linear<Result<T,E>> into Linear<Result<R,E>> by applying a function to the Ok value. Retains a Err value.

Examples found in repository?
examples/simple_states.rs (line 30)
26fn main() {
27    // Create a linear type and transition through the states
28    let file_content = Linear::new(Filename("README.md"))
29        .map(open_file)
30        .map_ok(read_text)
31        .unwrap_ok();
32
33    // destructure the file content
34    let FileContent(text) = file_content.into_inner();
35    assert!(text.contains("# Example"));
36}
Source

pub fn map_err<F: FnOnce(E) -> Result<T, R>, R>( self, f: F, ) -> Linear<Result<T, R>>

Transforms a Linear<Result<T,E>> into Linear<Result<T, R>> by applying a function to the Err value. Retains a Ok value.

Source

pub fn unwrap_ok(self) -> Linear<T>

Unwraps a Linear<Result<T,E>> into a Linear<T>.

§Panics

When the value is an Err.

Examples found in repository?
examples/simple_states.rs (line 31)
26fn main() {
27    // Create a linear type and transition through the states
28    let file_content = Linear::new(Filename("README.md"))
29        .map(open_file)
30        .map_ok(read_text)
31        .unwrap_ok();
32
33    // destructure the file content
34    let FileContent(text) = file_content.into_inner();
35    assert!(text.contains("# Example"));
36}
Source

pub fn unwrap_err(self) -> Linear<E>

Unwraps a Linear<Result<T,E>> into a Linear<E>.

§Panics

When the value is an Ok.

Source§

impl<T> Linear<Option<T>>

Additional methods for Linear<Option<T>>, only fundamental methods are supported. Anything beyond that needs to be handled manually.

Source

pub fn map_some<F: FnOnce(T) -> Option<R>, R>(self, f: F) -> Linear<Option<R>>

Transforms a Linear<Option<T>> into Linear<Option<R>> by applying a function to the Some value. Retains a None value.

Source

pub fn or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Self

Transforms a Linear<Option<T>> into Linear<Option<T>> by applying a function to the None value. Retains a Some value.

Source

pub fn unwrap_some(self) -> Linear<T>

Unwraps a Linear<Some<T>> into a Linear<T>.

§Panics

When the value is None.

Trait Implementations§

Source§

impl<T: Debug> Debug for Linear<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Hash> Hash for Linear<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord> Ord for Linear<T>

Source§

fn cmp(&self, other: &Linear<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for Linear<T>

Source§

fn eq(&self, other: &Linear<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for Linear<T>

Source§

fn partial_cmp(&self, other: &Linear<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq> Eq for Linear<T>

Source§

impl<T> StructuralPartialEq for Linear<T>

Auto Trait Implementations§

§

impl<T> Freeze for Linear<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Linear<T>
where T: RefUnwindSafe,

§

impl<T> Send for Linear<T>
where T: Send,

§

impl<T> Sync for Linear<T>
where T: Sync,

§

impl<T> Unpin for Linear<T>
where T: Unpin,

§

impl<T> UnwindSafe for Linear<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.