Skip to content

Commit

Permalink
Add AsLanguageTag trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed May 22, 2023
1 parent 0f22312 commit 37b45ba
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.4]
### Added
- Add `AsLanguageTag` trait.

## [0.2.0]
### Changed
- `LanguageTagBuf::as_ref` now returns a `LanguageTag<&[u8]>`.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "langtag"
version = "0.3.3"
version = "0.3.4"
authors = ["Timothée Haudebourg <author@haudebourg.net>"]
edition = "2018"
categories = ["parsing", "data-structures"]
Expand Down
8 changes: 8 additions & 0 deletions src/as_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::LanguageTag;

/// Type that can be borrowed as a [`LanguageTag`] reference.
///
/// This is the equivalent of `AsRef` for `LanguageTag`.
pub trait AsLanguageTag<T: ?Sized = [u8]> {
fn as_language_tag(&self) -> LanguageTag<T>;
}
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ macro_rules! iterator {
};
}

mod as_ref;
mod error;
mod extension;
mod grandfathered;
Expand All @@ -401,6 +402,7 @@ mod privateuse;
mod variant;

pub use self::langtag::*;
pub use as_ref::*;
pub use error::*;
pub use extension::*;
pub use grandfathered::*;
Expand Down Expand Up @@ -473,6 +475,12 @@ impl LanguageTagBuf {
}
}

impl<T: AsRef<[u8]>> AsLanguageTag<[u8]> for LanguageTagBuf<T> {
fn as_language_tag(&self) -> LanguageTag<[u8]> {
self.as_ref()
}
}

macro_rules! language_tag_impl {
() => {
/// Returns the bytes representation of the language tag.
Expand Down Expand Up @@ -586,6 +594,12 @@ macro_rules! language_tag_impl {
};
}

impl<'a, T: ?Sized> AsLanguageTag<T> for LanguageTag<'a, T> {
fn as_language_tag(&self) -> LanguageTag<T> {
*self
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> LanguageTag<'a, T> {
language_tag_impl!();
}
Expand Down

0 comments on commit 37b45ba

Please sign in to comment.