Struct bellframe::Bell

source ·
pub struct Bell { /* private fields */ }
Expand description

A type-safe representation of a ‘bell’, which adds things like conversions to and from commonly-used bell names. Each Bell takes a single byte in memory.

Implementations§

source§

impl Bell

source

pub fn from_name(c: char) -> Option<Bell>

Creates a Bell from a char containing a bell name (e.g. '4' or 'T'). If the name is not valid, then this fails and returns None. Note that lower case chars are not considered valid bell names.

Example
use bellframe::Bell;

// Converting a valid name to a `Bell` and back should be the identity function
assert_eq!(Bell::from_name('1')?.name(), "1");
assert_eq!(Bell::from_name('5')?.name(), "5");
assert_eq!(Bell::from_name('0')?.name(), "0");
assert_eq!(Bell::from_name('T')?.name(), "T");
// Converting a lower-case letter should return `None`, even if the upper case
// version is valid
assert_eq!(Bell::from_name('e'), None);
assert_eq!(Bell::from_name('t'), None);
// Converting any old rubbish will return `None` (no shade on Ferris)
assert_eq!(Bell::from_name('\r'), None);
assert_eq!(Bell::from_name('🦀'), None);
source

pub fn from_index(index: u8) -> Bell

Creates a Bell from a 0-indexed integer.

Panics

Panics if 255 is passed (a Bell with index 255 would imply a Stage of 256, which cannot be created).

Example
use bellframe::Bell;

// A 'Bell' with index 0 is the treble
assert_eq!(Bell::from_index(0).name(), "1");
// A 'Bell' with index 11 is the '12' or 'T'
assert_eq!(Bell::from_index(11).name(), "T");
source

pub fn from_number(number: u8) -> Option<Bell>

Creates a Bell from a 1-indexed integer. This could fail if number is 0, so in that case None is returned.

Example
use bellframe::Bell;

// The `Bell` with number '12' is the 12th and should be displayed as 'T'
assert_eq!(Bell::from_number(12)?.name(), "T");
// Trying to create a Bell with number `0` fails:
assert_eq!(Bell::from_number(0), None);
source

pub fn tenor(stage: Stage) -> Bell

Creates the Bell representing the tenor or heaviest bell on a given Stage. This could fail if the Stage has no Bells, so in that case None is returned.

Example
use bellframe::{Bell, Stage};

// The **5** is the 'tenor' when ringing Doubles
assert_eq!(Bell::tenor(Stage::DOUBLES), Bell::from_number(5)?);
// The 12 is the tenor on maximus
assert_eq!(Bell::tenor(Stage::MAXIMUS), Bell::from_number(12)?);
source

pub const TREBLE: Bell = _

A Bell representing the ‘treble’ on any stage. Equivalent to Bell::from_name('1').unwrap().

Example
use bellframe::Bell;

// `TREBLE` should be the bell with name '1'
assert_eq!(Bell::from_name('1'), Some(Bell::TREBLE));
// The `TREBLE` has index 0, and its number is 1
assert_eq!(Bell::TREBLE.index(), 0);
assert_eq!(Bell::TREBLE.number(), 1);
// The treble should display as `"1"`
assert_eq!(Bell::TREBLE.name(), "1");
source

pub const MAX: Bell = _

The largest Bell that can be stored.

source

pub fn to_char(self) -> Option<char>

Converts this Bell into the char that it should be displayed as. If the Bell is too big to have a corresponding name, then None is returned.

Example
use bellframe::Bell;

// A 'Bell' with index 0 is the treble, and therefore displays as `1`
assert_eq!(Bell::from_index(0).to_char(), Some('1'));
// The 11th should display as 'E'
assert_eq!(Bell::from_number(11)?.to_char(), Some('E'));

// Trying to display the 100th Bell fails:
assert_eq!(Bell::from_number(100)?.to_char(), None);
source

pub fn index(self) -> usize

Returns the 0-indexed representation of this Bell.

Example
use bellframe::Bell;

// Creating a `Bell` with `from_index` should return the same index passed to it
assert_eq!(Bell::from_index(0).index(), 0);
assert_eq!(Bell::from_index(12).index(), 12);

assert_eq!(Bell::from_name('8')?.index(), 7);
assert_eq!(Bell::from_name('0')?.index(), 9);
assert_eq!(Bell::from_name('T')?.index(), 11);
source

pub fn index_u8(self) -> u8

Returns the 0-indexed representation of this Bell, as a u8.

Example
use bellframe::Bell;

// Creating a `Bell` with `from_index` should return the same index passed to it
assert_eq!(Bell::from_index(0).index_u8(), 0);
assert_eq!(Bell::from_index(12).index_u8(), 12);

assert_eq!(Bell::from_name('8')?.index_u8(), 7);
assert_eq!(Bell::from_name('0')?.index_u8(), 9);
assert_eq!(Bell::from_name('T')?.index_u8(), 11);
source

pub fn number(self) -> u8

Returns the 1-indexed representation of this Bell.

Example
use bellframe::Bell;

assert_eq!(Bell::from_index(0).number(), 1);
assert_eq!(Bell::from_name('0')?.number(), 10);
// Using `from_number` should return the same number that was passed to it
assert_eq!(Bell::from_number(4)?.number(), 4);
assert_eq!(Bell::from_number(10)?.number(), 10);
source

pub fn name(self) -> String

Converts this Bell into a String that it should be displayed as. Unlike to_char, this does not fail if the Bell is to big to have a name. Instead, it returns the 1-indexed ‘number’ of the Bell in angle brackets.

Example
use bellframe::Bell;

// Bells which are <= 9th should return their number as a `String`
assert_eq!(Bell::from_number(1)?.name(), "1");
assert_eq!(Bell::from_number(5)?.name(), "5");
assert_eq!(Bell::from_number(9)?.name(), "9");
// The 10th display as "0"
assert_eq!(Bell::from_number(10)?.name(), "0");
// Other bells display as their single-character names
assert_eq!(Bell::from_number(12)?.name(), "T");
assert_eq!(Bell::from_number(16)?.name(), "D");
// Anything too big simply displays as '<{bell.number()}>'
assert_eq!(Bell::from_number(100)?.name(), "<100>");

Trait Implementations§

source§

impl Add<i16> for Bell

§

type Output = Bell

The resulting type after applying the + operator.
source§

fn add(self, rhs: i16) -> Self::Output

Performs the + operation. Read more
source§

impl AddAssign<i16> for Bell

source§

fn add_assign(&mut self, rhs: i16)

Performs the += operation. Read more
source§

impl Clone for Bell

source§

fn clone(&self) -> Bell

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Bell

source§

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

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

impl Display for Bell

source§

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

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

impl Hash for Bell

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 Ord for Bell

source§

fn cmp(&self, other: &Bell) -> Ordering

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

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

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<Bell> for Bell

source§

fn eq(&self, other: &Bell) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Bell> for Bell

source§

fn partial_cmp(&self, other: &Bell) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub<i16> for Bell

§

type Output = Bell

The resulting type after applying the - operator.
source§

fn sub(self, rhs: i16) -> Self::Output

Performs the - operation. Read more
source§

impl SubAssign<i16> for Bell

source§

fn sub_assign(&mut self, rhs: i16)

Performs the -= operation. Read more
source§

impl Copy for Bell

source§

impl Eq for Bell

source§

impl StructuralEq for Bell

source§

impl StructuralPartialEq for Bell

Auto Trait Implementations§

§

impl RefUnwindSafe for Bell

§

impl Send for Bell

§

impl Sync for Bell

§

impl Unpin for Bell

§

impl UnwindSafe for Bell

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more