Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move to using Cow<'static, str> for most headers/request options #953

Merged
merged 6 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions sdk/core/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::{borrow::Cow, fmt::Debug};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AccessToken(String);
pub struct AccessToken(Cow<'static, str>);

impl AccessToken {
pub fn new(access_token: String) -> Self {
Self(access_token)
pub fn new<T>(access_token: T) -> Self
where
T: Into<Cow<'static, str>>,
{
Self(access_token.into())
}

pub fn secret(&self) -> &str {
self.0.as_str()
&self.0
}
}

Expand Down
3 changes: 2 additions & 1 deletion sdk/core/src/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl HeaderValue {
Self(std::borrow::Cow::Borrowed(s))
}

fn from_cow<C>(c: C) -> Self
pub fn from_cow<C>(c: C) -> Self
where
C: Into<std::borrow::Cow<'static, str>>,
{
Expand Down Expand Up @@ -299,6 +299,7 @@ pub const IF_UNMODIFIED_SINCE: HeaderName = HeaderName::from_static("if-unmodifi
pub const IF_SEQUENCE_NUMBER_EQ: HeaderName = HeaderName::from_static("x-ms-if-sequence-number-eq");
pub const IF_SEQUENCE_NUMBER_LE: HeaderName = HeaderName::from_static("x-ms-if-sequence-number-le");
pub const IF_SEQUENCE_NUMBER_LT: HeaderName = HeaderName::from_static("x-ms-if-sequence-number-lt");
pub const IF_TAGS: HeaderName = HeaderName::from_static("x-ms-if-tags");
pub const ITEM_COUNT: HeaderName = HeaderName::from_static("x-ms-item-count");
pub const ITEM_TYPE: HeaderName = HeaderName::from_static("x-ms-item-type");
pub const KEEP_ALIVE: HeaderName = HeaderName::from_static("keep-alive");
Expand Down
94 changes: 94 additions & 0 deletions sdk/core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,100 @@ macro_rules! future {
};
}

/// The following macro invocation:
/// ```
/// # #[macro_use] extern crate azure_core;
/// create_request_header_cow!(ClientRequestId, CLIENT_REQUEST_ID, "builds a client request id",);
/// ```
/// Turns into a Header value used to construct requests.
#[macro_export]
macro_rules! create_request_header_cow {
($name:ident, $header:ident $(, $doc:expr)?, $(($variant:ident, $value:expr)), *) => {
$( #[doc = $doc] )?
#[derive(Debug, Clone)]
pub struct $name(std::borrow::Cow<'static, str>);

impl $name {
$(
pub const $variant: $name = $name::from_static($value);
)*

pub fn new<S>(s: S) -> Self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like most of this stuff is shared. Can we share the definition? We can probably do all of this in one macro since the invocation is unambiguous between the two use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There needs to be some disambiguation because of either Header or AppendToUrlQuery implementation.

where
S: Into<std::borrow::Cow<'static, str>>,
{
Self(s.into())
}

pub const fn from_static(s: &'static str) -> Self {
Self(std::borrow::Cow::Borrowed(s))
}
}

impl<S> From<S> for $name
where
S: Into<std::borrow::Cow<'static, str>>,
{
fn from(s: S) -> Self {
Self::new(s)
}
}

impl $crate::headers::Header for $name {
fn name(&self) -> $crate::headers::HeaderName {
$crate::headers::$header
}

fn value(&self) -> $crate::headers::HeaderValue {
$crate::headers::HeaderValue::from_cow(self.0.clone())
}
}
};
}

/// The following macro invocation:
/// ```
/// # #[macro_use] extern crate azure_core;
/// create_request_query_cow!(Prefix, "prefix");
/// ```
/// Turns into a request query option used to construct requests
#[macro_export]
macro_rules! create_request_query_cow {
bmc-msft marked this conversation as resolved.
Show resolved Hide resolved
($name:ident, $option:expr $(, $doc:expr)?) => {
bmc-msft marked this conversation as resolved.
Show resolved Hide resolved
$( #[doc = $doc] )?
#[derive(Debug, Clone)]
pub struct $name(std::borrow::Cow<'static, str>);

impl $name {
pub fn new<S>(s: S) -> Self
where
S: Into<std::borrow::Cow<'static, str>>,
{
Self(s.into())
}

pub const fn from_static(s: &'static str) -> Self {
Self(std::borrow::Cow::Borrowed(s))
}
}

impl<S> From<S> for $name
where
S: Into<std::borrow::Cow<'static, str>>,
{
fn from(s: S) -> Self {
Self::new(s)
}
}

impl $crate::AppendToUrlQuery for $name {
fn append_to_url_query(&self, url: &mut url::Url) {
url.query_pairs_mut().append_pair($option, &self.0);
}
}
};
}

/// The following macro invocation:
/// ```
/// # #[macro_use] extern crate azure_core;
Expand Down
39 changes: 0 additions & 39 deletions sdk/core/src/request_options/accept.rs

This file was deleted.

40 changes: 0 additions & 40 deletions sdk/core/src/request_options/accept_encoding.rs

This file was deleted.

29 changes: 0 additions & 29 deletions sdk/core/src/request_options/activity_id.rs

This file was deleted.

30 changes: 0 additions & 30 deletions sdk/core/src/request_options/app.rs

This file was deleted.

31 changes: 0 additions & 31 deletions sdk/core/src/request_options/client_request_id.rs

This file was deleted.

31 changes: 0 additions & 31 deletions sdk/core/src/request_options/client_version.rs

This file was deleted.

42 changes: 0 additions & 42 deletions sdk/core/src/request_options/content_disposition.rs

This file was deleted.

Loading