Struct rocket::response::Response

source ·
pub struct Response<'r> { /* private fields */ }
Expand description

A response, as returned by types implementing Responder.

See Builder for docs on how a Response is typically created and the module docs for notes on composing responses

Upgrading

A response may optionally register IoHandlers for upgraded requests via Response::add_upgrade() or the corresponding builder method Builder::upgrade(). If the incoming request 1) requests an upgrade via a Connection: Upgrade header and 2) includes a protocol in its Upgrade header that is registered by the returned Response, the connection will be upgraded. An upgrade response is sent to the client, and the registered IoHandler for the client’s preferred protocol is invoked with an IoStream representing a raw byte stream to the client. Note that protocol names are treated case-insensitively during matching.

If a connection is upgraded, Rocket automatically set the following in the upgrade response:

  • The response status to 101 Switching Protocols.
  • The Connection: Upgrade header.
  • The Upgrade header’s value to the selected protocol.

As such, a response should never set a 101 status nor the Connection or Upgrade headers: Rocket handles this automatically. Instead, it should set a status and headers to use in case the connection is not upgraded, either due to an error or because the client did not request an upgrade.

If a connection is not upgraded due to an error, even though there was a matching, registered protocol, the IoHandler is not invoked, and the original response is sent to the client without alteration.

Implementations§

source§

impl<'r> Response<'r>

source

pub fn new() -> Response<'r>

Creates a new, empty Response without a status, body, or headers. Because all HTTP responses must have a status, if a default Response is written to the client without a status, the status defaults to 200 Ok.

Example
use rocket::Response;
use rocket::http::Status;

let mut response = Response::new();

assert_eq!(response.status(), Status::Ok);
assert_eq!(response.headers().len(), 0);
assert!(response.body().is_none());
source

pub fn build() -> Builder<'r>

Returns a Builder with a base of Response::new().

Example
use rocket::Response;

let builder = Response::build();
source

pub fn build_from(other: Response<'r>) -> Builder<'r>

Returns a Builder with a base of other.

Example
use rocket::Response;

let other = Response::new();
let builder = Response::build_from(other);
source

pub fn status(&self) -> Status

Returns the status of self.

Example
use rocket::Response;
use rocket::http::Status;

let mut response = Response::new();
assert_eq!(response.status(), Status::Ok);

response.set_status(Status::NotFound);
assert_eq!(response.status(), Status::NotFound);
source

pub fn set_status(&mut self, status: Status)

Sets the status of self to status.

Example
use rocket::Response;
use rocket::http::Status;

let mut response = Response::new();
response.set_status(Status::ImATeapot);
assert_eq!(response.status(), Status::ImATeapot);
source

pub fn content_type(&self) -> Option<ContentType>

Returns the Content-Type header of self. If the header is not present or is malformed, returns None.

Example
use rocket::Response;
use rocket::http::ContentType;

let mut response = Response::new();
response.set_header(ContentType::HTML);
assert_eq!(response.content_type(), Some(ContentType::HTML));
source

pub fn cookies(&self) -> impl Iterator<Item = Cookie<'_>>

Returns an iterator over the cookies in self as identified by the Set-Cookie header. Malformed cookies are skipped.

Example
use rocket::Response;
use rocket::http::Cookie;

let mut response = Response::new();
response.set_header(Cookie::new("hello", "world!"));
let cookies: Vec<_> = response.cookies().collect();
assert_eq!(cookies, vec![Cookie::new("hello", "world!")]);
source

pub fn headers(&self) -> &HeaderMap<'r>

Returns a HeaderMap of all of the headers in self.

Example
use rocket::Response;
use rocket::http::Header;

let mut response = Response::new();
response.adjoin_raw_header("X-Custom", "1");
response.adjoin_raw_header("X-Custom", "2");

let mut custom_headers = response.headers().iter();
assert_eq!(custom_headers.next(), Some(Header::new("X-Custom", "1")));
assert_eq!(custom_headers.next(), Some(Header::new("X-Custom", "2")));
assert_eq!(custom_headers.next(), None);
source

pub fn set_header<'h: 'r, H: Into<Header<'h>>>(&mut self, header: H) -> bool

Sets the header header in self. Any existing headers with the name header.name will be lost, and only header will remain. The type of header can be any type that implements Into<Header>. This includes Header itself, ContentType and hyper::header types.

Example
use rocket::Response;
use rocket::http::ContentType;

let mut response = Response::new();

response.set_header(ContentType::HTML);
assert_eq!(response.headers().iter().next(), Some(ContentType::HTML.into()));
assert_eq!(response.headers().len(), 1);

response.set_header(ContentType::JSON);
assert_eq!(response.headers().iter().next(), Some(ContentType::JSON.into()));
assert_eq!(response.headers().len(), 1);
source

pub fn set_raw_header<'a: 'r, 'b: 'r, N, V>( &mut self, name: N, value: V ) -> boolwhere N: Into<Cow<'a, str>>, V: Into<Cow<'b, str>>,

Sets the custom header with name name and value value in self. Any existing headers with the same name will be lost, and the new custom header will remain. This method should be used sparingly; prefer to use set_header instead.

Example
use rocket::Response;
use rocket::http::Header;

let mut response = Response::new();

response.set_raw_header("X-Custom", "1");
assert_eq!(response.headers().get_one("X-Custom"), Some("1"));
assert_eq!(response.headers().len(), 1);

response.set_raw_header("X-Custom", "2");
assert_eq!(response.headers().get_one("X-Custom"), Some("2"));
assert_eq!(response.headers().len(), 1);
source

pub fn adjoin_header<'h: 'r, H: Into<Header<'h>>>(&mut self, header: H)

Adds the header header to self. If self contains headers with the name header.name, another header with the same name and value header.value is added. The type of header can be any type that implements Into<Header>. This includes Header itself, ContentType and hyper::header types.

Example
use rocket::Response;
use rocket::http::Header;
use rocket::http::hyper::header::ACCEPT;

let mut response = Response::new();
response.adjoin_header(Header::new(ACCEPT.as_str(), "application/json"));
response.adjoin_header(Header::new(ACCEPT.as_str(), "text/plain"));

let mut accept_headers = response.headers().iter();
assert_eq!(accept_headers.next(), Some(Header::new(ACCEPT.as_str(), "application/json")));
assert_eq!(accept_headers.next(), Some(Header::new(ACCEPT.as_str(), "text/plain")));
assert_eq!(accept_headers.next(), None);
source

pub fn adjoin_raw_header<'a: 'r, 'b: 'r, N, V>(&mut self, name: N, value: V)where N: Into<Cow<'a, str>>, V: Into<Cow<'b, str>>,

Adds a custom header with name name and value value to self. If self already contains headers with the name name, another header with the same name and value is added. The type of header can be any type implements Into<Header>. This includes Header itself, ContentType and hyper::header types.

Example
use rocket::Response;
use rocket::http::Header;

let mut response = Response::new();
response.adjoin_raw_header("X-Custom", "one");
response.adjoin_raw_header("X-Custom", "two");

let mut custom_headers = response.headers().iter();
assert_eq!(custom_headers.next(), Some(Header::new("X-Custom", "one")));
assert_eq!(custom_headers.next(), Some(Header::new("X-Custom", "two")));
assert_eq!(custom_headers.next(), None);
source

pub fn remove_header(&mut self, name: &str)

Removes all headers with the name name.

Example
use rocket::Response;

let mut response = Response::new();

response.adjoin_raw_header("X-Custom", "one");
response.adjoin_raw_header("X-Custom", "two");
response.adjoin_raw_header("X-Other", "hi");
assert_eq!(response.headers().len(), 3);

response.remove_header("X-Custom");
assert_eq!(response.headers().len(), 1);
source

pub fn body(&self) -> &Body<'r>

Returns an immutable borrow of the body of self, if there is one.

Example
use std::io::Cursor;
use rocket::Response;

let mut response = Response::new();
assert!(response.body().is_none());

let string = "Hello, world!";
response.set_sized_body(string.len(), Cursor::new(string));
assert!(response.body().is_some());
source

pub fn upgrade(&mut self, proto: &str) -> Option<Pin<&mut (dyn IoHandler + 'r)>>

Returns the IoHandler for the protocol proto.

Returns Some if such a handler was registered via Response::add_upgrade() or the corresponding builder method upgrade(). Otherwise returns None.

use std::pin::Pin;

use rocket::Response;
use rocket::data::{IoHandler, IoStream};
use rocket::tokio::io;

struct EchoHandler;

#[rocket::async_trait]
impl IoHandler for EchoHandler {
    async fn io(self: Pin<Box<Self>>, io: IoStream) -> io::Result<()> {
        let (mut reader, mut writer) = io::split(io);
        io::copy(&mut reader, &mut writer).await?;
        Ok(())
    }
}

let mut response = Response::new();
assert!(response.upgrade("raw-echo").is_none());

response.add_upgrade("raw-echo", EchoHandler);
assert!(response.upgrade("raw-echo").is_some());
source

pub fn body_mut(&mut self) -> &mut Body<'r>

Returns a mutable borrow of the body of self, if there is one. A mutable borrow allows for reading the body.

Example
use std::io::Cursor;
use rocket::Response;

let mut response = Response::new();
assert!(response.body().is_none());

let string = "Hello, world!";
response.set_sized_body(string.len(), Cursor::new(string));
let string = response.body_mut().to_string().await;
assert_eq!(string.unwrap(), "Hello, world!");
source

pub fn set_sized_body<B, S>(&mut self, size: S, body: B)where B: AsyncRead + AsyncSeek + Send + 'r, S: Into<Option<usize>>,

Sets the body of self to be the fixed-sized body with size size, which may be None. If size is None, the body’s size will be computing with calls to seek just before being written out in a response.

Example
use std::io;
use rocket::Response;

let string = "Hello, world!";

let mut response = Response::new();
response.set_sized_body(string.len(), io::Cursor::new(string));
assert_eq!(response.body_mut().to_string().await?, "Hello, world!");
source

pub fn set_streamed_body<B>(&mut self, body: B)where B: AsyncRead + Send + 'r,

Sets the body of self to body, which will be streamed.

The max chunk size is configured via Response::set_max_chunk_size() and defaults to Body::DEFAULT_MAX_CHUNK.

Example
use tokio::io::{repeat, AsyncReadExt};
use rocket::Response;

let mut response = Response::new();
response.set_streamed_body(repeat(97).take(5));
assert_eq!(response.body_mut().to_string().await?, "aaaaa");
source

pub fn add_upgrade<N, H>(&mut self, protocol: N, handler: H)where N: Into<Uncased<'r>>, H: IoHandler + 'r,

Registers handler as the I/O handler for upgrade protocol protocol.

Responses registering I/O handlers for upgraded protocols should not set the response status to 101, nor set the Connection or Upgrade headers. Rocket automatically sets these headers as needed. See Response#upgrading for details.

If a handler was previously registered for protocol, this handler replaces it. If the connection is upgraded to protocol, the last handler registered for the protocol is used to handle the connection. See IoHandler for details on implementing an I/O handler. For details on connection upgrading, see Response#upgrading.

Example
use std::pin::Pin;

use rocket::Response;
use rocket::data::{IoHandler, IoStream};
use rocket::tokio::io;

struct EchoHandler;

#[rocket::async_trait]
impl IoHandler for EchoHandler {
    async fn io(self: Pin<Box<Self>>, io: IoStream) -> io::Result<()> {
        let (mut reader, mut writer) = io::split(io);
        io::copy(&mut reader, &mut writer).await?;
        Ok(())
    }
}

let mut response = Response::new();
assert!(response.upgrade("raw-echo").is_none());

response.add_upgrade("raw-echo", EchoHandler);
assert!(response.upgrade("raw-echo").is_some());
source

pub fn set_max_chunk_size(&mut self, size: usize)

Sets the body’s maximum chunk size to size bytes.

The default max chunk size is Body::DEFAULT_MAX_CHUNK. The max chunk size is a property of the body and is thus reset whenever a body is set via Response::set_streamed_body(), Response::set_sized_body(), or the corresponding builder methods.

This setting does not typically need to be changed. Configuring a high value can result in high memory usage. Similarly, configuring a low value can result in excessive network writes. When unsure, leave the value unchanged.

Example
use tokio::io::{repeat, AsyncReadExt};
use rocket::Response;

let mut response = Response::new();
response.set_streamed_body(repeat(97).take(5));
response.set_max_chunk_size(3072);
source

pub fn merge(&mut self, other: Response<'r>)

Replaces this response’s status and body with that of other, if they exist in other. Any headers that exist in other replace the ones in self. Any in self that aren’t in other remain in self.

Example
use rocket::Response;
use rocket::http::{Status, ContentType};

let base = Response::build()
    .status(Status::NotFound)
    .header(ContentType::HTML)
    .raw_header("X-Custom", "value 1")
    .finalize();

let response = Response::build()
    .status(Status::ImATeapot)
    .raw_header("X-Custom", "value 2")
    .raw_header_adjoin("X-Custom", "value 3")
    .merge(base)
    .finalize();

assert_eq!(response.status(), Status::NotFound);

let ctype: Vec<_> = response.headers().get("Content-Type").collect();
assert_eq!(ctype, vec![ContentType::HTML.to_string()]);

let custom_values: Vec<_> = response.headers().get("X-Custom").collect();
assert_eq!(custom_values, vec!["value 1"]);
source

pub fn join(&mut self, other: Response<'r>)

Sets self’s status and body to that of other if they are not already set in self. Any headers present in both other and self are adjoined.

Example
use rocket::Response;
use rocket::http::{Status, ContentType};

let other = Response::build()
    .status(Status::NotFound)
    .header(ContentType::HTML)
    .raw_header("X-Custom", "value 1")
    .finalize();

let response = Response::build()
    .status(Status::ImATeapot)
    .raw_header("X-Custom", "value 2")
    .raw_header_adjoin("X-Custom", "value 3")
    .join(other)
    .finalize();

assert_eq!(response.status(), Status::ImATeapot);

let ctype: Vec<_> = response.headers().get("Content-Type").collect();
assert_eq!(ctype, vec![ContentType::HTML.to_string()]);

let custom_values: Vec<_> = response.headers().get("X-Custom").collect();
assert_eq!(custom_values, vec!["value 2", "value 3", "value 1"]);

Trait Implementations§

source§

impl Debug for Response<'_>

source§

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

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

impl<'r> Default for Response<'r>

source§

fn default() -> Response<'r>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'r> !RefUnwindSafe for Response<'r>

§

impl<'r> Send for Response<'r>

§

impl<'r> !Sync for Response<'r>

§

impl<'r> Unpin for Response<'r>

§

impl<'r> !UnwindSafe for Response<'r>

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<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

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

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

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> IntoCollection<T> for T

source§

fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,

Converts self into a collection.
source§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where F: FnMut(T) -> U, A: Array<Item = U>,

source§

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

source§

fn fg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the foreground set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like red() and green(), which have the same functionality but are pithier.

Example

Set foreground color to white using fg():

use yansi::{Paint, Color};

painted.fg(Color::White);

Set foreground color to white using white().

use yansi::Paint;

painted.white();
source§

fn primary(&self) -> Painted<&T>

Returns self with the fg() set to Color::Primary.

Example
println!("{}", value.primary());
source§

fn fixed(&self, color: u8) -> Painted<&T>

Returns self with the fg() set to Color::Fixed.

Example
println!("{}", value.fixed(color));
source§

fn rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the fg() set to Color::Rgb.

Example
println!("{}", value.rgb(r, g, b));
source§

fn black(&self) -> Painted<&T>

Returns self with the fg() set to Color::Black.

Example
println!("{}", value.black());
source§

fn red(&self) -> Painted<&T>

Returns self with the fg() set to Color::Red.

Example
println!("{}", value.red());
source§

fn green(&self) -> Painted<&T>

Returns self with the fg() set to Color::Green.

Example
println!("{}", value.green());
source§

fn yellow(&self) -> Painted<&T>

Returns self with the fg() set to Color::Yellow.

Example
println!("{}", value.yellow());
source§

fn blue(&self) -> Painted<&T>

Returns self with the fg() set to Color::Blue.

Example
println!("{}", value.blue());
source§

fn magenta(&self) -> Painted<&T>

Returns self with the fg() set to Color::Magenta.

Example
println!("{}", value.magenta());
source§

fn cyan(&self) -> Painted<&T>

Returns self with the fg() set to Color::Cyan.

Example
println!("{}", value.cyan());
source§

fn white(&self) -> Painted<&T>

Returns self with the fg() set to Color::White.

Example
println!("{}", value.white());
source§

fn bright_black(&self) -> Painted<&T>

Returns self with the fg() set to Color::BrightBlack.

Example
println!("{}", value.bright_black());
source§

fn bright_red(&self) -> Painted<&T>

Returns self with the fg() set to Color::BrightRed.

Example
println!("{}", value.bright_red());
source§

fn bright_green(&self) -> Painted<&T>

Returns self with the fg() set to Color::BrightGreen.

Example
println!("{}", value.bright_green());
source§

fn bright_yellow(&self) -> Painted<&T>

Returns self with the fg() set to Color::BrightYellow.

Example
println!("{}", value.bright_yellow());
source§

fn bright_blue(&self) -> Painted<&T>

Returns self with the fg() set to Color::BrightBlue.

Example
println!("{}", value.bright_blue());
source§

fn bright_magenta(&self) -> Painted<&T>

Returns self with the fg() set to Color::BrightMagenta.

Example
println!("{}", value.bright_magenta());
source§

fn bright_cyan(&self) -> Painted<&T>

Returns self with the fg() set to Color::BrightCyan.

Example
println!("{}", value.bright_cyan());
source§

fn bright_white(&self) -> Painted<&T>

Returns self with the fg() set to Color::BrightWhite.

Example
println!("{}", value.bright_white());
source§

fn bg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the background set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like on_red() and on_green(), which have the same functionality but are pithier.

Example

Set background color to red using fg():

use yansi::{Paint, Color};

painted.bg(Color::Red);

Set background color to red using on_red().

use yansi::Paint;

painted.on_red();
source§

fn on_primary(&self) -> Painted<&T>

Returns self with the bg() set to Color::Primary.

Example
println!("{}", value.on_primary());
source§

fn on_fixed(&self, color: u8) -> Painted<&T>

Returns self with the bg() set to Color::Fixed.

Example
println!("{}", value.on_fixed(color));
source§

fn on_rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the bg() set to Color::Rgb.

Example
println!("{}", value.on_rgb(r, g, b));
source§

fn on_black(&self) -> Painted<&T>

Returns self with the bg() set to Color::Black.

Example
println!("{}", value.on_black());
source§

fn on_red(&self) -> Painted<&T>

Returns self with the bg() set to Color::Red.

Example
println!("{}", value.on_red());
source§

fn on_green(&self) -> Painted<&T>

Returns self with the bg() set to Color::Green.

Example
println!("{}", value.on_green());
source§

fn on_yellow(&self) -> Painted<&T>

Returns self with the bg() set to Color::Yellow.

Example
println!("{}", value.on_yellow());
source§

fn on_blue(&self) -> Painted<&T>

Returns self with the bg() set to Color::Blue.

Example
println!("{}", value.on_blue());
source§

fn on_magenta(&self) -> Painted<&T>

Returns self with the bg() set to Color::Magenta.

Example
println!("{}", value.on_magenta());
source§

fn on_cyan(&self) -> Painted<&T>

Returns self with the bg() set to Color::Cyan.

Example
println!("{}", value.on_cyan());
source§

fn on_white(&self) -> Painted<&T>

Returns self with the bg() set to Color::White.

Example
println!("{}", value.on_white());
source§

fn on_bright_black(&self) -> Painted<&T>

Returns self with the bg() set to Color::BrightBlack.

Example
println!("{}", value.on_bright_black());
source§

fn on_bright_red(&self) -> Painted<&T>

Returns self with the bg() set to Color::BrightRed.

Example
println!("{}", value.on_bright_red());
source§

fn on_bright_green(&self) -> Painted<&T>

Returns self with the bg() set to Color::BrightGreen.

Example
println!("{}", value.on_bright_green());
source§

fn on_bright_yellow(&self) -> Painted<&T>

Returns self with the bg() set to Color::BrightYellow.

Example
println!("{}", value.on_bright_yellow());
source§

fn on_bright_blue(&self) -> Painted<&T>

Returns self with the bg() set to Color::BrightBlue.

Example
println!("{}", value.on_bright_blue());
source§

fn on_bright_magenta(&self) -> Painted<&T>

Returns self with the bg() set to Color::BrightMagenta.

Example
println!("{}", value.on_bright_magenta());
source§

fn on_bright_cyan(&self) -> Painted<&T>

Returns self with the bg() set to Color::BrightCyan.

Example
println!("{}", value.on_bright_cyan());
source§

fn on_bright_white(&self) -> Painted<&T>

Returns self with the bg() set to Color::BrightWhite.

Example
println!("{}", value.on_bright_white());
source§

fn attr(&self, value: Attribute) -> Painted<&T>

Enables the styling Attribute value.

This method should be used rarely. Instead, prefer to use attribute-specific builder methods like bold() and underline(), which have the same functionality but are pithier.

Example

Make text bold using attr():

use yansi::{Paint, Attribute};

painted.attr(Attribute::Bold);

Make text bold using using bold().

use yansi::Paint;

painted.bold();
source§

fn bold(&self) -> Painted<&T>

Returns self with the attr() set to Attribute::Bold.

Example
println!("{}", value.bold());
source§

fn dim(&self) -> Painted<&T>

Returns self with the attr() set to Attribute::Dim.

Example
println!("{}", value.dim());
source§

fn italic(&self) -> Painted<&T>

Returns self with the attr() set to Attribute::Italic.

Example
println!("{}", value.italic());
source§

fn underline(&self) -> Painted<&T>

Returns self with the attr() set to Attribute::Underline.

Example
println!("{}", value.underline());

Returns self with the attr() set to Attribute::Blink.

Example
println!("{}", value.blink());

Returns self with the attr() set to Attribute::RapidBlink.

Example
println!("{}", value.rapid_blink());
source§

fn invert(&self) -> Painted<&T>

Returns self with the attr() set to Attribute::Invert.

Example
println!("{}", value.invert());
source§

fn conceal(&self) -> Painted<&T>

Returns self with the attr() set to Attribute::Conceal.

Example
println!("{}", value.conceal());
source§

fn strike(&self) -> Painted<&T>

Returns self with the attr() set to Attribute::Strike.

Example
println!("{}", value.strike());
source§

fn quirk(&self, value: Quirk) -> Painted<&T>

Enables the yansi Quirk value.

This method should be used rarely. Instead, prefer to use quirk-specific builder methods like mask() and wrap(), which have the same functionality but are pithier.

Example

Enable wrapping using .quirk():

use yansi::{Paint, Quirk};

painted.quirk(Quirk::Wrap);

Enable wrapping using wrap().

use yansi::Paint;

painted.wrap();
source§

fn mask(&self) -> Painted<&T>

Returns self with the quirk() set to Quirk::Mask.

Example
println!("{}", value.mask());
source§

fn wrap(&self) -> Painted<&T>

Returns self with the quirk() set to Quirk::Wrap.

Example
println!("{}", value.wrap());
source§

fn linger(&self) -> Painted<&T>

Returns self with the quirk() set to Quirk::Linger.

Example
println!("{}", value.linger());
source§

fn clear(&self) -> Painted<&T>

Returns self with the quirk() set to Quirk::Clear.

Example
println!("{}", value.clear());
source§

fn bright(&self) -> Painted<&T>

Returns self with the quirk() set to Quirk::Bright.

Example
println!("{}", value.bright());
source§

fn on_bright(&self) -> Painted<&T>

Returns self with the quirk() set to Quirk::OnBright.

Example
println!("{}", value.on_bright());
source§

fn whenever(&self, value: Condition) -> Painted<&T>

Conditionally enable styling based on whether the Condition value applies. Replaces any previous condition.

See the crate level docs for more details.

Example

Enable styling painted only when both stdout and stderr are TTYs:

use yansi::{Paint, Condition};

painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
source§

fn new(self) -> Painted<Self>where Self: Sized,

Create a new Painted with a default Style. Read more
source§

fn paint<S>(&self, style: S) -> Painted<&Self>where S: Into<Style>,

Apply a style wholesale to self. Any previous style is replaced. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
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.
source§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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